Skip to content

Commit

Permalink
[PHP 8.1] Fix IntersectionTypesRector for non-object types (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 14, 2021
1 parent 0127d15 commit 7cdd03e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Fixture;

final class SkipScalarType
{
/**
* @param string&int $types
*/
public function process($types)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Fixture;

final class SomeClass
use Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source\FirstObjectToIntersect;
use Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source\SecondObjectToIntersect;

final class SkipScalarTypes
{
/**
* @param string&int $types
* @param FirstObjectToIntersect&SecondObjectToIntersect $types
*/
public function process($types)
{
Expand All @@ -18,12 +21,15 @@ final class SomeClass

namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Fixture;

final class SomeClass
use Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source\FirstObjectToIntersect;
use Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source\SecondObjectToIntersect;

final class SkipScalarTypes
{
/**
* @param string&int $types
* @param FirstObjectToIntersect&SecondObjectToIntersect $types
*/
public function process(int&string $types)
public function process(\Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source\FirstObjectToIntersect&\Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source\SecondObjectToIntersect $types)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source;

final class FirstObjectToIntersect
{

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

declare(strict_types=1);

namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Source;

final class SecondObjectToIntersect
{

}
21 changes: 21 additions & 0 deletions rules/Php81/Rector/FunctionLike/IntersectionTypesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\TypeWithClassName;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -110,6 +111,10 @@ private function refactorParamTypes(
continue;
}

if (! $this->isIntersectionableType($paramType)) {
continue;
}

$phpParserIntersectionType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$paramType,
TypeKind::PARAM()
Expand All @@ -123,4 +128,20 @@ private function refactorParamTypes(
$this->hasChanged = true;
}
}

/**
* Only class-type are supported https://wiki.php.net/rfc/pure-intersection-types#supported_types
*/
private function isIntersectionableType(IntersectionType $intersectionType): bool
{
foreach ($intersectionType->getTypes() as $intersectionedType) {
if ($intersectionedType instanceof TypeWithClassName) {
continue;
}

return false;
}

return true;
}
}

0 comments on commit 7cdd03e

Please sign in to comment.