From 4b1d1b57445b28362744448e8ded24474ba00e26 Mon Sep 17 00:00:00 2001 From: trickeyone Date: Thu, 18 Oct 2018 17:36:41 -0700 Subject: [PATCH] Restore strict comparison for arrays of scalar values Switching to strict checking in #97 was a minor BC-break, which can be alleviated by doing it only for scalar values. As a consequence, 2 objects that are equal but not the same will pass the check. Fixes #113 --- .../Expr/ClosureExpressionVisitor.php | 9 ++++++-- .../ClosureExpressionVisitorTest.php | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php index 43e5cf106..90ec79496 100644 --- a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php +++ b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php @@ -7,6 +7,7 @@ use RuntimeException; use function in_array; use function is_array; +use function is_scalar; use function iterator_to_array; use function method_exists; use function preg_match; @@ -155,12 +156,16 @@ public function walkComparison(Comparison $comparison) case Comparison::IN: return static function ($object) use ($field, $value) : bool { - return in_array(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value, true); + $fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field); + + return in_array($fieldValue, $value, is_scalar($fieldValue)); }; case Comparison::NIN: return static function ($object) use ($field, $value) : bool { - return ! in_array(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value, true); + $fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field); + + return ! in_array($fieldValue, $value, is_scalar($fieldValue)); }; case Comparison::CONTAINS: diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index ab4e2d2be..e04f5936b 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -143,6 +143,17 @@ public function testWalkInComparison() : void self::assertTrue($closure(new TestObject('04'))); } + public function testWalkInComparisonObjects() : void + { + $closure = $this->visitor->walkComparison($this->builder->in('foo', [new TestObject(1), new TestObject(2), new TestObject(4)])); + + self::assertTrue($closure(new TestObject(new TestObject(2)))); + self::assertTrue($closure(new TestObject(new TestObject(1)))); + self::assertFalse($closure(new TestObject(new TestObject(0)))); + self::assertTrue($closure(new TestObject(new TestObject(4)))); + self::assertFalse($closure(new TestObject(new TestObject('baz')))); + } + public function testWalkNotInComparison() : void { $closure = $this->visitor->walkComparison($this->builder->notIn('foo', [1, 2, 3, '04'])); @@ -154,6 +165,17 @@ public function testWalkNotInComparison() : void self::assertFalse($closure(new TestObject('04'))); } + public function testWalkNotInComparisonObjects() : void + { + $closure = $this->visitor->walkComparison($this->builder->notIn('foo', [new TestObject(1), new TestObject(2), new TestObject(4)])); + + self::assertFalse($closure(new TestObject(new TestObject(1)))); + self::assertFalse($closure(new TestObject(new TestObject(2)))); + self::assertTrue($closure(new TestObject(new TestObject(0)))); + self::assertFalse($closure(new TestObject(new TestObject(4)))); + self::assertTrue($closure(new TestObject(new TestObject('baz')))); + } + public function testWalkContainsComparison() : void { $closure = $this->visitor->walkComparison($this->builder->contains('foo', 'hello'));