Skip to content

Commit

Permalink
Improve Use_ const types in docblocks (#5005)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 12, 2023
1 parent 7e2bc14 commit 848c2ad
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ public function __construct(
*
* @param string[] $oldToNewClasses
*/
public function changeTypeInAnnotationTypes(Node $node, PhpDocInfo $phpDocInfo, array $oldToNewClasses, bool &$hasChanged): bool
{
public function changeTypeInAnnotationTypes(
Node $node,
PhpDocInfo $phpDocInfo,
array $oldToNewClasses,
bool &$hasChanged
): bool {
$this->processAssertChoiceTagValueNode($oldToNewClasses, $phpDocInfo, $hasChanged);
$this->processDoctrineRelationTagValueNode($node, $oldToNewClasses, $phpDocInfo, $hasChanged);
$this->processSerializerTypeTagValueNode($oldToNewClasses, $phpDocInfo, $hasChanged);
Expand All @@ -39,8 +43,11 @@ public function changeTypeInAnnotationTypes(Node $node, PhpDocInfo $phpDocInfo,
/**
* @param array<string, string> $oldToNewClasses
*/
private function processAssertChoiceTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo, bool &$hasChanged): void
{
private function processAssertChoiceTagValueNode(
array $oldToNewClasses,
PhpDocInfo $phpDocInfo,
bool &$hasChanged
): void {
$assertChoiceDoctrineAnnotationTagValueNode = $phpDocInfo->findOneByAnnotationClass(
'Symfony\Component\Validator\Constraints\Choice'
);
Expand Down Expand Up @@ -107,8 +114,11 @@ private function processDoctrineRelationTagValueNode(
/**
* @param array<string, string> $oldToNewClasses
*/
private function processSerializerTypeTagValueNode(array $oldToNewClasses, PhpDocInfo $phpDocInfo, bool &$hasChanged): void
{
private function processSerializerTypeTagValueNode(
array $oldToNewClasses,
PhpDocInfo $phpDocInfo,
bool &$hasChanged
): void {
$doctrineAnnotationTagValueNode = $phpDocInfo->findOneByAnnotationClass('JMS\Serializer\Annotation\Type');
if (! $doctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ public function getFullyQualifiedName(): string
return $this->fullyQualifiedClass;
}

public function getUseNode(?int $useType = null): Use_
/**
* @param Use_::TYPE_* $useType
*/
public function getUseNode(int $useType): Use_
{
$name = new Name($this->fullyQualifiedClass);
$name->setAttribute(AttributeKey::IS_USEUSE_NAME, true);

$useUse = new UseUse($name, $this->getClassName());

$use = new Use_([$useUse]);
if ($useType !== null) {
$use->type = $useType;
}
$use->type = $useType;

return $use;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ public function getShortNameNode(): Name
return $name;
}

public function getUseNode(?int $useType = null): Use_
/**
* @param Use_::TYPE_* $useType
*/
public function getUseNode(int $useType): Use_
{
$name = new Name($this->getClassName());
$name->setAttribute(AttributeKey::IS_USEUSE_NAME, true);

$useUse = new UseUse($name);

$use = new Use_([$useUse]);
if ($useType !== null) {
$use->type = $useType;
}
$use->type = $useType;

return $use;
}
Expand All @@ -67,9 +68,4 @@ public function getShortNameLowered(): string
{
return strtolower($this->getShortName());
}

public function getClassNameLowered(): string
{
return strtolower($this->getClassName());
}
}
3 changes: 3 additions & 0 deletions rules/CodingStyle/Application/UseImportsAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,16 @@ private function createUses(
?string $namespaceName
): array {
$newUses = [];

/** @var array<Use_::TYPE_*, array<AliasedObjectType|FullyQualifiedObjectType>> $importsMapping */
$importsMapping = [
Use_::TYPE_NORMAL => $useImportTypes,
Use_::TYPE_CONSTANT => $constantUseImportTypes,
Use_::TYPE_FUNCTION => $functionUseImportTypes,
];

foreach ($importsMapping as $type => $importTypes) {
/** @var AliasedObjectType|FullyQualifiedObjectType $importType */
foreach ($importTypes as $importType) {
if ($namespaceName !== null && $this->isCurrentNamespace($namespaceName, $importType)) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion rules/CodingStyle/ClassNameImport/AliasUsesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Rector\CodingStyle\ClassNameImport;

use PhpParser\Node\Stmt\Use_;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;

final class AliasUsesResolver
Expand Down
3 changes: 1 addition & 2 deletions rules/CodingStyle/ClassNameImport/UsedImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Rector\CodingStyle\ClassNameImport;

use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use Rector\CodingStyle\ClassNameImport\ValueObject\UsedImports;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand Down Expand Up @@ -68,5 +68,4 @@ public function resolveForStmts(array $stmts): UsedImports

return new UsedImports($usedImports, $usedFunctionImports, $usedConstImports);
}

}
16 changes: 10 additions & 6 deletions rules/CodingStyle/ClassNameImport/ValueObject/UsedImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,40 @@
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

final class UsedImports {
final class UsedImports
{
/**
* @param array<FullyQualifiedObjectType|AliasedObjectType> $useImports
* @param FullyQualifiedObjectType[] $functionImports
* @param FullyQualifiedObjectType[] $constantImports
*/
public function __construct(private readonly array $useImports, private readonly array $functionImports, private readonly array $constantImports)
{
public function __construct(
private readonly array $useImports,
private readonly array $functionImports,
private readonly array $constantImports
) {
}

/**
* @return array<FullyQualifiedObjectType|AliasedObjectType>
*/
public function getUseImports() : array
public function getUseImports(): array
{
return $this->useImports;
}

/**
* @return FullyQualifiedObjectType[]
*/
public function getFunctionImports() : array
public function getFunctionImports(): array
{
return $this->functionImports;
}

/**
* @return FullyQualifiedObjectType[]
*/
public function getConstantImports() : array
public function getConstantImports(): array
{
return $this->constantImports;
}
Expand Down
50 changes: 25 additions & 25 deletions rules/Naming/PropertyRenamer/PropertyPromotionRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ public function renamePropertyPromotion(Class_|Interface_ $classLike): bool
return $hasChanged;
}

public function renameParamDoc(
PhpDocInfo $phpDocInfo,
ClassMethod $classMethod,
Param $param,
string $paramVarName,
string $desiredPropertyName
): void {
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramVarName);
if (! $paramTagValueNode instanceof ParamTagValueNode) {
return;
}

$paramRename = $this->paramRenameFactory->createFromResolvedExpectedName(
$classMethod,
$param,
$desiredPropertyName
);
if (! $paramRename instanceof ParamRename) {
return;
}

$this->paramRenamer->rename($paramRename);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
}

private function renameParamVarNameAndVariableUsage(
ClassLike $classLike,
ClassMethod $classMethod,
Expand Down Expand Up @@ -111,31 +136,6 @@ private function renameParamVarNameAndVariableUsage(
$this->variableRenamer->renameVariableInFunctionLike($classMethod, $paramVarName, $desiredPropertyName);
}

public function renameParamDoc(
PhpDocInfo $phpDocInfo,
ClassMethod $classMethod,
Param $param,
string $paramVarName,
string $desiredPropertyName
): void {
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramVarName);
if (! $paramTagValueNode instanceof ParamTagValueNode) {
return;
}

$paramRename = $this->paramRenameFactory->createFromResolvedExpectedName(
$classMethod,
$param,
$desiredPropertyName
);
if (! $paramRename instanceof ParamRename) {
return;
}

$this->paramRenamer->rename($paramRename);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
}

/**
* Sometimes the bare type is not enough.
* This allows prefixing type in variable names, e.g. "Type $firstType"
Expand Down
2 changes: 1 addition & 1 deletion rules/Php80/DocBlock/PropertyPromotionDocBlockMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function mergePropertyAndParamDocBlocks(
$param->setAttribute(AttributeKey::COMMENTS, $mergedComments);
}
}

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($param);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\TypeCombinator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -189,7 +188,13 @@ public function refactor(Node $node): ?Node
$paramName
);
} elseif ($paramTagValueNode->parameterName !== '$' . $propertyName) {
$this->propertyPromotionRenamer->renameParamDoc($constructorPhpDocInfo, $constructClassMethod, $param, $paramTagValueNode->parameterName, $propertyName);
$this->propertyPromotionRenamer->renameParamDoc(
$constructorPhpDocInfo,
$constructClassMethod,
$param,
$paramTagValueNode->parameterName,
$propertyName
);
}

// property name has higher priority
Expand Down
7 changes: 6 additions & 1 deletion rules/Renaming/NodeManipulator/ClassRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ private function refactorPhpDoc(
}

$hasChanged = $this->docBlockClassRenamer->renamePhpDocType($phpDocInfo, $oldToNewTypes);
$hasChanged = $this->phpDocClassRenamer->changeTypeInAnnotationTypes($node, $phpDocInfo, $oldToNewClasses, $hasChanged);
$hasChanged = $this->phpDocClassRenamer->changeTypeInAnnotationTypes(
$node,
$phpDocInfo,
$oldToNewClasses,
$hasChanged
);

if ($hasChanged) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
Expand Down

0 comments on commit 848c2ad

Please sign in to comment.