Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 1 addition & 7 deletions src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ public function processNode(Node $node, Scope $scope): array
),
)->identifier('phpunit.assertEquals')
->fixNode($node, static function (CallLike $node) use ($correctName) {
if ($node instanceof Node\Expr\MethodCall) {
Copy link
Contributor Author

@staabm staabm Oct 30, 2025

Choose a reason for hiding this comment

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

since a recent change we have

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

at the very top of this rule, therefore we don't need to narrow here again anymore.

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

if ($node instanceof Node\Expr\StaticCall) {
$node->name = new Node\Identifier($correctName);
}
$node->name = new Node\Identifier($correctName);

return $node;
})
Expand Down
26 changes: 24 additions & 2 deletions src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,35 @@ public function processNode(Node $node, Scope $scope): array

if ($expectedArgumentValue->name->toLowerString() === 'true') {
return [
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(),
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')
->identifier('phpunit.assertTrue')
->fixNode($node, static function (CallLike $node) {
$node->name = new Node\Identifier('assertTrue');

$args = $node->getArgs();
unset($args[0]);
$node->args = $args;
Copy link
Member

Choose a reason for hiding this comment

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

Pretty sure you're breaking the args list contract here 😊

Copy link
Contributor Author

@staabm staabm Nov 2, 2025

Choose a reason for hiding this comment

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

good point.

what do you think about moving the new PHPParser stubs into the regular phpstan-src stubs/ directory and load them conditional via a new phpstan-src config parameter, so we can enable the more precise types in 1st party extension repos.

Copy link
Member

Choose a reason for hiding this comment

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

@staabm I'm not sure about it. I don't feel like adding a configuration option and documenting it is justified just as a temporary measure before your PR on PHP-Parser is merged.


return $node;
})
->build(),
];
}

if ($expectedArgumentValue->name->toLowerString() === 'false') {
return [
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->identifier('phpunit.assertFalse')->build(),
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')
->identifier('phpunit.assertFalse')
->fixNode($node, static function (CallLike $node) {
$node->name = new Node\Identifier('assertFalse');

$args = $node->getArgs();
unset($args[0]);
$node->args = $args;

return $node;
})
->build(),
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function testRule(): void
]);
}

public function testFix(): void
{
$this->fix(__DIR__ . '/data/assert-same-boolean-expected-fixable.php', __DIR__ . '/data/assert-same-boolean-expected-fixable.php.fixed');
}

/**
* @return string[]
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace AssertSameBooleanTestCaseFix;

class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
{
public function returnsBool(): bool
{
if (rand(0, 1)) {
return false;
}
return true;
}

public function doFoo(): void
{
$this->assertSame(true, $this->returnBool());
self::assertSame(false, $this->returnBool());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace AssertSameBooleanTestCaseFix;

class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
{
public function returnsBool(): bool
{
if (rand(0, 1)) {
return false;
}
return true;
}

public function doFoo(): void
{
$this->assertTrue($this->returnBool());
self::assertFalse($this->returnBool());
}

}
Loading