Skip to content

Commit 26f7bc1

Browse files
authored
[Php74] Skip closure in attribute on ClosureToArrowFunctionRector (#7834)
* [Php74] Skip closure in attribute on ClosureToArrowFunctionRector * [Php74] Skip closure in attribute on ClosureToArrowFunctionRector * [Php74] Skip closure in attribute on ClosureToArrowFunctionRector
1 parent 760eb16 commit 26f7bc1

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;
4+
5+
class SkipInsideAttribute
6+
{
7+
#[Idempotency(key: static function(Command $command) : IdempotencyKey {
8+
return new IdempotencyKey(
9+
'key',
10+
);
11+
}, seconds: 604_800)]
12+
public function __invoke(Command $command)
13+
{}
14+
}

rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function refactor(Node $node): ?Node
7373
return null;
7474
}
7575

76+
if ($node->getAttribute(AttributeKey::IS_CLOSURE_IN_ATTRIBUTE) === true) {
77+
return null;
78+
}
79+
7680
$attributes = $node->getAttributes();
7781
unset($attributes[AttributeKey::ORIGINAL_NODE]);
7882

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ final class AttributeKey
187187
*/
188188
public const IS_ARRAY_IN_ATTRIBUTE = 'is_array_in_attribute';
189189

190+
/**
191+
* @var string
192+
*/
193+
public const IS_CLOSURE_IN_ATTRIBUTE = 'is_closure_in_attribute';
194+
190195
/**
191196
* @var string
192197
*/

src/PhpParser/NodeVisitor/ContextNodeVisitor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ static function (Node $subNode): null {
145145
$subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true);
146146
}
147147

148+
if ($subNode instanceof Closure) {
149+
$subNode->setAttribute(AttributeKey::IS_CLOSURE_IN_ATTRIBUTE, true);
150+
}
151+
148152
return null;
149153
}
150154
);

0 commit comments

Comments
 (0)