Skip to content

Commit

Permalink
[Downgrade PHP 8.1] Add DowngradeReadonlyPropertyRector (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed Nov 18, 2021
1 parent 1258576 commit 587d8d8
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config/phpstan/parser.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
parameters:
phpVersion: 80100

services:
defaultAnalysisParser:
factory: @cachedRectorParser
Expand Down
2 changes: 2 additions & 0 deletions config/set/downgrade-php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\DowngradePhp81\Rector\FuncCall\DowngradeFirstClassCallableSyntaxRector;
use Rector\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector;
use Rector\DowngradePhp81\Rector\Instanceof_\DowngradePhp81ResourceReturnToObjectRector;
use Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
Expand All @@ -19,4 +20,5 @@
$services->set(DowngradeFirstClassCallableSyntaxRector::class);
$services->set(DowngradeNeverTypeDeclarationRector::class);
$services->set(DowngradePhp81ResourceReturnToObjectRector::class);
$services->set(DowngradeReadonlyPropertyRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeReadonlyPropertyRectorTest 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,34 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
public readonly string $foo;

public function __construct()
{
$this->foo = 'foo';
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
/**
* @readonly
*/
public string $foo;

public function __construct()
{
$this->foo = 'foo';
}
}

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

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
/**
* Property comment.
*/
public readonly string $foo;

public function __construct()
{
$this->foo = 'foo';
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
/**
* Property comment.
* @readonly
*/
public string $foo;

public function __construct()
{
$this->foo = 'foo';
}
}

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

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
/**
* Property comment.
* @readonly
*/
public readonly string $foo;

public function __construct()
{
$this->foo = 'foo';
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
/**
* Property comment.
* @readonly
*/
public string $foo;

public function __construct()
{
$this->foo = 'foo';
}
}

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

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
public function __construct(
public readonly string $foo = 'foo'
)
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\Fixture;

class SomeClass
{
public function __construct(
public string $foo = 'foo'
)
{
}
}

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

declare(strict_types=1);

use Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeReadonlyPropertyRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp81\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/readonly_properties_v2
*
* @see \Rector\Tests\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector\DowngradeReadonlyPropertyRectorTest
*/
final class DowngradeReadonlyPropertyRector extends AbstractRector
{
/**
* @var string
*/
private const TAGNAME = 'readonly';

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Property::class, Param::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove "readonly" property type, add a "@readonly" tag instead',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public readonly string $foo;
public function __construct()
{
$this->foo = 'foo';
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @readonly
*/
public string $foo;
public function __construct()
{
$this->foo = 'foo';
}
}
CODE_SAMPLE
),
]
);
}

/**
* @param Property|Param $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->visibilityManipulator->isReadonly($node)) {
return null;
}

if ($node instanceof Property) {
$this->addPhpDocTag($node);
}

$this->visibilityManipulator->removeReadonly($node);

return $node;
}

private function addPhpDocTag(Property $property): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

if ($phpDocInfo->hasByName(self::TAGNAME)) {
return;
}

$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode('')));
}
}
3 changes: 1 addition & 2 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -111,7 +110,7 @@ private function refactorParam(Param $param): Param | null
return null;
}

if ($this->visibilityManipulator->hasVisibility($param, Class_::MODIFIER_READONLY)) {
if ($this->visibilityManipulator->isReadonly($param)) {
return null;
}

Expand Down
19 changes: 18 additions & 1 deletion rules/Privatization/NodeManipulator/VisibilityManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ public function removeAbstract(ClassMethod $classMethod): void

public function makeReadonly(Property | Param $node): void
{
$this->addVisibilityFlag($node, Class_::MODIFIER_READONLY);
$this->addVisibilityFlag($node, Visibility::READONLY);
}

public function isReadonly(Property | Param $node): bool
{
return $this->hasVisibility($node, Visibility::READONLY);
}

public function removeReadonly(Property | Param $node): void
{
$this->removeVisibilityFlag($node, Visibility::READONLY);
}

private function addVisibilityFlag(
Expand All @@ -126,6 +136,13 @@ private function addVisibilityFlag(
$node->flags |= $visibility;
}

private function removeVisibilityFlag(
Class_ | ClassMethod | Property | ClassConst | Param $node,
int $visibility
): void {
$node->flags &= ~$visibility;
}

private function replaceVisibilityFlag(ClassMethod | Property | ClassConst $node, int $visibility): void
{
$isStatic = $node instanceof ClassMethod && $node->isStatic();
Expand Down
5 changes: 5 additions & 0 deletions src/ValueObject/Visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ final class Visibility
* @var int
*/
public const FINAL = Class_::MODIFIER_FINAL;

/**
* @var int
*/
public const READONLY = Class_::MODIFIER_READONLY;
}

0 comments on commit 587d8d8

Please sign in to comment.