-
Notifications
You must be signed in to change notification settings - Fork 56
Make AssertSameBooleanExpectedRule auto-fixable #252
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure you're breaking the args list contract here 😊
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
|
staabm marked this conversation as resolved.
Outdated
|
||
| $node->args = $args; | ||
|
|
||
| return $node; | ||
| }) | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
| 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()); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
at the very top of this rule, therefore we don't need to narrow here again anymore.