Skip to content

Commit

Permalink
Refactor RenameVariableToMatchMethodCallReturnTypeRector to StmtsAwar…
Browse files Browse the repository at this point in the history
…eInterface (#3977)
  • Loading branch information
TomasVotruba committed May 26, 2023
1 parent bb3d37a commit 33d05d3
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 249 deletions.
14 changes: 8 additions & 6 deletions config/set/naming.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RenameParamToMatchTypeRector::class);
$rectorConfig->rule(RenamePropertyToMatchTypeRector::class);
$rectorConfig->rule(RenameVariableToMatchNewTypeRector::class);
$rectorConfig->rule(RenameVariableToMatchMethodCallReturnTypeRector::class);
$rectorConfig->rule(RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class);
$rectorConfig->rule(RenameForeachValueVariableToMatchExprVariableRector::class);
$rectorConfig->rules([
RenameParamToMatchTypeRector::class,
RenamePropertyToMatchTypeRector::class,
RenameVariableToMatchNewTypeRector::class,
RenameVariableToMatchMethodCallReturnTypeRector::class,
RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class,
RenameForeachValueVariableToMatchExprVariableRector::class,
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public function changeTypeInAnnotationTypes(Node $node, PhpDocInfo $phpDocInfo,
*/
private function processAssertChoiceTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo): void
{
$assertChoiceTagValueNode = $phpDocInfo->findOneByAnnotationClass(
$assertChoiceDoctrineAnnotationTagValueNode = $phpDocInfo->findOneByAnnotationClass(
'Symfony\Component\Validator\Constraints\Choice'
);
if (! $assertChoiceTagValueNode instanceof DoctrineAnnotationTagValueNode) {
if (! $assertChoiceDoctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
return;
}

$callbackArrayItemNode = $assertChoiceTagValueNode->getValue('callback');
$callbackArrayItemNode = $assertChoiceDoctrineAnnotationTagValueNode->getValue('callback');
if (! $callbackArrayItemNode instanceof ArrayItemNode) {
return;
}
Expand Down
38 changes: 19 additions & 19 deletions packages/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ public function changeVarType(PhpDocInfo $phpDocInfo, Type $newType): void
}

// override existing type
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newPHPStanPhpDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newType,
TypeKind::PROPERTY
);

$currentVarTagValueNode = $phpDocInfo->getVarTagValueNode();
if ($currentVarTagValueNode instanceof VarTagValueNode) {
// only change type
$currentVarTagValueNode->type = $newPHPStanPhpDocType;
$currentVarTagValueNode->type = $newPHPStanPhpDocTypeNode;
$phpDocInfo->markAsChanged();
} else {
// add completely new one
$varTagValueNode = new VarTagValueNode($newPHPStanPhpDocType, '', '');
$varTagValueNode = new VarTagValueNode($newPHPStanPhpDocTypeNode, '', '');

$phpDocInfo->addTagValueNode($varTagValueNode);
}
Expand All @@ -121,7 +121,7 @@ public function changeReturnType(PhpDocInfo $phpDocInfo, Type $newType): bool
}

// override existing type
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newPHPStanPhpDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newType,
TypeKind::RETURN
);
Expand All @@ -130,11 +130,11 @@ public function changeReturnType(PhpDocInfo $phpDocInfo, Type $newType): bool

if ($currentReturnTagValueNode instanceof ReturnTagValueNode) {
// only change type
$currentReturnTagValueNode->type = $newPHPStanPhpDocType;
$currentReturnTagValueNode->type = $newPHPStanPhpDocTypeNode;
$phpDocInfo->markAsChanged();
} else {
// add completely new one
$returnTagValueNode = new ReturnTagValueNode($newPHPStanPhpDocType, '');
$returnTagValueNode = new ReturnTagValueNode($newPHPStanPhpDocTypeNode, '');
$phpDocInfo->addTagValueNode($returnTagValueNode);
}

Expand All @@ -152,7 +152,7 @@ public function changeParamType(PhpDocInfo $phpDocInfo, Type $newType, Param $pa
return;
}

$phpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::PARAM);
$phpDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::PARAM);
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);

// override existing type
Expand All @@ -172,10 +172,10 @@ public function changeParamType(PhpDocInfo $phpDocInfo, Type $newType, Param $pa
return;
}

$paramTagValueNode->type = $phpDocType;
$paramTagValueNode->type = $phpDocTypeNode;
$phpDocInfo->markAsChanged();
} else {
$paramTagValueNode = $this->paramPhpDocNodeFactory->create($phpDocType, $param);
$paramTagValueNode = $this->paramPhpDocNodeFactory->create($phpDocTypeNode, $param);
$phpDocInfo->addTagValueNode($paramTagValueNode);
}
}
Expand Down Expand Up @@ -212,13 +212,13 @@ public function copyPropertyDocToParam(Property $property, Param $param): void
return;
}

$varTag = $phpDocInfo->getVarTagValueNode();
if (! $varTag instanceof VarTagValueNode) {
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
$this->processKeepComments($property, $param);
return;
}

if ($varTag->description !== '') {
if ($varTagValueNode->description !== '') {
return;
}

Expand All @@ -229,7 +229,7 @@ public function copyPropertyDocToParam(Property $property, Param $param): void
return;
}

if (! $this->isAllowed($varTag->type)) {
if (! $this->isAllowed($varTagValueNode->type)) {
return;
}

Expand All @@ -241,7 +241,7 @@ public function copyPropertyDocToParam(Property $property, Param $param): void
$param->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);

$phpDocInfo = $parentNode->getAttribute(AttributeKey::PHP_DOC_INFO);
$paramType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($varTag, $property);
$paramType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($varTagValueNode, $property);

$this->changeParamType($phpDocInfo, $paramType, $param, $paramVarName);
$this->processKeepComments($property, $param);
Expand All @@ -260,22 +260,22 @@ public function changeVarTypeNode(PhpDocInfo $phpDocInfo, TypeNode $typeNode): v
private function processKeepComments(Property $property, Param $param): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($param);
$varTag = $phpDocInfo->getVarTagValueNode();
$varTagValueNode = $phpDocInfo->getVarTagValueNode();

$toBeRemoved = ! $varTag instanceof VarTagValueNode;
$toBeRemoved = ! $varTagValueNode instanceof VarTagValueNode;
$this->commentsMerger->keepComments($param, [$property]);

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($param);
$varTag = $phpDocInfo->getVarTagValueNode();
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $toBeRemoved) {
return;
}

if (! $varTag instanceof VarTagValueNode) {
if (! $varTagValueNode instanceof VarTagValueNode) {
return;
}

if ($varTag->description !== '') {
if ($varTagValueNode->description !== '') {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function processNodes(
if ($node instanceof Trait_) {
$traitName = $this->resolveClassName($node);

$traitReflectionClass = $this->reflectionProvider->getClass($traitName);
$traitClassReflection = $this->reflectionProvider->getClass($traitName);

$traitScope = clone $mutatingScope;

Expand All @@ -194,7 +194,7 @@ public function processNodes(
$this->privatesAccessor->setPrivatePropertyOfClass(
$traitContext,
'classReflection',
$traitReflectionClass,
$traitClassReflection,
ClassReflection::class
);
$this->privatesAccessor->setPrivatePropertyOfClass(
Expand Down
15 changes: 9 additions & 6 deletions packages/PhpAttribute/UseAliasNameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@ public function match(
foreach ($uses as $use) {
foreach ($use->uses as $useUse) {
// we need to use original use statement
$originalUseUse = $useUse->getAttribute(AttributeKey::ORIGINAL_NODE);
if (! $originalUseUse instanceof UseUse) {
$originalUseUseNode = $useUse->getAttribute(AttributeKey::ORIGINAL_NODE);
if (! $originalUseUseNode instanceof UseUse) {
continue;
}

if (! $originalUseUse->alias instanceof Identifier) {
if (! $originalUseUseNode->alias instanceof Identifier) {
continue;
}

$alias = $originalUseUse->alias->toString();
$alias = $originalUseUseNode->alias->toString();
if (! str_starts_with($shortAnnotationName, $alias)) {
continue;
}

$fullyQualifiedAnnotationName = $originalUseUse->name->toString() . ltrim($shortAnnotationName, $alias);
$fullyQualifiedAnnotationName = $originalUseUseNode->name->toString() . ltrim(
$shortAnnotationName,
$alias
);
if ($fullyQualifiedAnnotationName !== $annotationToAttribute->getTag()) {
continue;
}
Expand All @@ -57,7 +60,7 @@ public function match(
}

// now we now we are matching correct contanct and old and new have the same number of parts
$useImportPartCount = substr_count($originalUseUse->name->toString(), '\\') + 1;
$useImportPartCount = substr_count($originalUseUseNode->name->toString(), '\\') + 1;
$newAttributeImportPart = array_slice($attributeParts, 0, $useImportPartCount);
$newAttributeImport = implode('\\', $newAttributeImportPart);

Expand Down
8 changes: 4 additions & 4 deletions packages/ReadWrite/Guard/VariableToConstantGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function __construct(

public function isReadArg(Arg $arg): bool
{
$parentParent = $arg->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentParent instanceof FuncCall) {
$parentParentNode = $arg->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentParentNode instanceof FuncCall) {
return true;
}

$functionNameString = $this->nodeNameResolver->getName($parentParent);
$functionNameString = $this->nodeNameResolver->getName($parentParentNode);
if ($functionNameString === null) {
return true;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public function isReadArg(Arg $arg): bool
return true;
}

$argumentPosition = $this->getArgumentPosition($parentParent, $arg);
$argumentPosition = $this->getArgumentPosition($parentParentNode, $arg);
return ! in_array($argumentPosition, $referenceParametersPositions, true);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Tests\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\Fixture;

use PhpParser\Node\Expr\ClassConstFetch;

final class DoubleMethodCall
{
public function run()
{
$magicGet = $this->createClassConstFetch('SomeClass', 'MAGIC_GET');
echo $magicGet;

$magicSet = $this->createClassConstFetch('SomeClass', 'MAGIC_SET');
echo $magicSet;
}

protected function createClassConstFetch(string $class, string $constant): ClassConstFetch
{
return new ClassConstFetch($class, $constant);
}
}

?>
-----
<?php

namespace Rector\Tests\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\Fixture;

use PhpParser\Node\Expr\ClassConstFetch;

final class DoubleMethodCall
{
public function run()
{
$classConstFetch = $this->createClassConstFetch('SomeClass', 'MAGIC_GET');
echo $classConstFetch;

$magicSet = $this->createClassConstFetch('SomeClass', 'MAGIC_SET');
echo $magicSet;
}

protected function createClassConstFetch(string $class, string $constant): ClassConstFetch
{
return new ClassConstFetch($class, $constant);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\Fixture;

use Rector\Tests\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\Source\SameNameMethod;

final class SkipArrowFunction
{
public function run(SameNameMethod $sameNameMethod)
{
$fn1 = fn($x) => $x + $stamp = $sameNameMethod->getName();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ private function isPrivateConstant(ClassConstFetch $classConstFetch, Class_ $cla

private function isUsedInPrivateMethod(ClassConstFetch $classConstFetch): bool
{
$method = $this->betterNodeFinder->findParentType($classConstFetch, ClassMethod::class);

if (! $method instanceof ClassMethod) {
$classMethod = $this->betterNodeFinder->findParentType($classConstFetch, ClassMethod::class);
if (! $classMethod instanceof ClassMethod) {
return false;
}

return $method->flags === Class_::MODIFIER_PRIVATE;
return $classMethod->flags === Class_::MODIFIER_PRIVATE;
}

private function shouldBeSkipped(Class_ $class, ClassConstFetch $classConstFetch): bool
Expand Down

0 comments on commit 33d05d3

Please sign in to comment.