Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Php81] Handle crash on ArrowFunction attribute on FirstClassCallableRector #6313

Merged
merged 3 commits into from
Sep 18, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;

function run(\Closure $closue): void
{
}

run(
#[SomeAttribute(['array-argument'])]
fn(): null => null,
);
43 changes: 39 additions & 4 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Match_;
Expand All @@ -30,14 +32,19 @@
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\EnumCase;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
Expand Down Expand Up @@ -129,11 +136,15 @@ public function processNodes(
/** @var MutatingScope $mutatingScope */
$mutatingScope = $this->resolveClassOrInterfaceScope($node, $mutatingScope);
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->decorateNodeAttrGroups($node, $mutatingScope, $nodeCallback);

return;
}

if ($node instanceof Trait_) {
$this->processTrait($node, $mutatingScope, $nodeCallback);
$this->decorateNodeAttrGroups($node, $mutatingScope, $nodeCallback);

return;
}

Expand All @@ -153,6 +164,8 @@ public function processNodes(
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

$this->decorateNodeAttrGroups($node, $mutatingScope, $nodeCallback);

if ($node instanceof FileWithoutNamespace) {
$this->nodeScopeResolverProcessNodes($node->stmts, $mutatingScope, $nodeCallback);
return;
Expand Down Expand Up @@ -360,12 +373,36 @@ private function processArrayItem(ArrayItem $arrayItem, MutatingScope $mutatingS
$arrayItem->value->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

private function decorateTraitAttrGroups(Trait_ $trait, MutatingScope $mutatingScope): void
/**
* @param callable(Node $trait, MutatingScope $scope): void $nodeCallback
*/
private function decorateNodeAttrGroups(Node $node, MutatingScope $mutatingScope, callable $nodeCallback): void
{
foreach ($trait->attrGroups as $attrGroup) {
// better to have AttrGroupsAwareInterface for all Node definition with attrGroups property
// but because may conflict with StmtsAwareInterface patch, this needs to be here
if (
! $node instanceof Param &&
! $node instanceof ArrowFunction &&
! $node instanceof Closure &&
! $node instanceof ClassConst &&
! $node instanceof ClassLike &&
! $node instanceof ClassMethod &&
! $node instanceof EnumCase &&
! $node instanceof Function_ &&
! $node instanceof Property
) {
return;
}

foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
foreach ($attr->args as $arg) {
$arg->value->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->nodeScopeResolverProcessNodes(
[new Expression($arg->value)],
$mutatingScope,
$nodeCallback
);
}
}
}
Expand Down Expand Up @@ -501,7 +538,6 @@ private function processTrait(Trait_ $trait, MutatingScope $mutatingScope, calla
if (! $this->reflectionProvider->hasClass($traitName)) {
$trait->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->nodeScopeResolverProcessNodes($trait->stmts, $mutatingScope, $nodeCallback);
$this->decorateTraitAttrGroups($trait, $mutatingScope);

return;
}
Expand All @@ -521,6 +557,5 @@ private function processTrait(Trait_ $trait, MutatingScope $mutatingScope, calla

$trait->setAttribute(AttributeKey::SCOPE, $traitScope);
$this->nodeScopeResolverProcessNodes($trait->stmts, $traitScope, $nodeCallback);
$this->decorateTraitAttrGroups($trait, $traitScope);
}
}