Skip to content

Commit

Permalink
Merge pull request #249 from trickeyone/1.6.x
Browse files Browse the repository at this point in the history
Enable strict comparison for arrays of scalar values (1.6.x)
  • Loading branch information
greg0ire authored Jul 27, 2020
2 parents 5f04703 + 4b1d1b5 commit 55f8b79
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
Expand All @@ -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'));
Expand Down

0 comments on commit 55f8b79

Please sign in to comment.