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
13 changes: 8 additions & 5 deletions src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
Copy link
Contributor Author

@staabm staabm Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this PR, any CallLike triggered autoloading of the PHPUnit classes via reflection (no matter whether it actually contained the assert prefix in the method name or no matter whether the CallLike was related to PHPUnit at all)

if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
return [];
}

if ($node->isFirstClassCallable()) {
if (count($node->getArgs()) < 2) {
return [];
}

if (count($node->getArgs()) < 2) {
if ($node->isFirstClassCallable()) {
return [];
}

if (
!$node->name instanceof Node\Identifier
|| !in_array(strtolower($node->name->name), ['assertequals', 'assertnotequals'], true)
) {
return [];
}

if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}

$leftType = TypeCombinator::removeNull($scope->getType($node->getArgs()[0]->value));
$rightType = TypeCombinator::removeNull($scope->getType($node->getArgs()[1]->value));

Expand Down
3 changes: 0 additions & 3 deletions src/Rules/PHPUnit/AssertRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
class AssertRuleHelper
{

/**
* @phpstan-assert-if-true Node\Expr\MethodCall|Node\Expr\StaticCall $node
*/
public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): bool
{
if ($node instanceof Node\Expr\MethodCall) {
Expand Down
12 changes: 7 additions & 5 deletions src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
return [];
}

if ($node->isFirstClassCallable()) {
if (count($node->getArgs()) < 2) {
return [];
}

if (count($node->getArgs()) < 2) {
if ($node->isFirstClassCallable()) {
return [];
}
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
Expand All @@ -43,6 +41,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}

if ($expectedArgumentValue->name->toLowerString() === 'true') {
return [
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(),
Expand Down
12 changes: 7 additions & 5 deletions src/Rules/PHPUnit/AssertSameNullExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
return [];
}

if ($node->isFirstClassCallable()) {
if (count($node->getArgs()) < 2) {
return [];
}

if (count($node->getArgs()) < 2) {
if ($node->isFirstClassCallable()) {
return [];
}
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
return [];
}

if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}

$expectedArgumentValue = $node->getArgs()[0]->value;
if (!($expectedArgumentValue instanceof ConstFetch)) {
return [];
Expand Down
13 changes: 7 additions & 6 deletions src/Rules/PHPUnit/AssertSameWithCountRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
if (!$node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
return [];
}
if (count($node->getArgs()) < 2) {
return [];
}

if ($node->isFirstClassCallable()) {
return [];
}

if (count($node->getArgs()) < 2) {
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
return [];
}
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {

if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
return [];
}

$right = $node->getArgs()[1]->value;

if (
$right instanceof Node\Expr\FuncCall
&& $right->name instanceof Node\Name
Expand Down
Loading