Skip to content

Commit 1642aea

Browse files
authored
[cleanup] deprecate SHOULD_KEEP_PRE_SLASH, remove dont traverse const on StringClassNameToClassConstantRector (#7704)
* cover class constants in StringClassNameToClassConstantRector, as no reason to skip them * [cleanup] deprecate SHOULD_KEEP_PRE_SLASH, remove dont traverse const on StringClassNameToClassConstantRector
1 parent 229a5f9 commit 1642aea

File tree

10 files changed

+55
-152
lines changed

10 files changed

+55
-152
lines changed

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,5 @@ parameters:
392392
-
393393
identifier: rector.noIntegerRefactorReturn
394394
paths:
395-
- rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php
396395
- rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
397396
- tests/Issues/InfiniteLoop/Rector/MethodCall/InfinityLoopRector.php
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture;
4+
5+
final class IncludeClassConst
6+
{
7+
const SKIP_TYPES = [
8+
'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\SomeUser'
9+
];
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture;
17+
18+
final class IncludeClassConst
19+
{
20+
const SKIP_TYPES = [
21+
\Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\SomeUser::class
22+
];
23+
}
24+
25+
?>

rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_dynamic_variable.php.inc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture;
44

5-
class SkipDynamicVariable
5+
final class SkipDynamicVariable
66
{
77
public function bar()
88
{
@@ -12,4 +12,13 @@ class SkipDynamicVariable
1212
${'field'.$i} = $employee->data['dd'.$i];
1313
}
1414
}
15-
}
15+
16+
public function foo()
17+
{
18+
$dd1 = 1;
19+
20+
for ($i=1; $i <= 2; $i++) {
21+
${'field'.$i} = ${'dd'.$i};
22+
}
23+
}
24+
}

rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_dynamic_variable2.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.

rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_in_array_constant.php.inc

Lines changed: 0 additions & 10 deletions
This file was deleted.

rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/FixtureKeepPreSlash/pre_slash.php.inc

Lines changed: 0 additions & 27 deletions
This file was deleted.

rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/KeepPreSlashTest.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/config/configured_rule_keep_pre_slash.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
namespace Rector\Php55\Rector\String_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr\BinaryOp\Concat;
98
use PhpParser\Node\Expr\ClassConstFetch;
10-
use PhpParser\Node\Expr\FuncCall;
119
use PhpParser\Node\Name\FullyQualified;
1210
use PhpParser\Node\Scalar\String_;
13-
use PhpParser\Node\Stmt\ClassConst;
14-
use PhpParser\NodeVisitor;
1511
use PHPStan\Reflection\ReflectionProvider;
1612
use Rector\Contract\Rector\ConfigurableRectorInterface;
13+
use Rector\NodeTypeResolver\Node\AttributeKey;
1714
use Rector\Rector\AbstractRector;
1815
use Rector\ValueObject\PhpVersionFeature;
1916
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -27,6 +24,7 @@
2724
final class StringClassNameToClassConstantRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
2825
{
2926
/**
27+
* @deprecated since 2.2.12. Default behavior now.
3028
* @var string
3129
*/
3230
public const SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash';
@@ -36,8 +34,6 @@ final class StringClassNameToClassConstantRector extends AbstractRector implemen
3634
*/
3735
private array $classesToSkip = [];
3836

39-
private bool $shouldKeepPreslash = false;
40-
4137
public function __construct(
4238
private readonly ReflectionProvider $reflectionProvider,
4339
) {
@@ -75,11 +71,7 @@ public function run()
7571
}
7672
CODE_SAMPLE
7773
,
78-
[
79-
'ClassName',
80-
'AnotherClassName',
81-
self::SHOULD_KEEP_PRE_SLASH => false,
82-
],
74+
['ClassName', 'AnotherClassName'],
8375
),
8476
]);
8577
}
@@ -89,26 +81,15 @@ public function run()
8981
*/
9082
public function getNodeTypes(): array
9183
{
92-
return [String_::class, FuncCall::class, ClassConst::class];
84+
return [String_::class];
9385
}
9486

9587
/**
96-
* @param String_|FuncCall|ClassConst $node
97-
* @return Concat|ClassConstFetch|null|NodeVisitor::DONT_TRAVERSE_CHILDREN
88+
* @param String_ $node
9889
*/
99-
public function refactor(Node $node): Concat|ClassConstFetch|null|int
90+
public function refactor(Node $node): ClassConstFetch|null
10091
{
101-
// allow class strings to be part of class const arrays, as probably on purpose
102-
if ($node instanceof ClassConst) {
103-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
104-
}
105-
106-
// keep allowed string as condition
107-
if ($node instanceof FuncCall) {
108-
if ($this->isName($node, 'is_a')) {
109-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
110-
}
111-
92+
if ($this->shouldSkipIsA($node)) {
11293
return null;
11394
}
11495

@@ -125,14 +106,6 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int
125106
}
126107

127108
$fullyQualified = new FullyQualified($classLikeName);
128-
if ($this->shouldKeepPreslash && $classLikeName !== $node->value) {
129-
$preSlashCount = strlen($node->value) - strlen($classLikeName);
130-
$preSlash = str_repeat('\\', $preSlashCount);
131-
$string = new String_($preSlash);
132-
133-
return new Concat($string, new ClassConstFetch($fullyQualified, 'class'));
134-
}
135-
136109
return new ClassConstFetch($fullyQualified, 'class');
137110
}
138111

@@ -141,13 +114,6 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int
141114
*/
142115
public function configure(array $configuration): void
143116
{
144-
if (isset($configuration[self::SHOULD_KEEP_PRE_SLASH]) && is_bool(
145-
$configuration[self::SHOULD_KEEP_PRE_SLASH]
146-
)) {
147-
$this->shouldKeepPreslash = $configuration[self::SHOULD_KEEP_PRE_SLASH];
148-
unset($configuration[self::SHOULD_KEEP_PRE_SLASH]);
149-
}
150-
151117
Assert::allString($configuration);
152118

153119
$this->classesToSkip = $configuration;
@@ -190,4 +156,15 @@ private function shouldSkip(string $classLikeName): bool
190156

191157
return false;
192158
}
159+
160+
private function shouldSkipIsA(String_ $string): bool
161+
{
162+
if (! $string->getAttribute(AttributeKey::IS_ARG_VALUE, false)) {
163+
return false;
164+
}
165+
166+
$funcCallName = $string->getAttribute(AttributeKey::FROM_FUNC_CALL_NAME);
167+
168+
return $funcCallName === 'is_a';
169+
}
193170
}

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ArgNodeVisitor.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr\Array_;
9-
use PhpParser\Node\Expr\ArrayDimFetch;
108
use PhpParser\Node\Expr\FuncCall;
119
use PhpParser\Node\Name;
1210
use PhpParser\NodeVisitorAbstract;
@@ -32,14 +30,7 @@ public function enterNode(Node $node): ?Node
3230

3331
$funcCallName = $node->name->toString();
3432
foreach ($node->getArgs() as $arg) {
35-
if ($arg->value instanceof Array_) {
36-
$arg->value->setAttribute(AttributeKey::FROM_FUNC_CALL_NAME, $funcCallName);
37-
continue;
38-
}
39-
40-
if ($arg->value instanceof ArrayDimFetch) {
41-
$arg->value->setAttribute(AttributeKey::FROM_FUNC_CALL_NAME, $funcCallName);
42-
}
33+
$arg->value->setAttribute(AttributeKey::FROM_FUNC_CALL_NAME, $funcCallName);
4334
}
4435

4536
return null;

0 commit comments

Comments
 (0)