Skip to content

Commit

Permalink
[TypeDeclaration] Skip union intersection types on php 8.1, allow on …
Browse files Browse the repository at this point in the history
…php 8.2+ (#6395)

* [TypeDeclaration] Skip union intersection types on php 8.1

* Fix

* fix other test

* check when multi only
  • Loading branch information
samsonasik authored Oct 23, 2024
1 parent 69d3af9 commit d415260
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixturePhp81;

final class SkipUnionIntersection
{
public function run(\Iterator&\Countable $obj)
{
if (rand(0, 1)) {
return $obj;
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureUnionIntersection;

/**
* union intersection works on php 8.2+
*/
final class UnionIntersection
{
public function run(\Iterator&\Countable $obj)
{
if (rand(0, 1)) {
return $obj;
}

return '10';
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\FixtureUnionIntersection;

/**
* union intersection works on php 8.2+
*/
final class UnionIntersection
{
public function run(\Iterator&\Countable $obj): (\Iterator&\Countable)|string
{
if (rand(0, 1)) {
return $obj;
}

return '10';
}
}

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

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class UnionIntersectionTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureUnionIntersection');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule_union_intersection.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;
use Rector\ValueObject\PhpVersionFeature;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReturnUnionTypeRector::class);
$rectorConfig->phpVersion(PhpVersionFeature::UNION_INTERSECTION_TYPES);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ClosureReturnTypeRector::class);
$rectorConfig->phpVersion(PhpVersion::PHP_82);
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rector\Php\PhpVersionProvider;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper;
use Rector\ValueObject\PhpVersionFeature;

Expand Down Expand Up @@ -121,6 +122,10 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return current($intersectionedTypeNodes);
}

if ($typeKind === TypeKind::UNION && ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_INTERSECTION_TYPES)) {
return null;
}

return new Node\IntersectionType($intersectionedTypeNodes);
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ final class PhpVersionFeature
*/
public const INTERSECTION_TYPES = PhpVersion::PHP_81;

/**
* @see https://php.watch/versions/8.2/dnf-types
* @var int
*/
public const UNION_INTERSECTION_TYPES = PhpVersion::PHP_82;

/**
* @see https://wiki.php.net/rfc/array_unpacking_string_keys
* @var int
Expand Down

0 comments on commit d415260

Please sign in to comment.