Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

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

namespace Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\Fixture;

use Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\Source\ParentWithRelativeConstant;

final class SkipOnClassConstantNotMovable extends ParentWithRelativeConstant
{
public const A = 'child-a';

private const USES_SELF_INHERITED = [...self::USES_SELF];

private const USES_MAGIC_INHERITED = [...self::USES_MAGIC];

private const USES_NAMESPACED_CONSTANT_INHERITED = [...self::USES_NAMESPACED_CONSTANT];

private const USES_PRIVATE_BY_NAME_INHERITED = [...self::USES_PRIVATE_BY_NAME];
}

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

namespace Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\Fixture;

use Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\Source\ParentWithRelativeConstant;

final class SkipPropertyDefaultSpread extends ParentWithRelativeConstant
{
private $items = [...self::USES_SELF];

public function run($items = [...self::USES_SELF])
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class WithUnknownTypeFunctionItem

public function run()
{
$fruits = array_merge(['banana', 'orange'], is_array($this->getArrayOrTraversable()) ? $this->getArrayOrTraversable() : iterator_to_array($this->getArrayOrTraversable()), ['watermelon']);
$fruits = array_merge(['banana', 'orange'], is_array($items = $this->getArrayOrTraversable()) ? $items : iterator_to_array($items), ['watermelon']);
}
}

Expand Down
19 changes: 19 additions & 0 deletions rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Type\Type;
use Rector\DowngradePhp81\NodeAnalyzer\ArraySpreadAnalyzer;
use Rector\DowngradePhp81\NodeFactory\ArrayMergeFromArraySpreadFactory;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -112,12 +113,30 @@ public function refactor(Node $node): ?Node
return null;
}

// array_merge() is not allowed in a constant expression
if ($this->isInConstantExpr($node)) {
return null;
}

/** @var MutatingScope $scope */
$scope = ScopeFetcher::fetch($node);

return $this->arrayMergeFromArraySpreadFactory->createFromArray($node, $scope);
}

private function isInConstantExpr(Array_ $array): bool
{
if ($array->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE) === true) {
return true;
}

if ($array->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE) === true) {
return true;
}

return $array->getAttribute(AttributeKey::IS_PARAM_DEFAULT) === true;
}

private function refactorUnderClassLike(ClassLike $classLike): ?ClassLike
{
$hasChanged = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Type\IterableType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\DowngradePhp\Naming\VariableNaming;

final readonly class ArrayMergeFromArraySpreadFactory
{
public function __construct(
private VariableNaming $variableNaming
) {
}

public function createFromArray(Array_ $array, MutatingScope $mutatingScope): FuncCall
{
$newArrayItems = $this->dissolveArrayItems($array);
Expand Down Expand Up @@ -108,6 +117,17 @@ private function createArgFromSpreadArrayItem(MutatingScope $mutatingScope, Arra
}

// Print a ternary, handling either an array or an iterator
if ($arrayItem->value instanceof CallLike) {
// assign the call result first, so the call runs only once
$variable = new Variable($this->variableNaming->createCountedValueName('items', $mutatingScope));
$isArrayFuncCall = new FuncCall(new Name('is_array'), [
new Arg(new Assign($variable, $arrayItem->value)),
]);
$iteratorToArrayFuncCall = new FuncCall(new Name('iterator_to_array'), [new Arg($variable)]);

return new Arg(new Ternary($isArrayFuncCall, $variable, $iteratorToArrayFuncCall));
}

$inArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($arrayItem->value)]);
return new Arg(new Ternary($inArrayFuncCall, $arrayItem->value, $iteratorToArrayFuncCall));
}
Expand Down
Loading