Skip to content

Commit ca2ba70

Browse files
committed
refactor(FuncCall): Update RenameAppFunctionToResolveFunctionRector
- Change the base class from AbstractRector to AbstractProxyRector. - Simplify the constructor by removing the RenameFunctionRector dependency. - Implement the applyRefactor method to utilize the parent class's functionality. - Add a proxyRectorClass method to define the associated RenameFunctionRector.
1 parent 7cae1bb commit ca2ba70

9 files changed

Lines changed: 75 additions & 76 deletions

baselines/missingType.return.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
parameters:
44
ignoreErrors:
55
-
6-
message: '#^Method Guanguans\\RectorRules\\Rector\\AbstractProxyRector\:\:applyRefactor\(\) has no return type specified\.$#'
6+
message: '#^Method Guanguans\\RectorRules\\Rector\\AbstractProxyRector\:\:rawRefactor\(\) has no return type specified\.$#'
77
count: 1
88
path: ../src/Rector/AbstractProxyRector.php

baselines/typeCoverage.returnTypeCoverage.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
parameters:
44
ignoreErrors:
55
-
6-
message: '#^Out of 86 possible return types, only 84 \- 97\.6 %% actually have it\. Add more return types to get over 100 %%$#'
6+
message: '#^Out of 88 possible return types, only 86 \- 97\.7 %% actually have it\. Add more return types to get over 100 %%$#'
77
count: 2
88
path: ../src/Rector/AbstractProxyRector.php

composer-dependency-analyser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
__DIR__.'/tests/',
2828
])
2929
->ignoreUnknownClasses([
30-
// CodeSample::class,
30+
CodeSample::class,
3131
])
3232
/** @see \ShipMonk\ComposerDependencyAnalyser\Analyser::CORE_EXTENSIONS */
3333
->ignoreErrorsOnExtensions(

phpstan.neon.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ parameters:
131131
- identifier: cast.int
132132
- identifier: encapsedStringPart.nonString
133133
- identifier: method.childParameterType
134-
- identifier: method.dynamicName
134+
# - identifier: method.dynamicName
135135
- identifier: return.type
136136
- identifier: symplify.explicitInterfaceSuffixName
137137
- identifier: symplify.forbiddenArrayMethodCall
138138
- identifier: symplify.forbiddenNode
139139
- identifier: symplify.forbiddenStaticClassConstFetch
140140
- identifier: symplify.noArrayMapWithArrayCallable
141-
- identifier: symplify.noDynamicName
142-
# - message: '#^Unused Guanguans\\RectorRules\\Rector\\.*\\.*\:\:__construct$#'
141+
# - identifier: symplify.noDynamicName
142+
- message: '#^Unused Guanguans\\RectorRules\\Rector\\.*\\.*\:\:__construct$#'
143143
- message: '#^Cannot call method (all|reduce|getParameterOption)\(\) on mixed\.$#'
144144

145145
-

src/Rector/AbstractProxyRector.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,66 @@
2323
*/
2424
abstract class AbstractProxyRector extends AbstractRector
2525
{
26-
protected RectorConfig $rectorConfig;
27-
protected \Rector\Rector\AbstractRector $proxyRector;
26+
// /** @param list<mixed> $arguments */
27+
// public function __call(string $name, array $arguments)
28+
// {return $this->makeProxyRector()->{$name}(...$arguments);}
2829

2930
/**
30-
* @see \Rector\Testing\PHPUnit\AbstractLazyTestCase::getContainer()
31-
*
3231
* @throws \Illuminate\Contracts\Container\BindingResolutionException
32+
*
33+
* {@inheritDoc}
3334
*/
34-
public function __construct()
35+
final public function getNodeTypes(): array
3536
{
36-
$rectorConfig = (new LazyContainerFactory)->create();
37-
$rectorConfig->boot();
38-
$this->rectorConfig = $rectorConfig;
39-
$this->proxyRector = clone $rectorConfig->make($this->proxyRectorClass());
37+
return $this->makeProxyRector()->getNodeTypes();
4038
}
4139

4240
/**
43-
* @param list<mixed> $arguments
41+
* {@inheritDoc}
4442
*
45-
* @return mixed
43+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
4644
*/
47-
public function __call(string $name, array $arguments)
45+
final public function refactor(Node $node)
4846
{
49-
return $this->proxyRector->{$name}(...$arguments);
47+
return $this->rawRefactor($node);
5048
}
5149

5250
/**
53-
* @throws \ReflectionException
51+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
5452
*
55-
* {@inheritDoc}
53+
* @return null|list<Node>|NodeVisitor::REMOVE_NODE|\PhpParser\Node
5654
*/
57-
final public function getNodeTypes(): array
55+
protected function rawRefactor(Node $node)
5856
{
59-
/** @var \ReflectionClass<\Rector\Rector\AbstractRector> $reflectionClass */
60-
$reflectionClass = new \ReflectionClass($this->proxyRectorClass());
61-
62-
return $reflectionClass->newInstanceWithoutConstructor()->getNodeTypes();
57+
return $this->makeProxyRector()->refactor($node);
6358
}
6459

6560
/**
66-
* {@inheritDoc}
61+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
62+
*
63+
* @return \Rector\Contract\Rector\ConfigurableRectorInterface&\Rector\Rector\AbstractRector
6764
*/
68-
final public function refactor(Node $node)
65+
protected function makeProxyRector(): \Rector\Rector\AbstractRector
6966
{
70-
return $this->applyRefactor($node);
67+
return $this->makeRectorConfig()->make($this->proxyRectorClass());
7168
}
7269

7370
/**
74-
* @return null|list<Node>|NodeVisitor::REMOVE_NODE|\PhpParser\Node
71+
* @see \Rector\Testing\PHPUnit\AbstractLazyTestCase::getContainer()
7572
*/
76-
protected function applyRefactor(Node $node)
73+
protected function makeRectorConfig(): RectorConfig
7774
{
78-
return $this->proxyRector->refactor($node);
75+
static $rectorConfig;
76+
77+
if ($rectorConfig instanceof RectorConfig) {
78+
return $rectorConfig;
79+
}
80+
81+
$rectorConfig = (new LazyContainerFactory)->create();
82+
$rectorConfig->singletonIf($this->proxyRectorClass());
83+
$rectorConfig->boot();
84+
85+
return $rectorConfig;
7986
}
8087

8188
/**

src/Rector/Array_/SortListItemOfSameScalarTypeRector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ public function refactor(Node $node): ?Node
129129

130130
/**
131131
* @param array{
132-
* ignore_comment: bool,
133-
* ignore_docblock: bool,
134-
* sort_comparator: callable(string, string): int,
135-
* sort_direction: key-of<self::SORT_DIRECTION_MAP>,
132+
* ignore_comment?: bool,
133+
* ignore_docblock?: bool,
134+
* sort_comparator?: callable(string, string): int,
135+
* sort_direction?: key-of<self::SORT_DIRECTION_MAP>,
136136
* } $configuration
137137
*/
138138
public function configure(array $configuration): void

src/Rector/Class_/UpdateRectorMethodNodeParamDocblockFromNodeTypesRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function classType(): string
9696

9797
protected function classMethodNode(Class_ $classNode): ?ClassMethod
9898
{
99-
return $classNode->getMethod('refactor');
99+
return $classNode->getMethod('refactor') ?? $classNode->getMethod('rawRefactor');
100100
}
101101

102102
/**

src/Rector/FuncCall/RenameAppFunctionToResolveFunctionRector.php

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,16 @@
1414

1515
namespace Guanguans\RectorRules\Rector\FuncCall;
1616

17-
use Guanguans\RectorRules\Rector\AbstractRector;
17+
use Guanguans\RectorRules\Rector\AbstractProxyRector;
1818
use PhpParser\Node;
19-
use PhpParser\Node\Expr\FuncCall;
2019
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
2120
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2221

2322
/**
2423
* @see \Guanguans\RectorRulesTests\Rector\FuncCall\RenameAppFunctionToResolveFunctionRector\RenameAppFunctionToResolveFunctionRectorTest
2524
*/
26-
final class RenameAppFunctionToResolveFunctionRector extends AbstractRector
25+
final class RenameAppFunctionToResolveFunctionRector extends AbstractProxyRector
2726
{
28-
private RenameFunctionRector $renameFunctionRector;
29-
30-
public function __construct(RenameFunctionRector $renameFunctionRector)
31-
{
32-
$this->renameFunctionRector = clone $renameFunctionRector;
33-
$this->renameFunctionRector->configure([
34-
'app' => 'resolve',
35-
]);
36-
}
37-
38-
/**
39-
* @throws \ReflectionException
40-
*/
41-
public function getNodeTypes(): array
42-
{
43-
// return $this->renameFunctionRector->getNodeTypes();
44-
return (new \ReflectionClass(RenameFunctionRector::class))->newInstanceWithoutConstructor()->getNodeTypes();
45-
}
46-
47-
/**
48-
* @see https://github.com/driftingly/rector-laravel/blob/main/src/Rector/FuncCall/AppToResolveRector.php
49-
* @see https://github.com/laravel/framework/commit/8f232a972fd8b4bfa1901a47c1e649e2a1278bd6
50-
*
51-
* @param \PhpParser\Node\Expr\FuncCall $node
52-
*/
53-
public function refactor(Node $node): ?Node
54-
{
55-
$abstract = $node->getArg('abstract', 0);
56-
57-
if (null === $abstract || $this->getType($abstract->value)->isNull()->yes()) {
58-
return null;
59-
}
60-
61-
return $this->renameFunctionRector->refactor($node);
62-
}
63-
6427
/**
6528
* @return list<\Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample>
6629
*/
@@ -89,4 +52,32 @@ protected function codeSamples(): array
8952
),
9053
];
9154
}
55+
56+
/**
57+
* @see https://github.com/driftingly/rector-laravel/blob/main/src/Rector/FuncCall/AppToResolveRector.php
58+
* @see https://github.com/laravel/framework/commit/8f232a972fd8b4bfa1901a47c1e649e2a1278bd6
59+
*
60+
* @param \PhpParser\Node\Expr\FuncCall $node
61+
*
62+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
63+
*/
64+
protected function rawRefactor(Node $node): ?Node
65+
{
66+
$abstract = $node->getArg('abstract', 0);
67+
68+
if (null === $abstract || $this->getType($abstract->value)->isNull()->yes()) {
69+
return null;
70+
}
71+
72+
$this->makeProxyRector()->configure([
73+
'app' => 'resolve',
74+
]);
75+
76+
return parent::rawRefactor($node);
77+
}
78+
79+
protected function proxyRectorClass(): string
80+
{
81+
return RenameFunctionRector::class;
82+
}
9283
}

tests/Rector/FuncCall/RenameAppFunctionToResolveFunctionRector/RenameAppFunctionToResolveFunctionRectorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Guanguans\RectorRulesTests\Rector\AbstractRectorTestCase;
2424

2525
/**
26+
* @covers \Guanguans\RectorRules\Rector\AbstractProxyRector
2627
* @covers \Guanguans\RectorRules\Rector\FuncCall\RenameAppFunctionToResolveFunctionRector
2728
*/
2829
final class RenameAppFunctionToResolveFunctionRectorTest extends AbstractRectorTestCase

0 commit comments

Comments
 (0)