Skip to content

Commit

Permalink
[Php74][Php81] Allow mixed key on ArraySpreadInsteadOfArrayMergeRecto…
Browse files Browse the repository at this point in the history
…r on php 8.1 version feature enabled (#5190)

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 22, 2023
1 parent 5ac3cb3 commit a0d3244
Show file tree
Hide file tree
Showing 44 changed files with 182 additions and 111 deletions.
Expand Up @@ -170,7 +170,7 @@ private function prepareProcessCommandDefinition(): InputDefinition
// not sure why, but the 1st argument "command" is missing; this is needed for a command name
$arguments = $inputDefinition->getArguments();
$commandInputArgument = new InputArgument(self::COMMAND, InputArgument::REQUIRED);
$arguments = array_merge([$commandInputArgument], $arguments);
$arguments = [$commandInputArgument, ...$arguments];

$inputDefinition->setArguments($arguments);

Expand Down
Expand Up @@ -50,7 +50,7 @@ public function findDoctrineAnnotationsByClasses(PhpDocNode $phpDocNode, array $

foreach ($classes as $class) {
$justFoundTagValueNodes = $this->findDoctrineAnnotationsByClass($phpDocNode, $class);
$doctrineAnnotationTagValueNodes = array_merge($doctrineAnnotationTagValueNodes, $justFoundTagValueNodes);
$doctrineAnnotationTagValueNodes = [...$doctrineAnnotationTagValueNodes, ...$justFoundTagValueNodes];
}

return $doctrineAnnotationTagValueNodes;
Expand Down
Expand Up @@ -91,7 +91,7 @@ private function resolveAnnotationValues(BetterTokenIterator $tokenIterator, Nod
$resolvedValue = $this->resolveAnnotationValue($tokenIterator, $currentPhpNode);

if (is_array($resolvedValue)) {
$values = array_merge($values, $resolvedValue);
$values = [...$values, ...$resolvedValue];
} else {
$values[] = $resolvedValue;
}
Expand All @@ -107,7 +107,7 @@ private function resolveAnnotationValues(BetterTokenIterator $tokenIterator, Nod
$nestedValues = $this->resolveAnnotationValue($tokenIterator, $currentPhpNode);

if (is_array($nestedValues)) {
$values = array_merge($values, $nestedValues);
$values = [...$values, ...$nestedValues];
} else {
if ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_END)) {
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php
Expand Up @@ -93,7 +93,7 @@ public function resolveParentClassMethodReturnType(ClassReflection $classReflect
private function resolveParentClassMethods(ClassReflection $classReflection, string $methodName): array
{
$parentClassMethods = [];
$parents = array_merge($classReflection->getParents(), $classReflection->getInterfaces());
$parents = [...$classReflection->getParents(), ...$classReflection->getInterfaces()];
foreach ($parents as $parent) {
if (! $parent->hasNativeMethod($methodName)) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Expand Up @@ -61,7 +61,7 @@ public function getClassLikeAncestorNames(Class_ | Interface_ | Name $classOrNam
}

$classReflection = $this->reflectionProvider->getClass($fullName);
$ancestors = array_merge($classReflection->getParents(), $classReflection->getInterfaces());
$ancestors = [...$classReflection->getParents(), ...$classReflection->getInterfaces()];

return array_map(
static fn (ClassReflection $classReflection): string => $classReflection->getName(),
Expand Down
2 changes: 1 addition & 1 deletion packages/NodeTypeResolver/PHPStan/Type/TypeFactory.php
Expand Up @@ -83,7 +83,7 @@ private function unwrapUnionedTypes(array $types): array

foreach ($flattenTypes as $flattenType) {
if ($flattenType instanceof ConstantArrayType) {
$unwrappedTypes = array_merge($unwrappedTypes, $this->unwrapConstantArrayTypes($flattenType));
$unwrappedTypes = [...$unwrappedTypes, ...$this->unwrapConstantArrayTypes($flattenType)];
} else {
$unwrappedTypes = $this->resolveNonConstantArrayType($flattenType, $unwrappedTypes);
}
Expand Down
Expand Up @@ -139,10 +139,7 @@ private function resolveClassParentClassesAndInterfaces(ObjectType $objectType):
$implementedInterfaceClassReflections = array_reverse($classReflection->getInterfaces());

/** @var ClassReflection[] $parentClassAndInterfaceReflections */
$parentClassAndInterfaceReflections = array_merge(
$implementedInterfaceClassReflections,
$classReflection->getParents()
);
$parentClassAndInterfaceReflections = [...$implementedInterfaceClassReflections, ...$classReflection->getParents()];

return $this->filterOutNativeClassReflections($parentClassAndInterfaceReflections);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/Parallel/Command/WorkerCommandLineFactory.php
Expand Up @@ -41,7 +41,7 @@ public function create(
int $port
): string {
$commandArguments = array_slice($_SERVER['argv'], 1);
$args = array_merge([PHP_BINARY, $mainScript], $commandArguments);
$args = [PHP_BINARY, $mainScript, ...$commandArguments];

$workerCommandArray = [];

Expand Down
8 changes: 4 additions & 4 deletions packages/PostRector/Rector/UnusedImportRemovingPostRector.php
Expand Up @@ -121,13 +121,13 @@ private function findNamesInDocBlocks(Namespace_|FileWithoutNamespace $namespace
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$names = array_merge($names, $phpDocInfo->getAnnotationClassNames());
$names = [...$names, ...$phpDocInfo->getAnnotationClassNames()];

$constFetchNodeNames = $phpDocInfo->getConstFetchNodeClassNames();
$names = array_merge($names, $constFetchNodeNames);
$names = [...$names, ...$constFetchNodeNames];

$genericTagClassNames = $phpDocInfo->getGenericTagClassNames();
$names = array_merge($names, $genericTagClassNames);
$names = [...$names, ...$genericTagClassNames];
});

return $names;
Expand All @@ -141,7 +141,7 @@ private function resolveUsedPhpAndDocNames(Namespace_|FileWithoutNamespace $name
$phpNames = $this->findNonUseImportNames($namespace);
$docBlockNames = $this->findNamesInDocBlocks($namespace);

$names = array_merge($phpNames, $docBlockNames);
$names = [...$phpNames, ...$docBlockNames];
return array_unique($names);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/StaticTypeMapper/Naming/NameScopeFactory.php
Expand Up @@ -117,7 +117,7 @@ private function templateTemplateTypeMap(Node $node): TemplateTypeMap
}
}

$templateTypes = array_merge($nodeTemplateTypes, $classTemplateTypes);
$templateTypes = [...$nodeTemplateTypes, ...$classTemplateTypes];
return new TemplateTypeMap($templateTypes);
}

Expand Down
Expand Up @@ -83,7 +83,7 @@ private function hasClassMethodLockMatchingFileName(
string $methodName,
string $filePathPartName
): bool {
$ancestorClassReflections = array_merge($classReflection->getParents(), $classReflection->getInterfaces());
$ancestorClassReflections = [...$classReflection->getParents(), ...$classReflection->getInterfaces()];
foreach ($ancestorClassReflections as $ancestorClassReflection) {
// parent type
if (! $ancestorClassReflection->hasNativeMethod($methodName)) {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Expand Up @@ -233,6 +233,10 @@ parameters:
- packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php
- rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php
- rules/Php70/EregToPcreTransformer.php
- rules/DeadCode/NodeManipulator/LivingCodeManipulator.php
- rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php
- src/FileSystem/FilesFinder.php
- src/PhpParser/NodeTransformer.php

# impossible to validate json string is a class-string
- '#Parameter \#1 \$rectorClass of class Rector\\ChangesReporting\\ValueObject\\RectorWithLineChange constructor expects class\-string<Rector\\Core\\Contract\\Rector\\RectorInterface\>\|Rector\\Core\\Contract\\Rector\\RectorInterface, string given#'
Expand Down
@@ -0,0 +1,65 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;

class AnyKey
{
/**
* @return array<int>
*/
public function getA(): array
{
return [1, 2, 3];
}

/**
* @return array<int>
*/
public function getB(): array
{
return [4, 5, 6];
}

public function run()
{
$a = $this->getA();
$b = $this->getB();

return array_merge($a, $b);
}
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;

class AnyKey
{
/**
* @return array<int>
*/
public function getA(): array
{
return [1, 2, 3];
}

/**
* @return array<int>
*/
public function getB(): array
{
return [4, 5, 6];
}

public function run()
{
$a = $this->getA();
$b = $this->getB();

return [...$a, ...$b];
}
}

?>
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;

class SkipFirstClassCallable
{
public function run()
{
array_merge(...);
}
}

This file was deleted.

@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;

function x(): array
{
return ['b'=>1];
}
function y(): array
{
return ['a'=>1];
}

class StringKeysFromFunctions
{
public function run()
{
return array_merge(y(), x());
}
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;

function x(): array
{
return ['b'=>1];
}
function y(): array
{
return ['a'=>1];
}

class StringKeysFromFunctions
{
public function run()
{
return [...y(), ...x()];
}
}

?>
Expand Up @@ -68,7 +68,7 @@ public function refactor(Node $node): ?array

$nextClassConsts = $this->createNextClassConsts($allConsts, $node);

return array_merge([$node], $nextClassConsts);
return [$node, ...$nextClassConsts];
}

/**
Expand Down
27 changes: 6 additions & 21 deletions rules/DeadCode/NodeManipulator/LivingCodeManipulator.php
Expand Up @@ -61,10 +61,7 @@ public function keepLivingCodeFromExpr(Node | int | string | null $expr): array
}

if ($expr instanceof PropertyFetch) {
return array_merge(
$this->keepLivingCodeFromExpr($expr->var),
$this->keepLivingCodeFromExpr($expr->name)
);
return [...$this->keepLivingCodeFromExpr($expr->var), ...$this->keepLivingCodeFromExpr($expr->name)];
}

if ($expr instanceof ArrayDimFetch) {
Expand All @@ -77,17 +74,11 @@ public function keepLivingCodeFromExpr(Node | int | string | null $expr): array
}
}

return array_merge(
$this->keepLivingCodeFromExpr($expr->var),
$this->keepLivingCodeFromExpr($expr->dim)
);
return [...$this->keepLivingCodeFromExpr($expr->var), ...$this->keepLivingCodeFromExpr($expr->dim)];
}

if ($expr instanceof ClassConstFetch || $expr instanceof StaticPropertyFetch) {
return array_merge(
$this->keepLivingCodeFromExpr($expr->class),
$this->keepLivingCodeFromExpr($expr->name)
);
return [...$this->keepLivingCodeFromExpr($expr->class), ...$this->keepLivingCodeFromExpr($expr->name)];
}

if ($this->isBinaryOpWithoutChange($expr)) {
Expand All @@ -97,10 +88,7 @@ public function keepLivingCodeFromExpr(Node | int | string | null $expr): array
}

if ($expr instanceof Instanceof_) {
return array_merge(
$this->keepLivingCodeFromExpr($expr->expr),
$this->keepLivingCodeFromExpr($expr->class)
);
return [...$this->keepLivingCodeFromExpr($expr->expr), ...$this->keepLivingCodeFromExpr($expr->class)];
}

if ($expr instanceof Isset_) {
Expand Down Expand Up @@ -141,10 +129,7 @@ private function isBinaryOpWithoutChange(Expr $expr): bool
*/
private function processBinary(BinaryOp $binaryOp): array
{
return array_merge(
$this->keepLivingCodeFromExpr($binaryOp->left),
$this->keepLivingCodeFromExpr($binaryOp->right)
);
return [...$this->keepLivingCodeFromExpr($binaryOp->left), ...$this->keepLivingCodeFromExpr($binaryOp->right)];
}

/**
Expand All @@ -154,7 +139,7 @@ private function processIsset(Isset_ $isset): array
{
$livingExprs = [];
foreach ($isset->vars as $expr) {
$livingExprs = array_merge($livingExprs, $this->keepLivingCodeFromExpr($expr));
$livingExprs = [...$livingExprs, ...$this->keepLivingCodeFromExpr($expr)];
}

return $livingExprs;
Expand Down
11 changes: 4 additions & 7 deletions rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php
Expand Up @@ -142,10 +142,7 @@ public function refactor(Node $node): ?Node
if (! $nextStmt instanceof Return_) {
$afterStmts[] = $stmt->stmts[0];

$node->stmts = array_merge(
$newStmts,
$this->processReplaceIfs($stmt, $booleanAndConditions, new Return_(), $afterStmts, $nextStmt)
);
$node->stmts = [...$newStmts, ...$this->processReplaceIfs($stmt, $booleanAndConditions, new Return_(), $afterStmts, $nextStmt)];

return $node;
}
Expand All @@ -172,7 +169,7 @@ public function refactor(Node $node): ?Node
);

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

return $node;
}
Expand Down Expand Up @@ -208,7 +205,7 @@ private function processReplaceIfs(
$ifs = $this->invertedIfFactory->createFromConditions($if, $conditions, $ifNextReturn, $nextStmt);
$this->mirrorComments($ifs[0], $if);

$result = array_merge($ifs, $afters);
$result = [...$ifs, ...$afters];
if ($if->stmts[0] instanceof Return_) {
return $result;
}
Expand All @@ -221,7 +218,7 @@ private function processReplaceIfs(
return $result;
}

return array_merge($result, [$ifNextReturn]);
return [...$result, $ifNextReturn];
}

private function shouldSkip(If_ $if, ?Stmt $nexStmt): bool
Expand Down

0 comments on commit a0d3244

Please sign in to comment.