[DowngradePhp80] Add DowngradeMixedTypeTypedPropertyRector (#2579)

* [DowngradePhp80] Add DowngradeMixedTypeTypedPropertyRector

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-06-27 19:26:13 +07:00 committed by GitHub
parent 89a684f2b7
commit e81691f1c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 177 additions and 0 deletions

View File

@ -30,6 +30,7 @@ use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRect
use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector;
use Rector\DowngradePhp80\Rector\New_\DowngradeArbitraryExpressionsSupportRector;
use Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeMixedTypeTypedPropertyRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
use Rector\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector;
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
@ -79,4 +80,5 @@ return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeArrayFilterNullableCallbackRector::class);
$rectorConfig->rule(DowngradeNumberFormatNoFourthArgRector::class);
$rectorConfig->rule(DowngradeStringReturnTypeOnToStringRector::class);
$rectorConfig->rule(DowngradeMixedTypeTypedPropertyRector::class);
};

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp80\Rector\Property\DowngradeMixedTypeTypedPropertyRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeMixedTypeTypedPropertyRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Property\DowngradeMixedTypeTypedPropertyRector\Fixture;
class MixedTyped
{
public mixed $value;
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Property\DowngradeMixedTypeTypedPropertyRector\Fixture;
class MixedTyped
{
/**
* @var mixed
*/
public $value;
}
?>

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector\Fixture;
class SkipMixedNoTyped
{
/**
* @var mixed
*/
private $prop;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector\Fixture;
class SkipNonMixed
{
private string $prop;
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\DowngradePhp80\Rector\Property\DowngradeMixedTypeTypedPropertyRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeMixedTypeTypedPropertyRector::class);
};

View File

@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
use Rector\CodeQuality\NodeFactory\PropertyTypeDecorator;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp80\Rector\Property\DowngradeMixedTypeTypedPropertyRector\DowngradeMixedTypeTypedPropertyRectorTest
*/
final class DowngradeMixedTypeTypedPropertyRector extends AbstractRector
{
public function __construct(
private readonly PropertyTypeDecorator $propertyTypeDecorator
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Property::class];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Removes mixed type property type definition, adding `@var` annotations instead.', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
private mixed $property;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var mixed
*/
private $property;
}
CODE_SAMPLE
),
]);
}
/**
* @param Property $node
*/
public function refactor(Node $node): ?Node
{
if ($node->type === null) {
return null;
}
if ($this->shouldSkip($node)) {
return null;
}
$this->propertyTypeDecorator->decoratePropertyWithDocBlock($node, $node->type);
$node->type = null;
return $node;
}
private function shouldSkip(Property $property): bool
{
if ($property->type === null) {
return true;
}
$type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($property->type);
if (! $type instanceof MixedType) {
return true;
}
return ! $type->isExplicitMixed();
}
}