diff --git a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php index eb426a856..decd7b2df 100644 --- a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php +++ b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php @@ -28,8 +28,8 @@ class ClosureExpressionVisitor extends ExpressionVisitor { /** * Accesses the field of a given object. This field has to be public - * directly or indirectly (through an accessor get*, is*, or a magic - * method, __get, __call). + * directly or indirectly (through an accessor of the same name, get*, is*, + * or a magic method, __get, __call). * * @param object|array $object * @@ -41,6 +41,10 @@ public static function getObjectFieldValue($object, string $field) return $object[$field]; } + if (method_exists($object, $field)) { + return $object->$field(); + } + $accessors = ['get', 'is']; foreach ($accessors as $accessor) { diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index a05574a84..769bc40e5 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -40,6 +40,13 @@ public function testGetObjectFieldValueIsAccessorWithIsPrefix() : void self::assertTrue($this->visitor->getObjectFieldValue($object, 'isBaz')); } + public function testGetObjectFieldValueAccessorWithoutPrefix() : void + { + $object = new TestObject(1, 2, 3, true); + + self::assertTrue($this->visitor->getObjectFieldValue($object, 'quux')); + } + public function testGetObjectFieldValueIsAccessorCamelCase() : void { $object = new TestObjectNotCamelCase(1); @@ -279,18 +286,22 @@ class TestObject /** @var mixed */ private $qux; + /** @var mixed */ + private $quux; + /** * @param mixed $foo * @param mixed $bar * @param mixed $baz * @param mixed $qux */ - public function __construct($foo = null, $bar = null, $baz = null, $qux = null) + public function __construct($foo = null, $bar = null, $baz = null, $qux = null, $quux = null) { $this->foo = $foo; $this->bar = $bar; $this->baz = $baz; $this->qux = $qux; + $this->quux = $qux; } /** @@ -328,6 +339,14 @@ public function isBaz() { return $this->baz; } + + /** + * @return mixed + */ + public function quux() + { + return $this->quux; + } } class TestObjectNotCamelCase