Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rector] Refactor UnderscoreToCamelCaseVariableNameRector with latest Rector compatible code #4612

Merged
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
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()));
}
}