Skip to content

Commit c46519b

Browse files
nykopolfabpot
authored andcommitted
[PropertyAccess][DX] Enhance exception that say that some methods are missing if they don't
1 parent a53aba3 commit c46519b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,17 @@ private function getWriteAccessInfo($class, $property, $value)
714714
// we call the getter and hope the __call do the job
715715
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
716716
$access[self::ACCESS_NAME] = $setter;
717+
} elseif (null !== $methods = $this->findAdderAndRemover($reflClass, $singulars)) {
718+
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
719+
$access[self::ACCESS_NAME] = sprintf(
720+
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
721+
'the new value must be an array or an instance of \Traversable, '.
722+
'"%s" given.',
723+
$property,
724+
$reflClass->name,
725+
implode('()", "', $methods),
726+
is_object($value) ? get_class($value) : gettype($value)
727+
);
717728
} else {
718729
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
719730
$access[self::ACCESS_NAME] = sprintf(

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,15 @@ public function testIsWritableReturnsFalseIfNoAdderNorRemoverExists()
194194

195195
$this->assertFalse($this->propertyAccessor->isWritable($car, 'axes', $axes));
196196
}
197+
198+
/**
199+
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
200+
* expectedExceptionMessageRegExp /The property "axes" in class "Mock_PropertyAccessorCollectionTest_Car[^"]*" can be defined with the methods "addAxis()", "removeAxis()" but the new value must be an array or an instance of \Traversable, "string" given./
201+
*/
202+
public function testSetValueFailsIfAdderAndRemoverExistButValueIsNotTraversable()
203+
{
204+
$car = $this->getMock(__CLASS__.'_Car');
205+
206+
$this->propertyAccessor->setValue($car, 'axes', 'Not an array or Traversable');
207+
}
197208
}

0 commit comments

Comments
 (0)