Skip to content

Commit 887abad

Browse files
authored
improve direct assert rule (#628)
1 parent 16c787f commit 887abad

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

rules-tests/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector/Fixture/that_assert_true.php.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
99
public function test()
1010
{
1111
$this->assertThat('value', $this->isTrue());
12+
13+
$this->assertThat('name', $this->isType('string'));
14+
$this->assertThat('age', $this->isString());
1215
}
1316
}
1417

@@ -25,6 +28,9 @@ final class ThatAssertTrue extends \PHPUnit\Framework\TestCase
2528
public function test()
2629
{
2730
$this->assertTrue('value');
31+
32+
$this->assertIsString('name');
33+
$this->assertIsString('age');
2834
}
2935
}
3036

rules/CodeQuality/Rector/MethodCall/AssertThatToDirectAssertRector.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
99
use PhpParser\Node\Identifier;
10+
use Rector\PhpParser\Node\Value\ValueResolver;
1011
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1112
use Rector\Rector\AbstractRector;
1213
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -17,8 +18,25 @@
1718
*/
1819
final class AssertThatToDirectAssertRector extends AbstractRector
1920
{
21+
/**
22+
* @var array<string, string>
23+
*/
24+
private const array IS_TO_ASSERT_METHOD_MAP = [
25+
'isTrue' => 'assertTrue',
26+
'isFalse' => 'assertFalse',
27+
'isNull' => 'assertNull',
28+
'isEmpty' => 'assertEmpty',
29+
'isCountable' => 'assertIsCountable',
30+
'isArray' => 'assertIsArray',
31+
'isString' => 'assertIsString',
32+
'isInt' => 'assertIsInt',
33+
'isFloat' => 'assertIsFloat',
34+
'isBool' => 'assertIsBool',
35+
];
36+
2037
public function __construct(
2138
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
39+
private readonly ValueResolver $valueResolver,
2240
) {
2341
}
2442

@@ -95,8 +113,25 @@ public function refactor(Node $node): ?Node
95113
return $node;
96114
}
97115

98-
if ($exactAssertName === 'isTrue') {
99-
$node->name = new Identifier('assertTrue');
116+
foreach (self::IS_TO_ASSERT_METHOD_MAP as $isName => $assertName) {
117+
if (! $this->isName($exactAssertMethodCall->name, $isName)) {
118+
continue;
119+
}
120+
121+
$node->name = new Identifier($assertName);
122+
unset($node->args[1]);
123+
124+
return $node;
125+
}
126+
127+
if ($this->isName($exactAssertMethodCall->name, 'isType')) {
128+
$exactFirstArg = $exactAssertMethodCall->getArgs()[0];
129+
$expectedType = $this->valueResolver->getValue($exactFirstArg->value);
130+
if (! is_string($expectedType)) {
131+
return null;
132+
}
133+
134+
$node->name = new Identifier('assertIs' . ucfirst($expectedType));
100135
unset($node->args[1]);
101136

102137
return $node;

0 commit comments

Comments
 (0)