Skip to content
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
4 changes: 2 additions & 2 deletions tools/.phpstan/composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"require-dev": {
"phpstan/phpstan": "^2.3.0",
"phpstan/phpstan": "^2.3.1",
"phpstan/extension-installer": "^1.4.3",
"phpstan/phpstan-phpunit": "^2.1.0",
"phpstan/phpstan-phpunit": "^2.1.1",
"phpstan/phpstan-strict-rules": "^2.1.0",
"ergebnis/phpstan-rules": "^2.14.0"
},
Expand Down
22 changes: 11 additions & 11 deletions tools/.phpstan/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions tools/.phpstan/vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@
},
{
"name": "phpstan/phpstan",
"version": "2.3.0",
"version_normalized": "2.3.0.0",
"version": "2.3.1",
"version_normalized": "2.3.1.0",
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/b1f9890d9af53a25c58129e78f0aadd31f90d7f4",
"reference": "b1f9890d9af53a25c58129e78f0aadd31f90d7f4",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/6155239b488e95d26d1ebd95c6b4bb294d7f09db",
"reference": "6155239b488e95d26d1ebd95c6b4bb294d7f09db",
"shasum": ""
},
"require": {
Expand All @@ -202,7 +202,7 @@
"conflict": {
"phpstan/phpstan-shim": "*"
},
"time": "2026-10-06T06:15:36+00:00",
"time": "2026-10-08T21:09:11+00:00",
"bin": [
"phpstan",
"phpstan.phar"
Expand Down Expand Up @@ -255,17 +255,17 @@
},
{
"name": "phpstan/phpstan-phpunit",
"version": "2.1.0",
"version_normalized": "2.1.0.0",
"version": "2.1.1",
"version_normalized": "2.1.1.0",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan-phpunit.git",
"reference": "a31e9dea4b5858dcad4a8dddd97f3384a90d46d1"
"reference": "8d16b8478e523a47f8c02e83bce8981b5b5a3b03"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/a31e9dea4b5858dcad4a8dddd97f3384a90d46d1",
"reference": "a31e9dea4b5858dcad4a8dddd97f3384a90d46d1",
"url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/8d16b8478e523a47f8c02e83bce8981b5b5a3b03",
"reference": "8d16b8478e523a47f8c02e83bce8981b5b5a3b03",
"shasum": ""
},
"require": {
Expand All @@ -284,7 +284,7 @@
"phpunit/phpunit": "^9.6",
"shipmonk/name-collision-detector": "^2.1"
},
"time": "2026-10-06T07:32:40+00:00",
"time": "2026-10-06T18:12:32+00:00",
"type": "phpstan-extension",
"extra": {
"phpstan": {
Expand All @@ -310,7 +310,7 @@
],
"support": {
"issues": "https://github.com/phpstan/phpstan-phpunit/issues",
"source": "https://github.com/phpstan/phpstan-phpunit/tree/2.1.0"
"source": "https://github.com/phpstan/phpstan-phpunit/tree/2.1.1"
},
"install-path": "../phpstan/phpstan-phpunit"
},
Expand Down
16 changes: 8 additions & 8 deletions tools/.phpstan/vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '846cfc101596f96307196597c2f1898c444db22f',
'reference' => '82ec82e0eccc363c18da38370e705984c9fc9253',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '846cfc101596f96307196597c2f1898c444db22f',
'reference' => '82ec82e0eccc363c18da38370e705984c9fc9253',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down Expand Up @@ -47,18 +47,18 @@
'dev_requirement' => true,
),
'phpstan/phpstan' => array(
'pretty_version' => '2.3.0',
'version' => '2.3.0.0',
'reference' => 'b1f9890d9af53a25c58129e78f0aadd31f90d7f4',
'pretty_version' => '2.3.1',
'version' => '2.3.1.0',
'reference' => '6155239b488e95d26d1ebd95c6b4bb294d7f09db',
'type' => 'library',
'install_path' => __DIR__ . '/../phpstan/phpstan',
'aliases' => array(),
'dev_requirement' => true,
),
'phpstan/phpstan-phpunit' => array(
'pretty_version' => '2.1.0',
'version' => '2.1.0.0',
'reference' => 'a31e9dea4b5858dcad4a8dddd97f3384a90d46d1',
'pretty_version' => '2.1.1',
'version' => '2.1.1.0',
'reference' => '8d16b8478e523a47f8c02e83bce8981b5b5a3b03',
'type' => 'phpstan-extension',
'install_path' => __DIR__ . '/../phpstan/phpstan-phpunit',
'aliases' => array(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

namespace PHPStan\Rules\PHPUnit;

use Countable;
use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use function array_merge;
use function count;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -43,11 +50,83 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$errorBuilder = RuleErrorBuilder::message(sprintf('%s() is not allowed. Use more strict assertion.', $node->name->toString()))
->identifier('phpunit.assertEmpty');

if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) {
return [$errorBuilder->build()];
}

$replacement = $this->getReplacement($scope->getNativeType($node->getArgs()[0]->value), $node->name->toLowerString() === 'assertnotempty');
if ($replacement !== null) {
[$correctName, $expectedValue] = $replacement;
$errorBuilder->fixNode($node, static function (CallLike $node) use ($correctName, $expectedValue) {
$node->name = new Identifier($correctName);
if ($expectedValue !== null) {
$node->args = array_merge([new Node\Arg($expectedValue)], $node->args);
}

return $node;
});
}

return [
RuleErrorBuilder::message(sprintf('%s() is not allowed. Use more strict assertion.', $node->name->toString()))
->identifier('phpunit.assertEmpty')
->build(),
$errorBuilder->build(),
];
}

/**
* @return array{string, Node\Expr|null}|null
*/
private function getReplacement(Type $type, bool $negated): ?array
{
if ($type instanceof UnionType) {
if (TypeCombinator::containsNull($type)) {
$typeWithoutNull = TypeCombinator::removeNull($type);

$classReflections = $typeWithoutNull->getObjectClassReflections();
if (count($classReflections) === 0) {
return null;
}
foreach ($classReflections as $classReflection) {
if (
$classReflection->isBuiltin()
|| !$classReflection->isFinalByKeyword()
|| $classReflection->implementsInterface(Countable::class)
) {
return null;
}

$parentClass = $classReflection->getParentClass();
while ($parentClass !== null) {
// Builtin parents can define different empty() semantics.
if ($parentClass->isBuiltin()) {
return null;
}
$parentClass = $parentClass->getParentClass();
}
}

return [$negated ? 'assertNotNull' : 'assertNull', null];
}

return null;
}

if ($type->isBoolean()->yes()) {
return [$negated ? 'assertTrue' : 'assertFalse', null];
}
if ($type->isArray()->yes()) {
return [$negated ? 'assertNotCount' : 'assertCount', new Int_(0)];
}
if ($type->isInteger()->yes()) {
return [$negated ? 'assertNotSame' : 'assertSame', new Int_(0)];
}
if ($type->isNonFalsyString()->yes()) {
return [$negated ? 'assertNotSame' : 'assertSame', new String_('')];
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function processNode(Node $node, Scope $scope): array
),
)->identifier('phpunit.assertEquals')
->fixNode($node, static function (CallLike $node) use ($correctName) {
if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) {
return $node;
}

$node->name = new Node\Identifier($correctName);

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\PHPUnit;

use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use function in_array;
Expand Down Expand Up @@ -46,4 +47,16 @@ public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): b
return $testCaseType->isSuperTypeOf($calledOnType)->yes();
}

public static function hasNamedOrUnpackedArguments(CallLike $call): bool
{
foreach ($call->getArgs() as $arg) {
// PHPUnit does not support named arguments for most of its APIs, e.g. assert*.
if ($arg->name !== null || $arg->unpack) {
return true;
}
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')
->identifier('phpunit.assertTrue')
->fixNode($node, static function (CallLike $node) {
if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) {
return $node;
}

$node->name = new Node\Identifier('assertTrue');
$node->args = self::rewriteArgs($node->args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message('You should use assertNull() instead of assertSame(null, $actual).')
->identifier('phpunit.assertNull')
->fixNode($node, static function (CallLike $node) {
if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) {
return $node;
}

$node->name = new Node\Identifier('assertNull');
$node->args = self::rewriteArgs($node->args);

Expand Down
Loading
Loading