Skip to content

Commit 70ecacc

Browse files
janedbalondrejmirtes
authored andcommitted
AssertTypeSpecifyingExtensionHelper: rootExpr with unknown variable to avoid always-true false positives (#197)
1 parent 614acc1 commit 70ecacc

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
useReflectionObject;
2424
usefunctionarray_key_exists;
2525
usefunctioncount;
26+
usefunctionin_array;
2627
usefunctionstrlen;
2728
usefunctionstrpos;
2829
usefunctionsubstr;
@@ -33,6 +34,12 @@ class AssertTypeSpecifyingExtensionHelper
3334
/** @var Closure[] */
3435
privatestatic$resolvers;
3536

37+
/**
38+
* Those can specify types correctly, but would produce always-true issue
39+
* @var string[]
40+
*/
41+
privatestatic$resolversCausingAlwaysTrue = ['ContainsOnlyInstancesOf', 'ContainsEquals', 'Contains'];
42+
3643
/**
3744
* @param Arg[] $args
3845
*/
@@ -87,10 +94,14 @@ public static function specifyTypes(
8794
if ($expression === null) {
8895
returnnewSpecifiedTypes([], []);
8996
}
97+
98+
$bypassAlwaysTrueIssue = in_array(self::trimName($name), self::$resolversCausingAlwaysTrue, true);
99+
90100
return$typeSpecifier->specifyTypesInCondition(
91101
$scope,
92102
$expression,
93-
TypeSpecifierContext::createTruthy()
103+
TypeSpecifierContext::createTruthy(),
104+
$bypassAlwaysTrueIssue ? newExpr\BinaryOp\BooleanAnd($expression, newExpr\Variable('nonsense')) : null
94105
);
95106
}
96107

‎tests/Rules/PHPUnit/data/assert-same.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public function testOther()
6565
$foo->assertSame();
6666
}
6767

68+
publicfunctiontestAssertContains()
69+
{
70+
$this->assertContains('not in the list', new \ArrayObject([1]));
71+
$this->assertContainsEquals('not in the list', new \ArrayObject([1]));
72+
$this->assertNotContains('not in the list', new \ArrayObject([1]));
73+
}
74+
6875
publicfunctiontestStaticMethodReturnWithSameTypeIsNotReported()
6976
{
7077
$this->assertSame(self::createSomething('foo'), self::createSomething('foo'));

0 commit comments

Comments
 (0)