Skip to content

Commit 816c733

Browse files
authored
[CodeQuality] Handle crash on first class callable inside match on OptionalParametersAfterRequiredRector (#6263)
* [CodeQuality] Handle crash on first class callable inside match on OptionalParametersAfterRequiredRector * Fix * Fix * Fix * Fix
1 parent 8073ee7 commit 816c733

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;
4+
5+
use Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Source\SomeClass;
6+
7+
final class SkipMatchWithFirstClassCallable
8+
{
9+
public function run($type):void
10+
{
11+
match($type) {
12+
'string' => SomeClass::fromString(...),
13+
'int' => function () {
14+
return SomeClass::fromInt(...);
15+
}
16+
};
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Source;
6+
7+
class SomeClass
8+
{
9+
public static function fromString(string $id) {}
10+
public static function fromInt(int $id) {}
11+
}

src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PhpParser\Node\Expr\ClassConstFetch;
1818
use PhpParser\Node\Expr\ConstFetch;
1919
use PhpParser\Node\Expr\FuncCall;
20+
use PhpParser\Node\Expr\Match_;
2021
use PhpParser\Node\Expr\MethodCall;
2122
use PhpParser\Node\Expr\New_;
2223
use PhpParser\Node\Expr\NullsafeMethodCall;
@@ -270,6 +271,11 @@ public function processNodes(
270271
$this->processCallike($node, $mutatingScope);
271272
return;
272273
}
274+
275+
if ($node instanceof Match_) {
276+
$this->processMatch($node, $mutatingScope);
277+
return;
278+
}
273279
};
274280

275281
$this->nodeScopeResolverProcessNodes($stmts, $scope, $nodeCallback);
@@ -286,6 +292,20 @@ public function processNodes(
286292
return $stmts;
287293
}
288294

295+
private function processMatch(Match_ $match, MutatingScope $mutatingScope): void
296+
{
297+
$match->cond->setAttribute(AttributeKey::SCOPE, $mutatingScope);
298+
foreach ($match->arms as $arm) {
299+
if ($arm->conds !== null) {
300+
foreach ($arm->conds as $cond) {
301+
$cond->setAttribute(AttributeKey::SCOPE, $mutatingScope);
302+
}
303+
}
304+
305+
$arm->body->setAttribute(AttributeKey::SCOPE, $mutatingScope);
306+
}
307+
}
308+
289309
/**
290310
* @param Stmt[] $stmts
291311
* @param callable(Node $node, MutatingScope $scope): void $nodeCallback

0 commit comments

Comments
 (0)