Skip to content

Commit

Permalink
[ci-review] Rector Rectify
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jul 3, 2022
1 parent 0637894 commit 4521c8f
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 68 deletions.
3 changes: 1 addition & 2 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,7 @@ private function addClassMethodParam(

$param = new Param(new Variable($argumentName), BuilderHelpers::normalizeValue($defaultValue));
if ($type !== null) {
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM);
$param->type = $typeNode;
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM);
}

$classMethod->params[$position] = $param;
Expand Down
3 changes: 1 addition & 2 deletions rules/CodeQuality/NodeAnalyzer/LocalPropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public function resolveFetchedPropertiesToTypesFromClass(Class_ $class): array
return null;
}

$propertyFetchType = $this->resolvePropertyFetchType($node);
$fetchedLocalPropertyNameToTypes[$propertyName][] = $propertyFetchType;
$fetchedLocalPropertyNameToTypes[$propertyName][] = $this->resolvePropertyFetchType($node);

return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
return null;
}

$newParams = $this->argumentSorter->sortArgsByExpectedParamOrder(
$classMethod->params = $this->argumentSorter->sortArgsByExpectedParamOrder(
$classMethod->params,
$expectedArgOrParamOrder
);

$classMethod->params = $newParams;

return $classMethod;
}

Expand Down
9 changes: 6 additions & 3 deletions rules/CodeQuality/Rector/For_/ForToForeachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public function refactor(Node $node): ?Node

private function processForToForeach(For_ $for, string $iteratedVariable): ?Foreach_
{
$originalVariableSingle = $this->inflector->singularize($iteratedVariable);
$iteratedVariableSingle = $originalVariableSingle;
$originalVariableSingle = null;
$iteratedVariableSingle = $this->inflector->singularize($iteratedVariable);
if ($iteratedVariableSingle === $iteratedVariable) {
$iteratedVariableSingle = 'single' . ucfirst($iteratedVariableSingle);
}
Expand All @@ -166,7 +166,10 @@ private function processForToForeach(For_ $for, string $iteratedVariable): ?Fore
return null;
}

private function createForeachFromForWithIteratedVariableSingle(For_ $for, string $iteratedVariableSingle): Foreach_
private function createForeachFromForWithIteratedVariableSingle(
\For_ $for,
string $iteratedVariableSingle
): Foreach_
{
$foreach = $this->foreachFactory->createFromFor(
$for,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public function refactor(Node $node): ?array

foreach ($args as $key => $arg) {
$assign = new Assign($arrayDimFetch, $arg->value);
$assignExpression = new Expression($assign);
$newStmts[] = $assignExpression;
$newStmts[] = new Expression($assign);

// keep comments of first line
if ($key === 0) {
Expand Down
11 changes: 5 additions & 6 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ public function autowire(PhpDocInfoFactory $phpDocInfoFactory): void
}

/**
* @return array<string, string>
* @return array<string, string>|null
*/
public function resolveFromFile(File $file): array
public function resolveFromFile(File $file): ?array
{
$shortNamesToFullyQualifiedNames = null;
$filePath = $file->getFilePath();

if (isset($this->shortNamesByFilePath[$filePath])) {
return $this->shortNamesByFilePath[$filePath];
}

$shortNamesToFullyQualifiedNames = $this->resolveForStmts($file->getNewStmts());
$this->shortNamesByFilePath[$filePath] = $shortNamesToFullyQualifiedNames;
$this->shortNamesByFilePath[$filePath] = $this->resolveForStmts($file->getNewStmts());

return $shortNamesToFullyQualifiedNames;
}
Expand Down Expand Up @@ -138,8 +138,7 @@ private function resolveForStmts(array $stmts): array
return null;
}

$fullyQualifiedName = $this->nodeNameResolver->getName($node);
$shortNamesToFullyQualifiedNames[$originalName->toString()] = $fullyQualifiedName;
$shortNamesToFullyQualifiedNames[$originalName->toString()] = $this->nodeNameResolver->getName($node);

return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ private function createConstructParams(array $requiredPropertiesWithPhpDocInfos)
$propertyType = $propertyType->type;
}

$param = new Param(new Variable($propertyName), null, $propertyType, false, false, [], $property->flags);
$params[] = $param;
$params[] = new Param(new Variable($propertyName), null, $propertyType, false, false, [], $property->flags);

$propertyPhpDocInfo = $requiredPropertyWithPhpDocInfo->getPhpDocInfo();

Expand Down
3 changes: 1 addition & 2 deletions rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public function refactor(Node $node)
return null;
}

$firstExpr = array_shift($livingCode);
$node->expr = $firstExpr;
$node->expr = array_shift($livingCode);

$newNodes = [];
foreach ($livingCode as $singleLivingCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
final class RemoveJustVariableAssignRector extends AbstractRector
{
public function __construct(
private VariableAnalyzer $variableAnalyzer
private readonly VariableAnalyzer $variableAnalyzer
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,10 @@ public function refactor(Node $node): ?Node
if (! $nextStmt instanceof Return_) {
$afterStmts[] = $stmt->stmts[0];

$newStmts = array_merge(
$node->stmts = array_merge(
$newStmts,
$this->processReplaceIfs($stmt, $booleanAndConditions, new Return_(), $afterStmts)
);
$node->stmts = $newStmts;
return $node;
}

Expand All @@ -150,10 +149,7 @@ public function refactor(Node $node): ?Node
}

$changedStmts = $this->processReplaceIfs($stmt, $booleanAndConditions, $ifNextReturnClone, $afterStmts);
$changedStmts = array_merge($newStmts, $changedStmts);

// update stmts
$node->stmts = $changedStmts;
$node->stmts = array_merge($newStmts, $changedStmts);

return $node;
}
Expand Down
5 changes: 3 additions & 2 deletions rules/Naming/Naming/ConflictingNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ private function resolveConflictingVariableNamesForNew(ClassMethod | Function_ |

$protectedNames = array_merge($paramNames, $newAssignNames, $nonNewAssignNames);

$protectedNames = $this->arrayFilter->filterWithAtLeastTwoOccurences($protectedNames);
$this->conflictingVariableNamesByClassMethod[$classMethodHash] = $protectedNames;
$this->conflictingVariableNamesByClassMethod[$classMethodHash] = $this->arrayFilter->filterWithAtLeastTwoOccurences(
$protectedNames
);

return $protectedNames;
}
Expand Down
6 changes: 3 additions & 3 deletions rules/Naming/Naming/OverridenExistingNamesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ private function resolveOveriddenNamesForNew(ClassMethod | Function_ | Closure $
}

$currentlyUsedNames = array_values($currentlyUsedNames);
$currentlyUsedNames = $this->arrayFilter->filterWithAtLeastTwoOccurences($currentlyUsedNames);

$this->overridenExistingVariableNamesByClassMethod[$classMethodHash] = $currentlyUsedNames;
$this->overridenExistingVariableNamesByClassMethod[$classMethodHash] = $this->arrayFilter->filterWithAtLeastTwoOccurences(
$currentlyUsedNames
);

return $currentlyUsedNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ private function hasNamespaceInPreviousNamespace(Namespace_ $namespace): bool
}

/**
* @return Namespace_|Stmt[]
* @return Namespace_|Stmt[]|null
*/
private function refactorFileWithoutNamespace(
FileWithoutNamespace $fileWithoutNamespace,
string $expectedNamespace
): Namespace_|array {
): ?array {
$namespace = null;
$nodes = $fileWithoutNamespace->stmts;

$declare = null;
Expand All @@ -148,8 +149,7 @@ private function refactorFileWithoutNamespace(
}
}

$namespace = new Namespace_(new Name($expectedNamespace), $nodes);
$nodesWithStrictTypesThenNamespace[] = $namespace;
$nodesWithStrictTypesThenNamespace[] = new Namespace_(new Name($expectedNamespace), $nodes);

$this->fullyQualifyStmtsAnalyzer->process($nodes);

Expand Down
3 changes: 1 addition & 2 deletions rules/Php72/NodeManipulator/ClosureNestedUsesDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function applyNestedUses(Closure $anonymousFunctionNode, Variable $useVar
}

$uses = array_merge($parent->uses, $uses);
$uses = $this->cleanClosureUses($uses);
$parent->uses = $uses;
$parent->uses = $this->cleanClosureUses($uses);

$parent = $this->betterNodeFinder->findParentType($parent, Closure::class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public function refactor(Node $node): ?Node
}

// property name has higher priority
$propertyName = $this->getName($property);
$param->var->name = $propertyName;
$param->var->name = $this->getName($property);
$param->flags = $property->flags;
// Copy over attributes of the "old" property
$param->attrGroups = $property->attrGroups;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public function collectPropertyFetchByMethods(Class_ $class, array $propertyName
continue;
}

$classMethodName = $this->nodeNameResolver->getName($classMethod);
$propertyUsageByMethods[$propertyName][] = $classMethodName;
$propertyUsageByMethods[$propertyName][] = $this->nodeNameResolver->getName($classMethod);
}
}

Expand Down
8 changes: 4 additions & 4 deletions rules/Privatization/Reflection/ClassConstantsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public function __construct(
}

/**
* @return array<string, mixed>
* @return array<string, mixed>|null
*/
public function getClassConstantNamesToValues(string $classWithConstants): array
public function getClassConstantNamesToValues(string $classWithConstants): ?array
{
$constantNamesToValues = null;
if (isset($this->cachedConstantNamesToValues[$classWithConstants])) {
return $this->cachedConstantNamesToValues[$classWithConstants];
}
Expand All @@ -34,8 +35,7 @@ public function getClassConstantNamesToValues(string $classWithConstants): array
$classReflection = $this->reflectionProvider->getClass($classWithConstants);
$nativeReflection = $classReflection->getNativeReflection();

$constantNamesToValues = $nativeReflection->getConstants();
$this->cachedConstantNamesToValues = $constantNamesToValues;
$this->cachedConstantNamesToValues = $nativeReflection->getConstants();

return $constantNamesToValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ private function processGetter(PropertyFetch $propertyFetch): ?Node
);

if ($propertyToMethodCall->getNewGetArguments() !== []) {
$args = $this->nodeFactory->createArgs($propertyToMethodCall->getNewGetArguments());
$methodCall->args = $args;
$methodCall->args = $this->nodeFactory->createArgs($propertyToMethodCall->getNewGetArguments());
}

return $methodCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ public function refactor(Node $node): ?Node
continue;
}

$attributeGroup = $this->phpAttributeGroupFactory->createFromClass(
$node->attrGroups[] = $this->phpAttributeGroupFactory->createFromClass(
PhpDocFromTypeDeclarationDecorator::RETURN_TYPE_WILL_CHANGE_ATTRIBUTE
);
$node->attrGroups[] = $attributeGroup;
$hasChanged = true;

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ private function hasNeededAttributeAlready(Class_ $class): bool

private function addAllowDynamicPropertiesAttribute(Class_ $class): Class_
{
$attributeGroup = $this->phpAttributeGroupFactory->createFromClass(self::ATTRIBUTE);
$class->attrGroups[] = $attributeGroup;
$class->attrGroups[] = $this->phpAttributeGroupFactory->createFromClass(self::ATTRIBUTE);

return $class;
}
Expand Down
10 changes: 4 additions & 6 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public function resolveStrictTypesFromCalls(array $calls): array
continue;
}

$argValueType = $this->resolveStrictArgValueType($arg);

$staticTypesByArgumentPosition[$position][] = $argValueType;
$staticTypesByArgumentPosition[$position][] = $this->resolveStrictArgValueType($arg);
}
}

Expand Down Expand Up @@ -82,9 +80,9 @@ private function unionToSingleType(array $staticTypesByArgumentPosition): array
$unionedType = $this->typeFactory->createMixedPassedOrUnionType($staticTypes);

// narrow parents to most child type
$unionedType = $this->narrowParentObjectTreeToSingleObjectChildType($unionedType);

$staticTypeByArgumentPosition[$position] = $unionedType;
$staticTypeByArgumentPosition[$position] = $this->narrowParentObjectTreeToSingleObjectChildType(
$unionedType
);
}

if (count($staticTypeByArgumentPosition) !== 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ private function processClassMethodNodeWithTypehints(
return;
}

$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($newType, TypeKind::RETURN);
$classMethod->returnType = $returnTypeNode;
$classMethod->returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($newType, TypeKind::RETURN);

$this->hasChanged = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public function refactor(Node $node): ?Node
}

$returnType = $this->typeFactory->createMixedPassedOrUnionType($newTypes);
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
$node->returnType = $returnTypeNode;
$node->returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public function refactor(Node $node): ?Node
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
/** @var PhpParserUnionType[] $returnedStrictTypes */
$unwrappedTypes = $this->typeNodeUnwrapper->unwrapNullableUnionTypes($returnedStrictTypes);
$returnType = new PhpParserUnionType($unwrappedTypes);
$node->returnType = $returnType;
$node->returnType = new PhpParserUnionType($unwrappedTypes);
return $node;
}

Expand Down
3 changes: 1 addition & 2 deletions rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function convertConstantArrayTypeToArrayType(ConstantArrayType $constantA
$unionType = $constantArrayType->getItemType();
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof ConstantStringType) {
$stringType = new StringType();
$nonConstantValueTypes[$stringType::class] = $stringType;
$nonConstantValueTypes[$stringType::class] = new StringType();
} elseif ($unionedType instanceof ObjectType) {
$nonConstantValueTypes[] = $unionedType;
} else {
Expand Down

0 comments on commit 4521c8f

Please sign in to comment.