Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"phpstan/phpstan": "^0.12.91",
"phpunit/phpunit": "^9.1",
"predis/predis": "^1.1",
"rector/rector": "0.11.60",
"symplify/package-builder": "^9.3"
"rector/rector": "0.11.60"
},
"suggest": {
"ext-fileinfo": "Improves mime type detection for files"
Expand Down
2 changes: 0 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_73);

$services = $containerConfigurator->services();
$services->load('Symplify\\PackageBuilder\\', __DIR__ . '/vendor/symplify/package-builder/src');

$services->set(UnderscoreToCamelCaseVariableNameRector::class);
$services->set(SimplifyUselessVariableRector::class);
$services->set(RemoveAlwaysElseRector::class);
Expand Down
48 changes: 18 additions & 30 deletions utils/Rector/UnderscoreToCamelCaseVariableNameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@

namespace Utils\Rector;

use Nette\Utils\Strings;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Php\ReservedKeywordAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Strings\StringFormatConverter;
use Rector\NodeNestingScope\ParentFinder;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

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

/**
* @var StringFormatConverter
* @var ParentFinder
*/
private $stringFormatConverter;
private $parentFinder;

public function __construct(
ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
StringFormatConverter $stringFormatConverter
ParentFinder $parentFinder
) {
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
$this->stringFormatConverter = $stringFormatConverter;
$this->parentFinder = $parentFinder;
}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -100,19 +98,20 @@ public function refactor(Node $node): ?Node
return null;
}

if (! Strings::contains($nodeName, '_')) {
return null;
}

if ($this->reservedKeywordAnalyzer->isNativeVariable($nodeName)) {
return null;
}

if ($nodeName[0] === '_') {
$underscorePosition = strpos($nodeName, '_');
// underscore not found, or in the first char, skip
if ((int) $underscorePosition === 0) {
return null;
}

$camelCaseName = $this->stringFormatConverter->underscoreAndHyphenToCamelCase($nodeName);
$replaceUnderscoreToSpace = str_replace('_', ' ', $nodeName);
$uppercaseFirstChar = ucwords($replaceUnderscoreToSpace);
$camelCaseName = lcfirst(str_replace(' ', '', $uppercaseFirstChar));

if ($camelCaseName === 'this') {
return null;
}
Expand All @@ -125,24 +124,13 @@ public function refactor(Node $node): ?Node

private function updateDocblock(Variable $variable, string $variableName, string $camelCaseName): void
{
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);

while ($parentNode) {
/**
* @var ClassMethod|Function_ $parentNode
*/
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);

if ($parentNode instanceof ClassMethod || $parentNode instanceof Function_) {
break;
}
}
$parentClassMethodOrFunction = $this->parentFinder->findByTypes($variable, [ClassMethod::class, Function_::class]);

if ($parentNode === null) {
if ($parentClassMethodOrFunction === null) {
return;
}

$docComment = $parentNode->getDocComment();
$docComment = $parentClassMethodOrFunction->getDocComment();
if ($docComment === null) {
return;
}
Expand All @@ -152,11 +140,11 @@ private function updateDocblock(Variable $variable, string $variableName, string
return;
}

if (! Strings::match($docCommentText, sprintf(self::PARAM_NAME_REGEX, $variableName))) {
if (! preg_match(sprintf(self::PARAM_NAME_REGEX, $variableName), $docCommentText)) {
return;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($parentNode);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($parentClassMethodOrFunction);
$paramTagValueNodes = $phpDocInfo->getParamTagValueNodes();

foreach ($paramTagValueNodes as $paramTagValueNode) {
Expand All @@ -166,6 +154,6 @@ private function updateDocblock(Variable $variable, string $variableName, string
}
}

$parentNode->setDocComment(new Doc($phpDocInfo->getPhpDocNode()->__toString()));
$parentClassMethodOrFunction->setDocComment(new Doc($phpDocInfo->getPhpDocNode()->__toString()));
}
}