Skip to content

Commit 0859f20

Browse files
authored
Merge pull request #5271 from samsonasik/refactor-underscore-to-camelcase-variable-name
[Rector] Refactor UnderscoreToCamelCaseVariableNameRector so no longer require symplify/package-builder
2 parents b8c1939 + efb3b4d commit 0859f20

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"phpstan/phpstan": "^0.12.91",
2525
"phpunit/phpunit": "^9.1",
2626
"predis/predis": "^1.1",
27-
"rector/rector": "0.11.60",
28-
"symplify/package-builder": "^9.3"
27+
"rector/rector": "0.11.60"
2928
},
3029
"suggest": {
3130
"ext-fileinfo": "Improves mime type detection for files"

rector.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@
117117
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_73);
118118

119119
$services = $containerConfigurator->services();
120-
$services->load('Symplify\\PackageBuilder\\', __DIR__ . '/vendor/symplify/package-builder/src');
121-
122120
$services->set(UnderscoreToCamelCaseVariableNameRector::class);
123121
$services->set(SimplifyUselessVariableRector::class);
124122
$services->set(RemoveAlwaysElseRector::class);

utils/Rector/UnderscoreToCamelCaseVariableNameRector.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@
1313

1414
namespace Utils\Rector;
1515

16-
use Nette\Utils\Strings;
1716
use PhpParser\Comment\Doc;
1817
use PhpParser\Node;
1918
use PhpParser\Node\Expr\Variable;
2019
use PhpParser\Node\Stmt\ClassMethod;
2120
use PhpParser\Node\Stmt\Function_;
2221
use Rector\Core\Php\ReservedKeywordAnalyzer;
2322
use Rector\Core\Rector\AbstractRector;
24-
use Rector\NodeTypeResolver\Node\AttributeKey;
25-
use Symplify\PackageBuilder\Strings\StringFormatConverter;
23+
use Rector\NodeNestingScope\ParentFinder;
2624
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2725
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2826

@@ -43,16 +41,16 @@ final class UnderscoreToCamelCaseVariableNameRector extends AbstractRector
4341
private $reservedKeywordAnalyzer;
4442

4543
/**
46-
* @var StringFormatConverter
44+
* @var ParentFinder
4745
*/
48-
private $stringFormatConverter;
46+
private $parentFinder;
4947

5048
public function __construct(
5149
ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
52-
StringFormatConverter $stringFormatConverter
50+
ParentFinder $parentFinder
5351
) {
5452
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
55-
$this->stringFormatConverter = $stringFormatConverter;
53+
$this->parentFinder = $parentFinder;
5654
}
5755

5856
public function getRuleDefinition(): RuleDefinition
@@ -100,19 +98,20 @@ public function refactor(Node $node): ?Node
10098
return null;
10199
}
102100

103-
if (! Strings::contains($nodeName, '_')) {
104-
return null;
105-
}
106-
107101
if ($this->reservedKeywordAnalyzer->isNativeVariable($nodeName)) {
108102
return null;
109103
}
110104

111-
if ($nodeName[0] === '_') {
105+
$underscorePosition = strpos($nodeName, '_');
106+
// underscore not found, or in the first char, skip
107+
if ((int) $underscorePosition === 0) {
112108
return null;
113109
}
114110

115-
$camelCaseName = $this->stringFormatConverter->underscoreAndHyphenToCamelCase($nodeName);
111+
$replaceUnderscoreToSpace = str_replace('_', ' ', $nodeName);
112+
$uppercaseFirstChar = ucwords($replaceUnderscoreToSpace);
113+
$camelCaseName = lcfirst(str_replace(' ', '', $uppercaseFirstChar));
114+
116115
if ($camelCaseName === 'this') {
117116
return null;
118117
}
@@ -125,24 +124,13 @@ public function refactor(Node $node): ?Node
125124

126125
private function updateDocblock(Variable $variable, string $variableName, string $camelCaseName): void
127126
{
128-
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
129-
130-
while ($parentNode) {
131-
/**
132-
* @var ClassMethod|Function_ $parentNode
133-
*/
134-
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
135-
136-
if ($parentNode instanceof ClassMethod || $parentNode instanceof Function_) {
137-
break;
138-
}
139-
}
127+
$parentClassMethodOrFunction = $this->parentFinder->findByTypes($variable, [ClassMethod::class, Function_::class]);
140128

141-
if ($parentNode === null) {
129+
if ($parentClassMethodOrFunction === null) {
142130
return;
143131
}
144132

145-
$docComment = $parentNode->getDocComment();
133+
$docComment = $parentClassMethodOrFunction->getDocComment();
146134
if ($docComment === null) {
147135
return;
148136
}
@@ -152,11 +140,11 @@ private function updateDocblock(Variable $variable, string $variableName, string
152140
return;
153141
}
154142

155-
if (! Strings::match($docCommentText, sprintf(self::PARAM_NAME_REGEX, $variableName))) {
143+
if (! preg_match(sprintf(self::PARAM_NAME_REGEX, $variableName), $docCommentText)) {
156144
return;
157145
}
158146

159-
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($parentNode);
147+
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($parentClassMethodOrFunction);
160148
$paramTagValueNodes = $phpDocInfo->getParamTagValueNodes();
161149

162150
foreach ($paramTagValueNodes as $paramTagValueNode) {
@@ -166,6 +154,6 @@ private function updateDocblock(Variable $variable, string $variableName, string
166154
}
167155
}
168156

169-
$parentNode->setDocComment(new Doc($phpDocInfo->getPhpDocNode()->__toString()));
157+
$parentClassMethodOrFunction->setDocComment(new Doc($phpDocInfo->getPhpDocNode()->__toString()));
170158
}
171159
}

0 commit comments

Comments
 (0)