Skip to content

Commit

Permalink
Bump to php parser 4.18 (#5362)
Browse files Browse the repository at this point in the history
* bump to php parser 4.18

* Update rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php

---------

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
TomasVotruba and samsonasik committed Dec 16, 2023
1 parent 39cce5c commit 120e0cc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -21,7 +21,7 @@
"fidry/cpu-core-counter": "^0.5.1",
"illuminate/container": "^10.20",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.17.1",
"nikic/php-parser": "^4.18.0",
"ondram/ci-detector": "^4.1",
"phpstan/phpdoc-parser": "^1.23",
"phpstan/phpstan": "^1.10.35",
Expand Down
Expand Up @@ -13,6 +13,6 @@
'Exception',

// keep '\\' prefix string on string '\Foo\Bar'
StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => true
StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => true,
]);
};
96 changes: 48 additions & 48 deletions rules/DeadCode/Rector/ClassMethod/RemoveNullTagValueNodeRector.php
Expand Up @@ -75,6 +75,50 @@ public function getNodeTypes(): array
return [ClassMethod::class, Function_::class, Expression::class, Property::class];
}

/**
* @param ClassMethod|Function_|Expression|Property $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Expression || $node instanceof Property) {
return $this->processVarTagNull($node);
}

$phpdocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$removedParamNames = [];

foreach ($node->params as $param) {
$paramName = $this->getName($param);
$paramTagValueNode = $phpdocInfo->getParamTagValueByName($paramName);

if ($paramTagValueNode instanceof ParamTagValueNode && $this->isNull($paramTagValueNode)) {
$removedParamNames[] = $paramTagValueNode->parameterName;
}
}

$hasRemoved = false;
if ($removedParamNames !== []) {
$this->removeParamNullTag($phpdocInfo, $removedParamNames);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

$hasRemoved = true;
}

$returnTagValueNode = $phpdocInfo->getReturnTagValue();
if ($returnTagValueNode instanceof ReturnTagValueNode && $this->isNull($returnTagValueNode)) {
$phpdocInfo->removeByType(ReturnTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

$hasRemoved = true;
}

if (! $hasRemoved) {
return null;
}

return $node;
}

private function isNull(VarTagValueNode|ParamTagValueNode|ReturnTagValueNode $tag): bool
{
return $tag->type instanceof IdentifierTypeNode
Expand All @@ -91,7 +135,7 @@ private function removeParamNullTag(PhpDocInfo $phpDocInfo, array $paramNames):
$phpDocNodeTraverser->traverseWithCallable(
$phpDocInfo->getPhpDocNode(),
'',
static function (AstNode $astNode) use ($paramNames) : ?int {
static function (AstNode $astNode) use ($paramNames): ?int {
if (! $astNode instanceof PhpDocTagNode) {
return null;
}
Expand All @@ -100,12 +144,13 @@ static function (AstNode $astNode) use ($paramNames) : ?int {
return null;
}

if (in_array($astNode->value->parameterName , $paramNames, true)) {
if (in_array($astNode->value->parameterName, $paramNames, true)) {
return PhpDocNodeTraverser::NODE_REMOVE;
}

return null;
});
}
);
}

private function processVarTagNull(Expression|Property $node): ?Node
Expand All @@ -114,7 +159,6 @@ private function processVarTagNull(Expression|Property $node): ?Node
$varTagValueNode = $phpdocInfo->getVarTagValueNode();

if ($varTagValueNode instanceof VarTagValueNode && $this->isNull($varTagValueNode)) {

$phpdocInfo->removeByType(VarTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

Expand All @@ -123,48 +167,4 @@ private function processVarTagNull(Expression|Property $node): ?Node

return null;
}

/**
* @param ClassMethod|Function_|Expression|Property $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Expression || $node instanceof Property) {
return $this->processVarTagNull($node);
}

$phpdocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$removedParamNames = [];

foreach ($node->params as $param) {
$paramName = $this->getName($param);
$paramTagValueNode = $phpdocInfo->getParamTagValueByName($paramName);

if ($paramTagValueNode instanceof ParamTagValueNode && $this->isNull($paramTagValueNode)) {
$removedParamNames[] = $paramTagValueNode->parameterName;
}
}

$hasRemoved = false;
if ($removedParamNames !== []) {
$this->removeParamNullTag($phpdocInfo, $removedParamNames);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

$hasRemoved = true;
}

$returnTagValueNode = $phpdocInfo->getReturnTagValue();
if ($returnTagValueNode instanceof ReturnTagValueNode && $this->isNull($returnTagValueNode)) {
$phpdocInfo->removeByType(ReturnTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

$hasRemoved = true;
}

if (! $hasRemoved) {
return null;
}

return $node;
}
}
Expand Up @@ -28,6 +28,11 @@
*/
final class StringClassNameToClassConstantRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
{
/**
* @var string
*/
public const SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash';

/**
* @var string
*/
Expand All @@ -40,11 +45,6 @@ final class StringClassNameToClassConstantRector extends AbstractRector implemen

private bool $shouldKeepPreslash = false;

/**
* @var string
*/
public const SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash';

public function __construct(
private readonly ReflectionProvider $reflectionProvider,
) {
Expand Down Expand Up @@ -85,7 +85,7 @@ public function run()
[
'ClassName',
'AnotherClassName',
StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => false,
StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => false,
],
),
]);
Expand Down Expand Up @@ -159,7 +159,9 @@ public function refactor(Node $node): Concat|ClassConstFetch|null|int
*/
public function configure(array $configuration): void
{
if (isset($configuration[self::SHOULD_KEEP_PRE_SLASH]) && is_bool($configuration[self::SHOULD_KEEP_PRE_SLASH])) {
if (isset($configuration[self::SHOULD_KEEP_PRE_SLASH]) && is_bool(
$configuration[self::SHOULD_KEEP_PRE_SLASH]
)) {
$this->shouldKeepPreslash = $configuration[self::SHOULD_KEEP_PRE_SLASH];
unset($configuration[self::SHOULD_KEEP_PRE_SLASH]);
}
Expand Down

0 comments on commit 120e0cc

Please sign in to comment.