Skip to content
Merged
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
33 changes: 32 additions & 1 deletion packages/NodeTypeResolver/src/StaticTypeToStringResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rector\NodeTypeResolver;

use Nette\Utils\Strings;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\CallableType;
Expand Down Expand Up @@ -58,13 +59,14 @@ function (UnionType $unionType): array {

return $types;
},

function (IntersectionType $intersectionType): array {
$types = [];
foreach ($intersectionType->getTypes() as $singleStaticType) {
$types = array_merge($types, $this->resolveObjectType($singleStaticType));
}

return $types;
return $this->removeGenericArrayTypeIfThereIsSpecificArrayType($types);
},
function (ObjectType $objectType): array {
// the must be absolute, since we have no other way to check absolute/local path
Expand Down Expand Up @@ -92,4 +94,33 @@ public function resolveObjectType(?Type $staticType): array

return [];
}

/**
* Removes "array" if there is "SomeType[]" already
*
* @param string[] $types
* @return string[]
*/
private function removeGenericArrayTypeIfThereIsSpecificArrayType(array $types): array
{
$hasSpecificArrayType = false;
foreach ($types as $key => $type) {
if (Strings::endsWith($type, '[]')) {
$hasSpecificArrayType = true;
break;
}
}

if ($hasSpecificArrayType === false) {
return $types;
}

foreach ($types as $key => $type) {
if ($type === 'array') {
unset($types[$key]);
}
}

return $types;
}
}