-
Notifications
You must be signed in to change notification settings - Fork 60
[PHPUnit12] Add AssertIsTypeMethodCallRector
#508
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
Open
mttsch
wants to merge
2
commits into
rectorphp:main
Choose a base branch
from
mttsch:feature/120-Assert-isType
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...it120/Rector/MethodCall/AssertIsTypeMethodCallRector/AssertIsTypeMethodCallRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AssertIsTypeMethodCallRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...s-tests/PHPUnit120/Rector/MethodCall/AssertIsTypeMethodCallRector/Fixture/fixture.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class Fixture extends TestCase | ||
{ | ||
public function testMethod(): void | ||
{ | ||
$this->assertThat([], $this->isType('array')); | ||
$this->assertThat(true, $this->isType('bool')); | ||
$this->assertThat(true, $this->isType('boolean')); | ||
$this->assertThat(fn () => 0, $this->isType('callable')); | ||
$this->assertThat(1.0, $this->isType('float')); | ||
$this->assertThat(1.0, $this->isType('double')); | ||
$this->assertThat(1, $this->isType('int')); | ||
$this->assertThat(1, $this->isType('integer')); | ||
$this->assertThat([], $this->isType('iterable')); | ||
$this->assertThat(null, $this->isType('null')); | ||
$this->assertThat(12, $this->isType('numeric')); | ||
$this->assertThat(new \stdClass(), $this->isType('object')); | ||
$this->assertThat(1.0, $this->isType('real')); | ||
$this->assertThat($resource, $this->isType('resource')); | ||
$this->assertThat($closedResource, $this->isType('resource (closed)')); | ||
$this->assertThat('', $this->isType('scalar')); | ||
$this->assertThat('', $this->isType('string')); | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class Fixture extends TestCase | ||
{ | ||
public function testMethod(): void | ||
{ | ||
$this->assertThat([], $this->isArray()); | ||
$this->assertThat(true, $this->isBool()); | ||
$this->assertThat(true, $this->isBool()); | ||
$this->assertThat(fn () => 0, $this->isCallable()); | ||
$this->assertThat(1.0, $this->isFloat()); | ||
$this->assertThat(1.0, $this->isFloat()); | ||
$this->assertThat(1, $this->isInt()); | ||
$this->assertThat(1, $this->isInt()); | ||
$this->assertThat([], $this->isIterable()); | ||
$this->assertThat(null, $this->isNull()); | ||
$this->assertThat(12, $this->isNumeric()); | ||
$this->assertThat(new \stdClass(), $this->isObject()); | ||
$this->assertThat(1.0, $this->isFloat()); | ||
$this->assertThat($resource, $this->isResource()); | ||
$this->assertThat($closedResource, $this->isClosedResource()); | ||
$this->assertThat('', $this->isScalar()); | ||
$this->assertThat('', $this->isString()); | ||
} | ||
} | ||
?> |
61 changes: 61 additions & 0 deletions
61
.../PHPUnit120/Rector/MethodCall/AssertIsTypeMethodCallRector/Fixture/static_fixture.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class StaticFixture extends TestCase | ||
{ | ||
public function testMethod(): void | ||
{ | ||
$this->assertThat([], self::isType('array')); | ||
$this->assertThat(true, self::isType('bool')); | ||
$this->assertThat(true, self::isType('boolean')); | ||
$this->assertThat(fn () => 0, self::isType('callable')); | ||
$this->assertThat(1.0, self::isType('float')); | ||
$this->assertThat(1.0, self::isType('double')); | ||
$this->assertThat(1, self::isType('int')); | ||
$this->assertThat(1, self::isType('integer')); | ||
$this->assertThat([], self::isType('iterable')); | ||
$this->assertThat(null, self::isType('null')); | ||
$this->assertThat(12, self::isType('numeric')); | ||
$this->assertThat(new \stdClass(), self::isType('object')); | ||
$this->assertThat(1.0, self::isType('real')); | ||
$this->assertThat($resource, self::isType('resource')); | ||
$this->assertThat($closedResource, self::isType('resource (closed)')); | ||
$this->assertThat('', self::isType('scalar')); | ||
$this->assertThat('', self::isType('string')); | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class StaticFixture extends TestCase | ||
{ | ||
public function testMethod(): void | ||
{ | ||
$this->assertThat([], self::isArray()); | ||
$this->assertThat(true, self::isBool()); | ||
$this->assertThat(true, self::isBool()); | ||
$this->assertThat(fn () => 0, self::isCallable()); | ||
$this->assertThat(1.0, self::isFloat()); | ||
$this->assertThat(1.0, self::isFloat()); | ||
$this->assertThat(1, self::isInt()); | ||
$this->assertThat(1, self::isInt()); | ||
$this->assertThat([], self::isIterable()); | ||
$this->assertThat(null, self::isNull()); | ||
$this->assertThat(12, self::isNumeric()); | ||
$this->assertThat(new \stdClass(), self::isObject()); | ||
$this->assertThat(1.0, self::isFloat()); | ||
$this->assertThat($resource, self::isResource()); | ||
$this->assertThat($closedResource, self::isClosedResource()); | ||
$this->assertThat('', self::isScalar()); | ||
$this->assertThat('', self::isString()); | ||
} | ||
} | ||
?> |
10 changes: 10 additions & 0 deletions
10
...ests/PHPUnit120/Rector/MethodCall/AssertIsTypeMethodCallRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(AssertIsTypeMethodCallRector::class); | ||
}; |
127 changes: 127 additions & 0 deletions
127
rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\PHPUnit120\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use Rector\PhpParser\Node\Value\ValueResolver; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/6053 | ||
* @see https://github.com/sebastianbergmann/phpunit/blob/12.0.0/ChangeLog-12.0.md | ||
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\AssertIsTypeMethodCallRectorTest | ||
*/ | ||
final class AssertIsTypeMethodCallRector extends AbstractRector | ||
{ | ||
private const IS_TYPE_VALUE_TO_METHOD = [ | ||
'array' => 'isArray', | ||
'bool' => 'isBool', | ||
'boolean' => 'isBool', | ||
'callable' => 'isCallable', | ||
'double' => 'isFloat', | ||
'float' => 'isFloat', | ||
'integer' => 'isInt', | ||
'int' => 'isInt', | ||
'iterable' => 'isIterable', | ||
'null' => 'isNull', | ||
'numeric' => 'isNumeric', | ||
'object' => 'isObject', | ||
'real' => 'isFloat', | ||
'resource' => 'isResource', | ||
'resource (closed)' => 'isClosedResource', | ||
'scalar' => 'isScalar', | ||
'string' => 'isString', | ||
]; | ||
|
||
public function __construct( | ||
private readonly ValueResolver $valueResolver, | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Replaces `Assert::isType()` calls with type-specific `Assert::is*()` calls', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeClass extends TestCase | ||
{ | ||
public function testMethod(): void | ||
{ | ||
$this->assertThat([], $this->isType('array')); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeClass extends TestCase | ||
{ | ||
public function testMethod(): void | ||
{ | ||
$this->assertThat([], $this->isArray()); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
), | ||
], | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): Node|null | ||
{ | ||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if (! $this->testsNodeAnalyzer->isPHPUnitTestCaseCall($node) || ! $this->isName($node->name, 'isType')) { | ||
return null; | ||
} | ||
|
||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (count($node->getArgs()) !== 1) { | ||
return null; | ||
} | ||
|
||
$arg = $node->getArg('type', 0); | ||
if (! $arg instanceof Arg) { | ||
return null; | ||
} | ||
|
||
$argValue = $this->valueResolver->getValue($arg); | ||
if (isset(self::IS_TYPE_VALUE_TO_METHOD[$argValue])) { | ||
if ($node instanceof MethodCall) { | ||
return new MethodCall($node->var, self::IS_TYPE_VALUE_TO_METHOD[$argValue]); | ||
} | ||
|
||
return new StaticCall($node->class, self::IS_TYPE_VALUE_TO_METHOD[$argValue]); | ||
|
||
} | ||
|
||
return null; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wonder if we can have another rule that would simply use exact assert method:
E.g. in phpunit code quality set. Much more readable and direct.
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.
I get your point but for this specific rector rule fixture, it makes sense to keep it like this to keep the fixture code somewhat "useful" instead of just calling the relevant methods and doing nothing with the return type.
Also see PHPUnit's test cases for these methods which are the exact same https://github.com/sebastianbergmann/phpunit/blob/a561bbf7dd850c1bef4f8db7230ffa68ca6797af/tests/unit/Framework/Assert/assertThatTest.php#L423-L426
But I guess you are talking more about a different rule from this one.
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.
Yes, this is job for next Rector rule :)