Skip to content

AssertTypeSpecifyingExtensionHelper: rootExpr with unknown variable to avoid always-true false positives #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ReflectionObject;
use function array_key_exists;
use function count;
use function in_array;
use function strlen;
use function strpos;
use function substr;
Expand All @@ -33,6 +34,12 @@ class AssertTypeSpecifyingExtensionHelper
/** @var Closure[] */
private static $resolvers;

/**
* Those can specify types correctly, but would produce always-true issue
* @var string[]
*/
private static $resolversCausingAlwaysTrue = ['ContainsOnlyInstancesOf', 'ContainsEquals', 'Contains'];

/**
* @param Arg[] $args
*/
Expand Down Expand Up @@ -87,10 +94,14 @@ public static function specifyTypes(
if ($expression === null) {
return new SpecifiedTypes([], []);
}

$bypassAlwaysTrueIssue = in_array(self::trimName($name), self::$resolversCausingAlwaysTrue, true);

return $typeSpecifier->specifyTypesInCondition(
$scope,
$expression,
TypeSpecifierContext::createTruthy()
TypeSpecifierContext::createTruthy(),
$bypassAlwaysTrueIssue ? new Expr\BinaryOp\BooleanAnd($expression, new Expr\Variable('nonsense')) : null
);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public function testOther()
$foo->assertSame();
}

public function testAssertContains()
{
$this->assertContains('not in the list', new \ArrayObject([1]));
$this->assertContainsEquals('not in the list', new \ArrayObject([1]));
$this->assertNotContains('not in the list', new \ArrayObject([1]));
}

public function testStaticMethodReturnWithSameTypeIsNotReported()
{
$this->assertSame(self::createSomething('foo'), self::createSomething('foo'));
Expand Down