Skip to content

Commit

Permalink
[Rector] Refactor UnderscoreToCamelCaseVariableNameRector with latest…
Browse files Browse the repository at this point in the history
… compatible code
  • Loading branch information
samsonasik committed Apr 25, 2021
1 parent b967d9e commit 6bfce3b
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions utils/Rector/UnderscoreToCamelCaseVariableNameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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\BetterPhpDocParser\PhpDocManipulator\PropertyDocBlockManipulator;
use Rector\Core\Php\ReservedKeywordAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StaticRectorStrings;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Strings\StringFormatConverter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,22 +30,22 @@ final class UnderscoreToCamelCaseVariableNameRector extends AbstractRector
private const PARAM_NAME_REGEX = '#(?<paramPrefix>@param\s.*\s+\$)(?<paramName>%s)#ms';

/**
* @var PropertyDocBlockManipulator
* @var ReservedKeywordAnalyzer
*/
private $propertyDocBlockManipulator;
private $reservedKeywordAnalyzer;

/**
* @var ReservedKeywordAnalyzer
* @var StringFormatConverter
*/
private $reservedKeywordAnalyzer;
private $stringFormatConverter;

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

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -109,14 +109,14 @@ public function refactor(Node $node): ?Node
return null;
}

$camelCaseName = StaticRectorStrings::underscoreToCamelCase($nodeName);
$camelCaseName = $this->stringFormatConverter->underscoreAndHyphenToCamelCase($nodeName);
if ($camelCaseName === 'this')
{
return null;
}

$node->name = $camelCaseName;
$this->updateDocblock($node, $nodeName, $camelCaseName);
$node->name = $camelCaseName;

return $node;
}
Expand Down Expand Up @@ -153,15 +153,22 @@ private function updateDocblock(Variable $variable, string $variableName, string
return;
}

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

$this->propertyDocBlockManipulator->renameParameterNameInDocBlock(
$parentNode,
$match['paramName'],
$camelCaseName
);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($parentNode);
$paramTagValueNodes = $phpDocInfo->getParamTagValueNodes();

foreach ($paramTagValueNodes as $paramTagValueNode)
{
if ($paramTagValueNode->parameterName === '$' . $variableName)
{
$paramTagValueNode->parameterName = '$' . $camelCaseName;
}
}

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

0 comments on commit 6bfce3b

Please sign in to comment.