Skip to content

Commit fe319d7

Browse files
authored
Rectify (#7084)
1 parent eb07bff commit fe319d7

2 files changed

Lines changed: 42 additions & 45 deletions

File tree

rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ private function refactorProperty(
191191
// remove assign
192192
unset($constructClassMethod->stmts[$key]);
193193

194-
195194
return true;
196195
}
197196
}

rules/CodingStyle/Rector/FuncCall/ClosureFromCallableToFirstClassCallableRector.php

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
namespace Rector\CodingStyle\Rector\FuncCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr;
10+
use PhpParser\Node\Expr\Array_;
11+
use PhpParser\Node\Expr\ClassConstFetch;
12+
use PhpParser\Node\Expr\FuncCall;
13+
use PhpParser\Node\Expr\MethodCall;
14+
use PhpParser\Node\Expr\StaticCall;
15+
use PhpParser\Node\Expr\Variable;
16+
use PhpParser\Node\Identifier;
17+
use PhpParser\Node\Name;
18+
use PhpParser\Node\Name\FullyQualified;
19+
use PhpParser\Node\Scalar\String_;
20+
use PhpParser\Node\VariadicPlaceholder;
821
use Rector\Rector\AbstractRector;
922
use Rector\ValueObject\PhpVersionFeature;
1023
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -16,19 +29,15 @@
1629
*/
1730
final class ClosureFromCallableToFirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface
1831
{
19-
public function __construct()
20-
{
21-
}
22-
2332
public function getRuleDefinition(): RuleDefinition
2433
{
2534
return new RuleDefinition(
2635
'Change `Closure::fromCallable()` to first class callable syntax',
2736
[
2837
new CodeSample('Closure::fromCallable([$obj, \'method\']);', '$obj->method(...);'),
29-
new CodeSample('Closure::fromCallable(\'trim\');', 'trim(...);'),
38+
new CodeSample("Closure::fromCallable('trim');", 'trim(...);'),
3039
new CodeSample(
31-
'Closure::fromCallable([\'SomeClass\', \'staticMethod\']);',
40+
"Closure::fromCallable(['SomeClass', 'staticMethod']);",
3241
'SomeClass::staticMethod(...);'
3342
),
3443
]
@@ -41,11 +50,11 @@ public function getRuleDefinition(): RuleDefinition
4150
*/
4251
public function getNodeTypes(): array
4352
{
44-
return [Node\Expr\StaticCall::class];
53+
return [StaticCall::class];
4554
}
4655

4756
/**
48-
* @param Node\Expr\StaticCall $node
57+
* @param StaticCall $node
4958
*/
5059
public function refactor(Node $node): ?Node
5160
{
@@ -54,53 +63,46 @@ public function refactor(Node $node): ?Node
5463
}
5564

5665
$arg = $node->args[0];
57-
if (! $arg instanceof Node\Arg) {
66+
if (! $arg instanceof Arg) {
5867
return null;
5968
}
6069

61-
if ($arg->value instanceof Node\Scalar\String_) {
62-
return new Node\Expr\FuncCall(
63-
$this->toFullyQualified($arg->value->value),
64-
[new Node\VariadicPlaceholder()],
65-
);
70+
if ($arg->value instanceof String_) {
71+
return new FuncCall($this->toFullyQualified($arg->value->value), [new VariadicPlaceholder()]);
6672
}
6773

68-
if ($arg->value instanceof Node\Expr\Array_) {
74+
if ($arg->value instanceof Array_) {
6975
if (
7076
! array_key_exists(0, $arg->value->items)
7177
|| ! array_key_exists(1, $arg->value->items)
72-
|| ! $arg->value->items[1]->value instanceof Node\Scalar\String_
78+
|| ! $arg->value->items[1]->value instanceof String_
7379
) {
7480
return null;
7581
}
7682

77-
if ($arg->value->items[0]->value instanceof Node\Expr\Variable) {
78-
return new Node\Expr\MethodCall(
83+
if ($arg->value->items[0]->value instanceof Variable) {
84+
return new MethodCall(
7985
$arg->value->items[0]->value,
8086
$arg->value->items[1]->value->value,
81-
[new Node\VariadicPlaceholder()],
87+
[new VariadicPlaceholder()],
8288
);
8389
}
8490

85-
if ($arg->value->items[0]->value instanceof Node\Scalar\String_) {
86-
$classNode = new Node\Name\FullyQualified($arg->value->items[0]->value->value);
87-
} elseif ($arg->value->items[0]->value instanceof Node\Expr\ClassConstFetch) {
88-
if ($arg->value->items[0]->value->class instanceof Node\Expr) {
91+
if ($arg->value->items[0]->value instanceof String_) {
92+
$classNode = new FullyQualified($arg->value->items[0]->value->value);
93+
} elseif ($arg->value->items[0]->value instanceof ClassConstFetch) {
94+
if ($arg->value->items[0]->value->class instanceof Expr) {
8995
return null;
9096
}
9197

92-
$classNode = new Node\Name\FullyQualified($arg->value->items[0]->value->class->name);
93-
} elseif ($arg->value->items[0]->value instanceof Node\Name\FullyQualified) {
94-
$classNode = new Node\Name\FullyQualified($arg->value->items[0]->value->name);
98+
$classNode = new FullyQualified($arg->value->items[0]->value->class->name);
99+
} elseif ($arg->value->items[0]->value instanceof FullyQualified) {
100+
$classNode = new FullyQualified($arg->value->items[0]->value->name);
95101
} else {
96102
return null;
97103
}
98104

99-
return new Node\Expr\StaticCall(
100-
$classNode,
101-
$arg->value->items[1]->value->value,
102-
[new Node\VariadicPlaceholder()],
103-
);
105+
return new StaticCall($classNode, $arg->value->items[1]->value->value, [new VariadicPlaceholder()]);
104106
}
105107

106108
return $node;
@@ -111,37 +113,33 @@ public function provideMinPhpVersion(): int
111113
return PhpVersionFeature::FIRST_CLASS_CALLABLE_SYNTAX;
112114
}
113115

114-
public function shouldSkip(Node\Expr\StaticCall $node): bool
116+
public function shouldSkip(StaticCall $staticCall): bool
115117
{
116-
if (! $node->class instanceof Node\Name) {
117-
return true;
118-
}
119-
120-
if (! $this->isName($node->class, 'Closure')) {
118+
if (! $staticCall->class instanceof Name) {
121119
return true;
122120
}
123121

124-
if (! $node->name instanceof Node\Identifier || $node->name->name !== 'fromCallable') {
122+
if (! $this->isName($staticCall->class, 'Closure')) {
125123
return true;
126124
}
127125

128-
if ($node->isFirstClassCallable()) {
126+
if (! $staticCall->name instanceof Identifier || $staticCall->name->name !== 'fromCallable') {
129127
return true;
130128
}
131129

132-
$args = $node->getArgs();
133-
if (count($args) !== 1) {
130+
if ($staticCall->isFirstClassCallable()) {
134131
return true;
135132
}
136133

137-
return false;
134+
$args = $staticCall->getArgs();
135+
return count($args) !== 1;
138136
}
139137

140-
public function toFullyQualified(string $functionName): Node\Name\FullyQualified
138+
public function toFullyQualified(string $functionName): FullyQualified
141139
{
142140
// in case there's already a \ prefix, remove it
143141
$functionName = ltrim($functionName, '\\');
144142

145-
return new Node\Name\FullyQualified($functionName);
143+
return new FullyQualified($functionName);
146144
}
147145
}

0 commit comments

Comments
 (0)