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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -127,16 +130,11 @@ private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr): bool
}

$arrayStaticType = $this->getStaticType($expr);
if ($arrayStaticType instanceof ConstantArrayType) {
foreach ($arrayStaticType->getKeyTypes() as $keyType) {
// key cannot be string
if ($keyType instanceof ConstantStringType) {
return true;
}
}
if ($this->isConstantArrayTypeWithStringKeyType($arrayStaticType)) {
return true;
}

return false;
return $arrayStaticType instanceof ArrayType && $arrayStaticType->getKeyType() instanceof StringType;
}

private function resolveValue(Expr $expr): Expr
Expand Down Expand Up @@ -174,4 +172,20 @@ private function isIteratorToArrayFuncCall(Expr $expr): bool
{
return $expr instanceof FuncCall && $this->isName($expr, 'iterator_to_array');
}

private function isConstantArrayTypeWithStringKeyType(Type $type): bool
{
if (! $type instanceof ConstantArrayType) {
return false;
}

foreach ($type->getKeyTypes() as $keyType) {
// key cannot be string
if ($keyType instanceof ConstantStringType) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

class SkipParseUrl
{
public function run()
{
return array_merge($this->parseUrl($url), $this->parseUrl($redirectLocation));
}

/**
* @return array<string, string>
*/
private function parseUrl(string $url): array
{
$urlParts = parse_url($url);

return array_filter($urlParts);
}
}