Skip to content

Commit badcffc

Browse files
[Arguments] Skip self class const fetch inside this class target replacement on ReplaceArgumentDefaultValueRector (#7940)
* [Arguments] Skip self class const fetch inside this class itself on ReplaceArgumentDefaultValueRector * [ci-review] Rector Rectify * allow from other base class * allow from other base class * ensure verify normalized value * ensure verify normalized value * ensure verify normalized value --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent d913384 commit badcffc

File tree

6 files changed

+75
-7
lines changed

6 files changed

+75
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;
4+
5+
class FromOtherBaseClass
6+
{
7+
public const PATH = 'path';
8+
9+
public static function test1(string $path)
10+
{
11+
\Mage::getStoreConfig(self::PATH);
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;
20+
21+
class FromOtherBaseClass
22+
{
23+
public const PATH = 'path';
24+
25+
public static function test1(string $path)
26+
{
27+
\Mage::getStoreConfig(\Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture\SomeBase::PATH);
28+
}
29+
}
30+
31+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;
4+
5+
class SomeBase
6+
{
7+
public const PATH = 'path';
8+
9+
public static function test1(string $path)
10+
{
11+
\Mage::getStoreConfig(self::PATH);
12+
}
13+
}

rules-tests/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector/config/configured_rule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,7 @@
7777
1,
7878
4
7979
),
80+
81+
new ReplaceArgumentDefaultValue(Mage::class, 'getStoreConfig', 0, 'path', 'Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture\SomeBase::PATH'),
8082
]);
8183
};

rules/Arguments/ArgumentDefaultValueReplacer.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
use PhpParser\Node\Expr\New_;
1414
use PhpParser\Node\Expr\StaticCall;
1515
use PhpParser\Node\Expr\Variable;
16+
use PhpParser\Node\Identifier;
17+
use PhpParser\Node\Name;
1618
use PhpParser\Node\Stmt\ClassMethod;
1719
use Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface;
1820
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
1921
use Rector\NodeAnalyzer\ArgsAnalyzer;
22+
use Rector\NodeTypeResolver\NodeTypeResolver;
2023
use Rector\PhpParser\AstResolver;
2124
use Rector\PhpParser\Node\NodeFactory;
2225
use Rector\PhpParser\Node\Value\ValueResolver;
26+
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
2327

2428
final readonly class ArgumentDefaultValueReplacer
2529
{
@@ -28,6 +32,7 @@ public function __construct(
2832
private ValueResolver $valueResolver,
2933
private ArgsAnalyzer $argsAnalyzer,
3034
private AstResolver $astResolver,
35+
private NodeTypeResolver $nodeTypeResolver
3136
) {
3237
}
3338

@@ -153,7 +158,24 @@ private function processArgs(
153158
if (is_scalar(
154159
$replaceArgumentDefaultValue->getValueBefore()
155160
) && $argValue === $replaceArgumentDefaultValue->getValueBefore()) {
156-
$particularArg->value = $this->normalizeValue($replaceArgumentDefaultValue->getValueAfter());
161+
$normalizedValueAfter = $this->normalizeValue($replaceArgumentDefaultValue->getValueAfter());
162+
if ($particularArg->value instanceof ClassConstFetch
163+
&& $particularArg->value->class instanceof Name
164+
&& $particularArg->value->class->isSpecialClassName()
165+
&& $normalizedValueAfter instanceof ClassConstFetch
166+
&& is_string($replaceArgumentDefaultValue->getValueAfter())
167+
&& str_contains($replaceArgumentDefaultValue->getValueAfter(), '::')) {
168+
[$targetClass, $targetConstant] = explode('::', $replaceArgumentDefaultValue->getValueAfter());
169+
$type = $this->nodeTypeResolver->getType($particularArg->value->class);
170+
if ($type instanceof FullyQualifiedObjectType
171+
&& $type->getClassName() === $targetClass
172+
&& $particularArg->value->name instanceof Identifier
173+
&& $particularArg->value->name->toString() === $targetConstant) {
174+
return null;
175+
}
176+
}
177+
178+
$particularArg->value = $normalizedValueAfter;
157179
return $expr;
158180
}
159181

src/Caching/Config/FileHashComputer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
/**
1313
* Inspired by https://github.com/symplify/easy-coding-standard/blob/e598ab54686e416788f28fcfe007fd08e0f371d9/packages/changed-files-detector/src/FileHashComputer.php
1414
*/
15-
final class FileHashComputer
15+
final readonly class FileHashComputer
1616
{
1717
/**
1818
* @param CacheMetaExtensionInterface[] $cacheMetaExtensions
1919
*/
2020
public function __construct(
21-
private readonly array $cacheMetaExtensions = []
21+
private array $cacheMetaExtensions = []
2222
) {
2323
}
2424

@@ -34,8 +34,8 @@ public function compute(string $filePath): string
3434
private function computeExtensionHash(): string
3535
{
3636
$extensionHash = '';
37-
foreach ($this->cacheMetaExtensions as $cacheMetaExtension) {
38-
$extensionHash .= $cacheMetaExtension->getKey() . ':' . $cacheMetaExtension->getHash();
37+
foreach ($this->cacheMetaExtensions as $cacheMetumExtension) {
38+
$extensionHash .= $cacheMetumExtension->getKey() . ':' . $cacheMetumExtension->getHash();
3939
}
4040

4141
return $extensionHash;

src/Configuration/RectorConfigBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ public function __invoke(RectorConfig $rectorConfig): void
322322
$rectorConfig->containerCacheDirectory($this->containerCacheDirectory);
323323
}
324324

325-
foreach ($this->cacheMetaExtensions as $cacheMetaExtensionClass) {
326-
$rectorConfig->cacheMetaExtension($cacheMetaExtensionClass);
325+
foreach ($this->cacheMetaExtensions as $cacheMetumExtension) {
326+
$rectorConfig->cacheMetaExtension($cacheMetumExtension);
327327
}
328328

329329
if ($this->importNames || $this->importDocBlockNames) {

0 commit comments

Comments
 (0)