Skip to content
Open
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
3 changes: 2 additions & 1 deletion config/sets/phpunit120.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveOverrideFinalConstructTestCaseRector::class);
$rectorConfig->rules([RemoveOverrideFinalConstructTestCaseRector::class, AssertIsTypeMethodCallRector::class]);
};
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';
}
}
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'));
Copy link
Member

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:

-$this->assertThat($value, $this->isType('null'));
+$this->assertNull($value);

E.g. in phpunit code quality set. Much more readable and direct.

Copy link
Contributor Author

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.

Copy link
Member

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 :)

$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());
}
}
?>
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());
}
}
?>
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 rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php
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;
}

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;
}
}