Skip to content

Commit c61efd3

Browse files
authored
[code-quality] add exception support to PreferPHPUnitThisCallRector (#477)
* bump couple deprecations * add exception support to PreferPHPUnitThisCallRector
1 parent 6f67935 commit c61efd3

File tree

9 files changed

+71
-19
lines changed

9 files changed

+71
-19
lines changed

composer.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
"php": ">=8.2"
88
},
99
"require-dev": {
10-
"rector/rector-src": "dev-main",
11-
"phpunit/phpunit": "^11.5",
12-
"phpstan/phpstan": "^2.1.8",
1310
"phpecs/phpecs": "^2.0",
1411
"phpstan/extension-installer": "^1.4",
12+
"phpstan/phpstan": "^2.1.8",
13+
"phpstan/phpstan-deprecation-rules": "^2.0",
14+
"phpstan/phpstan-webmozart-assert": "^2.0",
15+
"phpunit/phpunit": "^11.5",
16+
"rector/rector-src": "dev-main",
17+
"rector/swiss-knife": "^1.0",
18+
"rector/type-perfect": "^2.0",
19+
"symplify/phpstan-extensions": "^12.0",
1520
"symplify/vendor-patches": "^11.4",
16-
"tracy/tracy": "^2.10",
1721
"tomasvotruba/class-leak": "^1.2",
18-
"rector/swiss-knife": "^1.0",
19-
"phpstan/phpstan-webmozart-assert": "^2.0",
20-
"rector/type-perfect": "^2.0"
22+
"tracy/tracy": "^2.10"
2123
},
2224
"autoload": {
2325
"psr-4": {

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ parameters:
99
- rules-tests
1010

1111
reportUnmatchedIgnoredErrors: false
12+
errorFormat: symplify
1213

1314
scanDirectories:
1415
- stubs
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ExpectException extends TestCase
8+
{
9+
public function test()
10+
{
11+
self::expectException('some class');
12+
self::expectExceptionMessage('some message');
13+
14+
self::expectExceptionCode(100);
15+
self::expectExceptionMessageMatches('some regex');
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector\Fixture;
24+
25+
use PHPUnit\Framework\TestCase;
26+
27+
final class ExpectException extends TestCase
28+
{
29+
public function test()
30+
{
31+
$this->expectException('some class');
32+
$this->expectExceptionMessage('some message');
33+
34+
$this->expectExceptionCode(100);
35+
$this->expectExceptionMessageMatches('some regex');
36+
}
37+
}
38+
39+
?>

rules/AnnotationsToAttributes/Rector/ClassMethod/TestWithAnnotationToAttributeRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function refactor(Node $node): ?Node
137137
$testWithItems = explode("\n", trim($genericTagValueNode->value));
138138

139139
foreach ($testWithItems as $testWithItem) {
140-
$jsonArray = Json::decode(trim($testWithItem), Json::FORCE_ARRAY);
140+
$jsonArray = Json::decode(trim($testWithItem), forceArrays: true);
141141

142142
$attributeGroups[] = $this->phpAttributeGroupFactory->createFromClassWithItems(
143143
self::TEST_WITH_ATTRIBUTE,

rules/CodeQuality/Rector/ClassMethod/RemoveEmptyTestMethodRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\ClassMethod;
9-
use PhpParser\NodeTraverser;
9+
use PhpParser\NodeVisitor;
1010
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1111
use Rector\Rector\AbstractRector;
1212
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -78,6 +78,6 @@ public function refactor(Node $node): ?int
7878
return null;
7979
}
8080

81-
return NodeTraverser::REMOVE_NODE;
81+
return NodeVisitor::REMOVE_NODE;
8282
}
8383
}

rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PhpParser\Node\Stmt\Class_;
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PhpParser\Node\Stmt\Expression;
16-
use PhpParser\NodeTraverser;
16+
use PhpParser\NodeVisitor;
1717
use PHPStan\Reflection\ClassReflection;
1818
use Rector\NodeAnalyzer\ClassAnalyzer;
1919
use Rector\NodeTypeResolver\Node\AttributeKey;
@@ -160,12 +160,12 @@ private function shouldSkip(Class_ $class, ClassMethod $classMethod): bool
160160
(array) $classMethod->stmts,
161161
function (Node $subNode) use ($paramNames, &$isFoundParamUsed): ?int {
162162
if ($subNode instanceof StaticCall && $this->isName($subNode->name, MethodName::CONSTRUCT)) {
163-
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
163+
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
164164
}
165165

166166
if ($subNode instanceof Variable && $this->isNames($subNode, $paramNames)) {
167167
$isFoundParamUsed = true;
168-
return NodeTraverser::STOP_TRAVERSAL;
168+
return NodeVisitor::STOP_TRAVERSAL;
169169
}
170170

171171
return null;

rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PhpParser\Node\Expr\StaticCall;
1111
use PhpParser\Node\Stmt\Class_;
1212
use PhpParser\Node\Stmt\ClassMethod;
13-
use PhpParser\NodeTraverser;
13+
use PhpParser\NodeVisitor;
1414
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1515
use Rector\Rector\AbstractRector;
1616
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -24,7 +24,17 @@ final class PreferPHPUnitThisCallRector extends AbstractRector
2424
/**
2525
* @var string[]
2626
*/
27-
private const NON_ASSERT_STATIC_METHODS = ['createMock', 'atLeast', 'atLeastOnce', 'once', 'never'];
27+
private const NON_ASSERT_STATIC_METHODS = [
28+
'createMock',
29+
'atLeast',
30+
'atLeastOnce',
31+
'once',
32+
'never',
33+
'expectException',
34+
'expectExceptionMessage',
35+
'expectExceptionCode',
36+
'expectExceptionMessageMatches',
37+
];
2838

2939
public function __construct(
3040
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
@@ -84,7 +94,7 @@ public function refactor(Node $node): ?Node
8494
$this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|MethodCall {
8595
$isInsideStaticFunctionLike = ($node instanceof ClassMethod && $node->isStatic()) || ($node instanceof Closure && $node->static);
8696
if ($isInsideStaticFunctionLike) {
87-
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
97+
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
8898
}
8999

90100
if (! $node instanceof StaticCall) {

rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private function extractTestWithData(GenericTagValueNode $genericTagValueNode):
189189
$jsonArray = [];
190190

191191
foreach ($testWithItems as $testWithItem) {
192-
$jsonArray[] = Json::decode(trim($testWithItem), Json::FORCE_ARRAY);
192+
$jsonArray[] = Json::decode(trim($testWithItem), forceArrays: true);
193193
}
194194

195195
return $jsonArray;

rules/PHPUnit100/Rector/StmtsAwareInterface/ExpectsMethodCallDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PhpParser\Node\Expr\StaticCall;
1212
use PhpParser\Node\Expr\Variable;
1313
use PhpParser\Node\Stmt\Expression;
14-
use PhpParser\NodeTraverser;
14+
use PhpParser\NodeVisitor;
1515
use Rector\NodeNameResolver\NodeNameResolver;
1616
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
1717
use Rector\PHPUnit\Enum\ConsecutiveVariable;
@@ -78,7 +78,7 @@ public function decorate(Expression $expression): MethodCall|StaticCall|null
7878
new Arg(new Variable(ConsecutiveVariable::MATCHER)),
7979
]);
8080

81-
return NodeTraverser::STOP_TRAVERSAL;
81+
return NodeVisitor::STOP_TRAVERSAL;
8282
});
8383
}
8484

0 commit comments

Comments
 (0)