Fix undefined variable fromConstructor when passing context to getTypes

If passing context to getTypes, it checks value of $context['enable_constructor_extraction'] for true/false or the constructor value of enableConstructorExtraction and should then populate fromConstructor if necessary. The missing brackets around the first part of this check mean that fromConstructor is only applied if context is not set.

This fixes the issuse described at [symfony/symfony-docs#10969](https://github.com/symfony/symfony-docs/pull/10969)
This commit is contained in:
Mantis Development 2019-02-23 16:28:16 +00:00 committed by Fabien Potencier
parent 5b23a2b257
commit 7e4abee02e
2 changed files with 26 additions and 1 deletions

View File

@ -112,7 +112,7 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
}
if (
$context['enable_constructor_extraction'] ?? $this->enableConstructorExtraction &&
($context['enable_constructor_extraction'] ?? $this->enableConstructorExtraction) &&
$fromConstructor = $this->extractFromConstructor($class, $property)
) {
return $fromConstructor;

View File

@ -293,4 +293,29 @@ class ReflectionExtractorTest extends TestCase
[NotInstantiable::class, 'foo', false],
];
}
/**
* @dataProvider constructorTypesProvider
*/
public function testExtractTypeConstructor(string $class, string $property, array $type = null)
{
/* Check that constructor extractions works by default, and if passed in via context.
Check that null is returned if constructor extraction is disabled */
$this->assertEquals($type, $this->extractor->getTypes($class, $property, []));
$this->assertEquals($type, $this->extractor->getTypes($class, $property, ['enable_constructor_extraction' => true]));
$this->assertNull($this->extractor->getTypes($class, $property, ['enable_constructor_extraction' => false]));
}
public function constructorTypesProvider(): array
{
return [
// php71 dummy has following constructor: __construct(string $string, int $intPrivate)
[Php71Dummy::class, 'string', [new Type(Type::BUILTIN_TYPE_STRING, false)]],
[Php71Dummy::class, 'intPrivate', [new Type(Type::BUILTIN_TYPE_INT, false)]],
// Php71DummyExtended2 adds int $intWithAccessor
[Php71DummyExtended2::class, 'intWithAccessor', [new Type(Type::BUILTIN_TYPE_INT, false)]],
[Php71DummyExtended2::class, 'intPrivate', [new Type(Type::BUILTIN_TYPE_INT, false)]],
[DefaultValue::class, 'foo', null],
];
}
}