Skip to content

Commit e18bf6e

Browse files
bug #28220 [PropertyAccess] fix type error handling when writing values (xabbuh)
This PR was merged into the 2.8 branch. Discussion ---------- [PropertyAccess] fix type error handling when writing values | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Commits ------- 4575451 fix type error handling when writing values
2 parents 0332f86 + 4575451 commit e18bf6e

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,12 @@ public static function handleError($type, $message, $file, $line, $context)
231231

232232
private static function throwInvalidArgumentException($message, $trace, $i)
233233
{
234-
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file'] && isset($trace[$i]['args'][0])) {
234+
// the type mismatch is not caused by invalid arguments (but e.g. by an incompatible return type hint of the writer method)
235+
if (0 !== strpos($message, 'Argument ')) {
236+
return;
237+
}
238+
239+
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file'] && array_key_exists(0, $trace[$i]['args'])) {
235240
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
236241
$pos += \strlen($delim);
237242
$type = $trace[$i]['args'][0];

src/Symfony/Component/PropertyAccess/Tests/Fixtures/ReturnTyped.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public function addFoo(\DateTime $dateTime)
2828
public function removeFoo(\DateTime $dateTime)
2929
{
3030
}
31+
32+
public function setName($name): self
33+
{
34+
return 'This does not respect the return type on purpose.';
35+
}
3136
}

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,17 @@ public function testThrowTypeError()
538538
$this->propertyAccessor->setValue($object, 'date', 'This is a string, \DateTime expected.');
539539
}
540540

541+
/**
542+
* @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
543+
* @expectedExceptionMessage Expected argument of type "DateTime", "NULL" given
544+
*/
545+
public function testThrowTypeErrorWithNullArgument()
546+
{
547+
$object = new TypeHinted();
548+
549+
$this->propertyAccessor->setValue($object, 'date', null);
550+
}
551+
541552
public function testSetTypeHint()
542553
{
543554
$date = new \DateTime();
@@ -579,4 +590,16 @@ public function testDoNotDiscardReturnTypeError()
579590

580591
$this->propertyAccessor->setValue($object, 'foos', array(new \DateTime()));
581592
}
593+
594+
/**
595+
* @requires PHP 7
596+
*
597+
* @expectedException \TypeError
598+
*/
599+
public function testDoNotDiscardReturnTypeErrorWhenWriterMethodIsMisconfigured()
600+
{
601+
$object = new ReturnTyped();
602+
603+
$this->propertyAccessor->setValue($object, 'name', 'foo');
604+
}
582605
}

0 commit comments

Comments
 (0)