Skip to content

Commit

Permalink
Merge pull request #4612 from samsonasik/refactor-underscore-to-camel…
Browse files Browse the repository at this point in the history
…case

[Rector] Refactor UnderscoreToCamelCaseVariableNameRector with latest Rector compatible code
  • Loading branch information
samsonasik authored Apr 25, 2021
2 parents f66c545 + d5155f3 commit b20fa0d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
- 'system/**'
- '.github/workflows/test-rector.yml'
- composer.json
- 'utils/Rector'
- 'utils/Rector/**'
- 'rector.php'
push:
branches:
Expand All @@ -23,7 +23,7 @@ on:
- 'system/**'
- '.github/workflows/test-rector.yml'
- composer.json
- 'utils/Rector'
- 'utils/Rector/**'
- 'rector.php'

jobs:
Expand Down
42 changes: 25 additions & 17 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,7 +109,7 @@ public function refactor(Node $node): ?Node
return null;
}

$camelCaseName = StaticRectorStrings::underscoreToCamelCase($nodeName);
$camelCaseName = $this->stringFormatConverter->underscoreAndHyphenToCamelCase($nodeName);
if ($camelCaseName === 'this')
{
return null;
Expand Down Expand Up @@ -153,15 +153,23 @@ 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;
break;
}
}

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

0 comments on commit b20fa0d

Please sign in to comment.