Skip to content

Commit ba15f07

Browse files
committed
[cleanup] deprecate SHOULD_KEEP_PRE_SLASH, remove dont traverse const on StringClassNameToClassConstantRector
1 parent 531d57e commit ba15f07

File tree

6 files changed

+19
-119
lines changed

6 files changed

+19
-119
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

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 & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +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\NodeVisitor;
1411
use PHPStan\Reflection\ReflectionProvider;
1512
use Rector\Contract\Rector\ConfigurableRectorInterface;
13+
use Rector\NodeTypeResolver\Node\AttributeKey;
1614
use Rector\Rector\AbstractRector;
1715
use Rector\ValueObject\PhpVersionFeature;
1816
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -26,6 +24,7 @@
2624
final class StringClassNameToClassConstantRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
2725
{
2826
/**
27+
* @deprecated since 2.2.12. Default behavior now.
2928
* @var string
3029
*/
3130
public const SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash';
@@ -35,8 +34,6 @@ final class StringClassNameToClassConstantRector extends AbstractRector implemen
3534
*/
3635
private array $classesToSkip = [];
3736

38-
private bool $shouldKeepPreslash = false;
39-
4037
public function __construct(
4138
private readonly ReflectionProvider $reflectionProvider,
4239
) {
@@ -74,11 +71,7 @@ public function run()
7471
}
7572
CODE_SAMPLE
7673
,
77-
[
78-
'ClassName',
79-
'AnotherClassName',
80-
self::SHOULD_KEEP_PRE_SLASH => false,
81-
],
74+
['ClassName', 'AnotherClassName'],
8275
),
8376
]);
8477
}
@@ -88,21 +81,15 @@ public function run()
8881
*/
8982
public function getNodeTypes(): array
9083
{
91-
return [String_::class, FuncCall::class];
84+
return [String_::class];
9285
}
9386

9487
/**
95-
* @param String_|FuncCall $node
96-
* @return Concat|ClassConstFetch|null|NodeVisitor::DONT_TRAVERSE_CHILDREN
88+
* @param String_ $node
9789
*/
98-
public function refactor(Node $node): Concat|ClassConstFetch|null|int
90+
public function refactor(Node $node): ClassConstFetch|null
9991
{
100-
// keep allowed string as condition
101-
if ($node instanceof FuncCall) {
102-
if ($this->isName($node, 'is_a')) {
103-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
104-
}
105-
92+
if ($this->shouldSkipIsA($node)) {
10693
return null;
10794
}
10895

@@ -119,14 +106,6 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int
119106
}
120107

121108
$fullyQualified = new FullyQualified($classLikeName);
122-
if ($this->shouldKeepPreslash && $classLikeName !== $node->value) {
123-
$preSlashCount = strlen($node->value) - strlen($classLikeName);
124-
$preSlash = str_repeat('\\', $preSlashCount);
125-
$string = new String_($preSlash);
126-
127-
return new Concat($string, new ClassConstFetch($fullyQualified, 'class'));
128-
}
129-
130109
return new ClassConstFetch($fullyQualified, 'class');
131110
}
132111

@@ -135,13 +114,6 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int
135114
*/
136115
public function configure(array $configuration): void
137116
{
138-
if (isset($configuration[self::SHOULD_KEEP_PRE_SLASH]) && is_bool(
139-
$configuration[self::SHOULD_KEEP_PRE_SLASH]
140-
)) {
141-
$this->shouldKeepPreslash = $configuration[self::SHOULD_KEEP_PRE_SLASH];
142-
unset($configuration[self::SHOULD_KEEP_PRE_SLASH]);
143-
}
144-
145117
Assert::allString($configuration);
146118

147119
$this->classesToSkip = $configuration;
@@ -184,4 +156,15 @@ private function shouldSkip(string $classLikeName): bool
184156

185157
return false;
186158
}
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+
}
187170
}

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)