Skip to content

[PropertyInfo] Dont try to set null when argument is not nullable #38920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class PropertyAccessor implements PropertyAccessorInterface
* to specify the allowed magic methods (__get, __set, __call)
* or self::DISALLOW_MAGIC_METHODS for none
*/
public function __construct(/*int */$magicMethods = self::MAGIC_GET | self::MAGIC_SET, bool $throwExceptionOnInvalidIndex = false, CacheItemPoolInterface $cacheItemPool = null, bool $throwExceptionOnInvalidPropertyPath = true, PropertyReadInfoExtractorInterface $readInfoExtractor = null, PropertyWriteInfoExtractorInterface $writeInfoExtractor = null)
public function __construct(/*int */ $magicMethods = self::MAGIC_GET | self::MAGIC_SET, bool $throwExceptionOnInvalidIndex = false, CacheItemPoolInterface $cacheItemPool = null, bool $throwExceptionOnInvalidPropertyPath = true, PropertyReadInfoExtractorInterface $readInfoExtractor = null, PropertyWriteInfoExtractorInterface $writeInfoExtractor = null)
{
if (\is_bool($magicMethods)) {
trigger_deprecation('symfony/property-access', '5.2', 'Passing a boolean as the first argument to "%s()" is deprecated. Pass a combination of bitwise flags instead (i.e an integer).', __METHOD__);
Expand Down Expand Up @@ -612,6 +612,7 @@ private function getWriteInfo(string $class, string $property, $value): Property
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
'enable_adder_remover_extraction' => $useAdderAndRemover,
'value' => $value,
]);

if (isset($item)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,12 @@ public function getWriteInfo(string $class, string $property, array $context = [
}

$method = $reflClass->getMethod($methodName);
/** @var \ReflectionParameter $parameter */
$parameter = $method->getParameters()[0];
if (\array_key_exists('value', $context) && null === $context['value'] && !$parameter->allowsNull()) {
$errors[] = sprintf('The method "%s" in class "%s" was found but does not allow null.', $methodName, $class);
continue;
}

if (!\in_array($mutatorPrefix, $this->arrayMutatorPrefixes, true)) {
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $methodName, $this->getWriteVisiblityForMethod($method), $method->isStatic());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,56 @@ public function extractConstructorTypesProvider(): array
['ddd', null],
];
}

public function testGetWriteInfoForSettersWithNullableParameter()
{
$extractor = new ReflectionExtractor(['set']);
$writeInfo = $extractor->getWriteInfo(Dummy::class, 'optionalDate', [
'value' => new \DateTime(),
]);
$this->assertSame('method', $writeInfo->getType());
$this->assertEmpty($writeInfo->getErrors());

// If the key "value" exists and is null, we should still get the method
$writeInfo = $extractor->getWriteInfo(Dummy::class, 'optionalDate', [
'value' => null,
]);
$this->assertSame('method', $writeInfo->getType());
$this->assertEmpty($writeInfo->getErrors());

// If we dont pass a key "value", we should behave as if the value exists.
$writeInfo = $extractor->getWriteInfo(Dummy::class, 'optionalDate', []);
$this->assertSame('method', $writeInfo->getType());
$this->assertEmpty($writeInfo->getErrors());
}

public function testGetWriteInfoForSettersWithNonNullableParameter()
{
$extractor = new ReflectionExtractor(['set']);
$writeInfo = $extractor->getWriteInfo(Dummy::class, 'date', [
'value' => new \DateTime(),
]);
$this->assertSame('method', $writeInfo->getType());
$this->assertEmpty($writeInfo->getErrors());

// If the key "value" exists and is null, then make sure there is an error when parameter is required
$writeInfo = $extractor->getWriteInfo(Dummy::class, 'date', [
'value' => null,
]);
$this->assertSame('none', $writeInfo->getType());
$this->assertTrue(\in_array('The method "setDate" in class "Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy" was found but does not allow null.', $writeInfo->getErrors()));

// If we dont pass a key "value", we should behave as if the value exists.
$writeInfo = $extractor->getWriteInfo(Dummy::class, 'date', []);
$this->assertSame('method', $writeInfo->getType());
$this->assertEmpty($writeInfo->getErrors());
}

public function testGetWriteInfoForSettersWithNoParameter()
{
$extractor = new ReflectionExtractor(['add']);
$writeInfo = $extractor->getWriteInfo(Dummy::class, 'nothing');
$this->assertSame('none', $writeInfo->getType());
$this->assertNotEmpty($writeInfo->getErrors());
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,12 @@ public function addDate(\DateTime $date)
public function hasElement(string $element): bool
{
}

public function setOptionalDate(\DateTime $date = null)
{
}

public function addNothing()
{
}
}