diff --git a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php index cd99e2f10..81b8ca5d1 100644 --- a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php +++ b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php @@ -78,6 +78,10 @@ public static function getObjectFieldValue($object, $field) return $object->$accessor(); } + if (method_exists($object, $field)) { + return $object->$field(); + } + return $object->$field; } diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index eff227d4d..162dc664a 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -248,6 +248,18 @@ public function testSortDelegate() : void self::assertEquals('c', $objects[2]->getBar()); } + public function testSortByLiteralPublicAccessor() + { + $objects = [new TestObjectWithLiteralPublicAccessor('a'), new TestObjectWithLiteralPublicAccessor('b'), new TestObjectWithLiteralPublicAccessor('c')]; + $sort = ClosureExpressionVisitor::sortByField('foo', 1); + + usort($objects, $sort); + + self::assertEquals('a', $objects[0]->foo()); + self::assertEquals('b', $objects[1]->foo()); + self::assertEquals('c', $objects[2]->foo()); + } + public function testArrayComparison() : void { $closure = $this->visitor->walkComparison($this->builder->eq('foo', 42)); @@ -357,6 +369,22 @@ public function getFooBar() : ?int } } +class TestObjectWithLiteralPublicAccessor +{ + /** @var mixed */ + private $foo; + + public function __construct($foo = null) + { + $this->foo = $foo; + } + + public function foo() + { + return $this->foo; + } +} + class TestObjectBothPublic { /** @var mixed */