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
2 changes: 2 additions & 0 deletions config/set/downgrade-php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Rector\Config\RectorConfig;
use Rector\ValueObject\PhpVersion;
use Rector\DowngradePhp82\Rector\Class_\DowngradeReadonlyClassRector;
use Rector\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector;
use Rector\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector;
use Rector\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector;

Expand All @@ -14,5 +15,6 @@
DowngradeReadonlyClassRector::class,
DowngradeStandaloneNullTrueFalseReturnTypeRector::class,
DowngradeIteratorCountToArrayRector::class,
DowngradeUnionIntersectionRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector;

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

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

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

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

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture;

function onParamFunction((A&B)|C $foo)
{
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture;

/**
* @param (\Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\A & \Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\B)|\Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\C $foo
*/
function onParamFunction($foo)
{
}

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

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture;

final class OnProperty
{
public (A&B)|C $foo;
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture;

final class OnProperty
{
/**
* @var (\Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\A & \Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\B)|\Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\C
*/
public $foo;
}

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

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture;

function onReturnFunction(): (A&B)|C
{
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture;

/**
* @return (\Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\A & \Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\B)|\Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture\C
*/
function onReturnFunction()
{
}

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

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\Fixture;

final class SkipOtherUnion
{
public A|B|C $foo;
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeUnionIntersectionRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public function refactor(Node $node): Node|array|null
foreach ($args as $arg) {
if ($arg->value instanceof Ternary && $arg->value->else instanceof Throw_) {
$refactorTernary = $this->refactorTernary($arg->value, null, true);
if (is_array($refactorTernary) && $refactorTernary[0] instanceof Expression && $refactorTernary[0]->expr instanceof Assign) {
if (is_array(
$refactorTernary
) && $refactorTernary[0] instanceof Expression && $refactorTernary[0]->expr instanceof Assign) {
$arg->value = $refactorTernary[0]->expr->var;

return [...$refactorTernary, $node];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp82\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\UnionType;
use Rector\NodeManipulator\PropertyDecorator;
use Rector\PhpDocDecorator\PhpDocFromTypeDeclarationDecorator;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @changelog https://php.watch/versions/8.2/dnf-types
*
* @see \Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeUnionIntersectionRector\DowngradeUnionIntersectionRectorTest
*/
final class DowngradeUnionIntersectionRector extends AbstractRector
{
public function __construct(
private readonly PropertyDecorator $propertyDecorator,
private readonly PhpDocFromTypeDeclarationDecorator $phpDocFromTypeDeclarationDecorator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove the union type with intersection, use docblock based',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public (A&B)|C $foo;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
/**
* @var (A&B)|C
*/
public $foo;
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassLike::class, ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
}

/**
* @param ClassLike|ClassMethod|Function_|Closure|ArrowFunction $node
*/
public function refactor(Node $node): ?Node
{
if (! $node instanceof ClassLike) {
return $this->processFunctionLike($node);
}

return $this->processClassLike($node);
}

private function processFunctionLike(
ClassMethod|Function_|Closure|ArrowFunction $functionLike
): null|ClassMethod|Function_|Closure|ArrowFunction {
$paramDecorated = false;
foreach ($functionLike->getParams() as $param) {
if (! $this->isUnionIntersection($param->type)) {
continue;
}

Assert::isInstanceOf($param->type, UnionType::class);
$this->phpDocFromTypeDeclarationDecorator->decorateParam(
$param,
$functionLike,
[\PHPStan\Type\UnionType::class]
);
$paramDecorated = true;
}

if (! $this->isUnionIntersection($functionLike->returnType)) {
if ($paramDecorated) {
return $functionLike;
}

return null;
}

Assert::isInstanceOf($functionLike->returnType, UnionType::class);
$this->phpDocFromTypeDeclarationDecorator->decorateReturn($functionLike);

return $functionLike;
}

private function processClassLike(ClassLike $classLike): ?ClassLike
{
$hasChanged = false;
foreach ($classLike->getProperties() as $property) {
if (! $this->isUnionIntersection($property->type)) {
continue;
}

Assert::isInstanceOf($property->type, UnionType::class);
$this->propertyDecorator->decorateWithDocBlock($property, $property->type);

$property->type = null;
$hasChanged = true;
}

return $hasChanged ? $classLike : null;
}

private function isUnionIntersection(?Node $node): bool
{
if (! $node instanceof UnionType) {
return false;
}

foreach ($node->types as $type) {
if ($type instanceof IntersectionType) {
return true;
}
}

return false;
}
}