Skip to content

Commit d7e466a

Browse files
committed
bug symfony#18732 [PropertyAccess][DX] Enhance exception that say that some methods are missing if they don't (nykopol)
This PR was squashed before being merged into the 2.7 branch (closes symfony#18732). Discussion ---------- [PropertyAccess][DX] Enhance exception that say that some methods are missing if they don't | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#18694 | License | MIT | Doc PR | When you try do define a manyToMany association but you don't give an array or \Traversable, the raised exception say that some methods are missing while they don't. This PR check if the adder and setter methods exists and if so, give a exception that pointing on the real problem. Commits ------- c46519b [PropertyAccess][DX] Enhance exception that say that some methods are missing if they don't
2 parents 347cd87 + c46519b commit d7e466a

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)