Skip to content

Commit d7770c8

Browse files
Support Attribute in SortNamedParamRector (#7684)
* Support Attribute in SortNamedParam * Move to source folder
1 parent 9f4509d commit d7770c8

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Fixture;
4+
5+
use Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Source\MyAttribute;
6+
7+
#[MyAttribute(bar: 1, foo: 2)]
8+
#[MyAttribute(1, baz: 2, bar: 3)]
9+
class Test
10+
{
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Fixture;
18+
19+
use Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Source\MyAttribute;
20+
21+
#[MyAttribute(foo: 2, bar: 1)]
22+
#[MyAttribute(1, bar: 3, baz: 2)]
23+
class Test
24+
{
25+
}
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\FuncCall\SortNamedParamRector\Source;
4+
5+
#[\Attribute]
6+
class MyAttribute
7+
{
8+
public function __construct($foo = null, $bar = null, $baz = null)
9+
{
10+
}
11+
}

rules/CodeQuality/Rector/FuncCall/SortNamedParamRector.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Attribute;
10+
use PhpParser\Node\Expr\CallLike;
911
use PhpParser\Node\Expr\FuncCall;
1012
use PhpParser\Node\Expr\MethodCall;
1113
use PhpParser\Node\Expr\New_;
@@ -62,19 +64,23 @@ function run($foo = null, $bar = null, $baz = null) {}
6264
*/
6365
public function getNodeTypes(): array
6466
{
65-
return [MethodCall::class, StaticCall::class, New_::class, FuncCall::class];
67+
return [MethodCall::class, StaticCall::class, New_::class, FuncCall::class, Attribute::class];
6668
}
6769

6870
/**
69-
* @param MethodCall|StaticCall|New_|FuncCall $node
71+
* @param MethodCall|StaticCall|New_|FuncCall|Attribute $node
7072
*/
7173
public function refactor(Node $node): ?Node
7274
{
73-
if ($node->isFirstClassCallable()) {
75+
if ($node instanceof CallLike && $node->isFirstClassCallable()) {
7476
return null;
7577
}
7678

77-
$args = $node->getArgs();
79+
if ($node instanceof Attribute) {
80+
$args = $node->args;
81+
} else {
82+
$args = $node->getArgs();
83+
}
7884
if (count($args) <= 1) {
7985
return null;
8086
}
@@ -85,6 +91,8 @@ public function refactor(Node $node): ?Node
8591

8692
if ($node instanceof New_) {
8793
$functionLikeReflection = $this->reflectionResolver->resolveMethodReflectionFromNew($node);
94+
} elseif ($node instanceof Attribute) {
95+
$functionLikeReflection = $this->reflectionResolver->resolveMethodReflectionFromAttribute($node);
8896
} else {
8997
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
9098
}

src/Reflection/ReflectionResolver.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Reflection;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Attribute;
89
use PhpParser\Node\Expr\CallLike;
910
use PhpParser\Node\Expr\FuncCall;
1011
use PhpParser\Node\Expr\MethodCall;
@@ -256,6 +257,19 @@ public function resolveMethodReflectionFromNew(New_ $new): ?MethodReflection
256257
return $this->resolveMethodReflection($className, MethodName::CONSTRUCT, $scope);
257258
}
258259

260+
public function resolveMethodReflectionFromAttribute(Attribute $attribute): ?MethodReflection
261+
{
262+
$attributeClassType = $this->nodeTypeResolver->getType($attribute->name);
263+
$className = ClassNameFromObjectTypeResolver::resolve($attributeClassType);
264+
265+
if ($className === null) {
266+
return null;
267+
}
268+
269+
$scope = $attribute->getAttribute(AttributeKey::SCOPE);
270+
return $this->resolveMethodReflection($className, MethodName::CONSTRUCT, $scope);
271+
}
272+
259273
public function resolvePropertyReflectionFromPropertyFetch(
260274
PropertyFetch | StaticPropertyFetch $propertyFetch
261275
): ?PhpPropertyReflection {

0 commit comments

Comments
 (0)