Skip to content

Commit

Permalink
[TypeDeclaration] Handle crash on intersection in Union type on AddMe…
Browse files Browse the repository at this point in the history
…thodCallBasedStrictParamTypeRector (#2704)

* [TypeDeclaration] Skip intersection in Union type on AddMethodCallBasedStrictParamTypeRector

* Fixed 🎉

* [ci-review] Rector Rectify

* cs fix

* fix

* [ci-review] Rector Rectify

* reduce complexity

* final touch: eol

* final touch: comment rollback

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 27, 2022
1 parent a018130 commit fe0551e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType as PHPParserNodeIntersectionType;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
Expand All @@ -16,6 +17,7 @@
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -286,7 +288,7 @@ private function matchPhpParserUnionType(UnionType $unionType, string $typeKind)
* NullType inside UnionType is allowed
* make it on TypeKind property as changing other type, eg: return type may conflict with parent child implementation
*
* @var Identifier|Name|null $phpParserNode
* @var Identifier|Name|null|PHPParserNodeIntersectionType $phpParserNode
*/
$phpParserNode = $unionedType instanceof NullType && $typeKind === TypeKind::PROPERTY
? new Name('null')
Expand All @@ -296,9 +298,14 @@ private function matchPhpParserUnionType(UnionType $unionType, string $typeKind)
return null;
}

if ($phpParserNode instanceof PHPParserNodeIntersectionType && $unionedType instanceof IntersectionType) {
return null;
}

$phpParserUnionedTypes[] = $phpParserNode;
}

/** @var Identifier[]|Name[] $phpParserUnionedTypes */
$phpParserUnionedTypes = array_unique($phpParserUnionedTypes);

if (count($phpParserUnionedTypes) < 2) {
Expand Down
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\FixtureIntersection;

abstract class SkipIntersectionInUnion
{
public function prepareParameters(): array
{
$parameters = array_merge($this->getRequestParameters(), [
'request' => $this->getRequestName(),
]);

$this->removeEmptyValues($parameters);

return [];
}

private function removeEmptyValues($input): array
{
foreach ($input as &$value) {
if (! is_array($value)) {
continue;
}

$value = $this->removeEmptyValues($value);
}

return [];
}
}

0 comments on commit fe0551e

Please sign in to comment.