Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70ecacc

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

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎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
use ReflectionObject;
2424
use function array_key_exists;
2525
use function count;
26+
use function in_array;
2627
use function strlen;
2728
use function strpos;
2829
use function substr;
@@ -33,6 +34,12 @@ class AssertTypeSpecifyingExtensionHelper
3334
/** @var Closure[] */
3435
private static $resolvers;
3536

37+
/**
38+
* Those can specify types correctly, but would produce always-true issue
39+
* @var string[]
40+
*/
41+
private static $resolversCausingAlwaysTrue = ['ContainsOnlyInstancesOf', 'ContainsEquals', 'Contains'];
42+
3643
/**
3744
* @param Arg[] $args
3845
*/
@@ -87,10 +94,14 @@ public static function specifyTypes(
8794
if ($expression === null) {
8895
return new SpecifiedTypes([], []);
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 ? new Expr\BinaryOp\BooleanAnd($expression, new Expr\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+
public function testAssertContains()
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
public function testStaticMethodReturnWithSameTypeIsNotReported()
6976
{
7077
$this->assertSame(self::createSomething('foo'), self::createSomething('foo'));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /