Skip to content

Commit 22275db

Browse files
authored
[CodeQuality] Handle crash include first class callable on OptionalParametersAfterRequiredRector (#6552)
* [CodeQuality] Handle crash include first class callable on OptionalParametersAfterRequiredRector * more fixture * fix * fix
1 parent 12dfb2e commit 22275db

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;
6+
7+
final class SkipIncludeFirstClassCallable
8+
{
9+
public function run()
10+
{
11+
include $this->call(...);
12+
}
13+
14+
public function call()
15+
{
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;
6+
7+
use stdClass;
8+
9+
final class SkipInstanceofFirstClassCallable
10+
{
11+
public function run($a)
12+
{
13+
$this->call(...) instanceof stdClass;
14+
}
15+
16+
public function call()
17+
{
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;
6+
7+
final class SkipIssetFirstClassCallable
8+
{
9+
public function run()
10+
{
11+
isset($this->call(...));
12+
}
13+
14+
public function call()
15+
{
16+
}
17+
}

src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
use PhpParser\Node\Expr\Eval_;
2929
use PhpParser\Node\Expr\Exit_;
3030
use PhpParser\Node\Expr\FuncCall;
31+
use PhpParser\Node\Expr\Include_;
32+
use PhpParser\Node\Expr\Instanceof_;
33+
use PhpParser\Node\Expr\Isset_;
3134
use PhpParser\Node\Expr\List_;
3235
use PhpParser\Node\Expr\Match_;
3336
use PhpParser\Node\Expr\MethodCall;
@@ -72,6 +75,7 @@
7275
use PhpParser\Node\Stmt\Switch_;
7376
use PhpParser\Node\Stmt\Trait_;
7477
use PhpParser\Node\Stmt\TryCatch;
78+
use PhpParser\Node\Stmt\Unset_;
7579
use PhpParser\Node\UnionType;
7680
use PhpParser\NodeTraverser;
7781
use PHPStan\Analyser\MutatingScope;
@@ -209,7 +213,9 @@ public function processNodes(
209213
$node instanceof Eval_ ||
210214
$node instanceof Print_ ||
211215
$node instanceof Exit_ ||
212-
$node instanceof ArrowFunction
216+
$node instanceof ArrowFunction ||
217+
$node instanceof Include_ ||
218+
$node instanceof Instanceof_
213219
) && $node->expr instanceof Expr) {
214220
$node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);
215221
return;
@@ -356,6 +362,11 @@ public function processNodes(
356362
$this->processYield($node, $mutatingScope);
357363
return;
358364
}
365+
366+
if ($node instanceof Isset_ || $node instanceof Unset_) {
367+
$this->processIssetOrUnset($node, $mutatingScope);
368+
return;
369+
}
359370
};
360371

361372
$this->nodeScopeResolverProcessNodes($stmts, $scope, $nodeCallback);
@@ -383,6 +394,13 @@ private function processYield(Yield_ $yield, MutatingScope $mutatingScope): void
383394
}
384395
}
385396

397+
private function processIssetOrUnset(Isset_|Unset_ $node, MutatingScope $mutatingScope): void
398+
{
399+
foreach ($node->vars as $var) {
400+
$var->setAttribute(AttributeKey::SCOPE, $mutatingScope);
401+
}
402+
}
403+
386404
private function processMatch(Match_ $match, MutatingScope $mutatingScope): void
387405
{
388406
$match->cond->setAttribute(AttributeKey::SCOPE, $mutatingScope);

0 commit comments

Comments
 (0)