Skip to content

Commit 8b66cfc

Browse files
Merge branch '2.8' into 3.4
* 2.8: fix data mapper return type in docblock fix type error handling when writing values
2 parents 8860b66 + e18bf6e commit 8b66cfc

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/Symfony/Component/Form/FormConfigInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function getModelTransformers();
9999
/**
100100
* Returns the data mapper of the form.
101101
*
102-
* @return DataMapperInterface The data mapper
102+
* @return DataMapperInterface|null The data mapper
103103
*/
104104
public function getDataMapper();
105105

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

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

257257
private static function throwInvalidArgumentException($message, $trace, $i)
258258
{
259-
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file'] && isset($trace[$i]['args'][0])) {
259+
// the type mismatch is not caused by invalid arguments (but e.g. by an incompatible return type hint of the writer method)
260+
if (0 !== strpos($message, 'Argument ')) {
261+
return;
262+
}
263+
264+
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file'] && array_key_exists(0, $trace[$i]['args'])) {
260265
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
261266
$pos += \strlen($delim);
262267
$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
@@ -540,6 +540,17 @@ public function testThrowTypeError()
540540
$this->propertyAccessor->setValue($object, 'date', 'This is a string, \DateTime expected.');
541541
}
542542

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

664675
$this->propertyAccessor->setValue($object, 'foos', array(new \DateTime()));
665676
}
677+
678+
/**
679+
* @requires PHP 7
680+
*
681+
* @expectedException \TypeError
682+
*/
683+
public function testDoNotDiscardReturnTypeErrorWhenWriterMethodIsMisconfigured()
684+
{
685+
$object = new ReturnTyped();
686+
687+
$this->propertyAccessor->setValue($object, 'name', 'foo');
688+
}
666689
}

0 commit comments

Comments
 (0)