Skip to content

Commit

Permalink
[DowngradePhp80] Add DowngradeMixedTypeTypedPropertyRector (#2579)
Browse files Browse the repository at this point in the history
* [DowngradePhp80] Add DowngradeMixedTypeTypedPropertyRector

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 27, 2022
1 parent 89a684f commit e81691f
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -79,4 +80,5 @@
$rectorConfig->rule(DowngradeArrayFilterNullableCallbackRector::class);
$rectorConfig->rule(DowngradeNumberFormatNoFourthArgRector::class);
$rectorConfig->rule(DowngradeStringReturnTypeOnToStringRector::class);
$rectorConfig->rule(DowngradeMixedTypeTypedPropertyRector::class);
};
Original file line number Diff line number Diff line change
@@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

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

namespace Rector\Tests\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector\Fixture;

class SkipMixedNoTyped
{
/**
* @var mixed
*/
private $prop;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector\Fixture;

class SkipNonMixed
{
private string $prop;
}
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\DowngradePhp80\Rector\Property\DowngradeMixedTypeTypedPropertyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeMixedTypeTypedPropertyRector::class);
};
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit e81691f

Please sign in to comment.