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
4 changes: 3 additions & 1 deletion config/set/downgrade-php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector;
use Rector\DowngradePhp84\Rector\ClassMethod\DowngradeDeprecatedAttributeRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindKeyRector;
use Rector\DowngradePhp84\Rector\ClassMethod\DowngradeDeprecatedAttributeRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector;
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector;
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
Expand All @@ -24,5 +25,6 @@
DowngradeArrayFindRector::class,
DowngradeArrayFindKeyRector::class,
DowngradeDeprecatedAttributeRector::class,
DowngradeFinalPropertyRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector;

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

final class DowngradeFinalPropertyRectorTest 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,27 @@
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class AlreadyHasFinalTag
{
/**
* @final
*/
final protected $foo = 'bar';
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class AlreadyHasFinalTag
{
/**
* @final
*/
protected $foo = 'bar';
}

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

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class ExistingDocblock
{
/**
* @var string
*/
final protected $foo = 'bar';
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class ExistingDocblock
{
/**
* @var string
* @final
*/
protected $foo = 'bar';
}

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

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class Fixture
{
protected final $foo = 'bar';
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class Fixture
{
/**
* @final
*/
protected $foo = 'bar';
}

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

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

trait InTrait
{
final protected string $foo = 'bar';
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

trait InTrait
{
/**
* @final
*/
protected string $foo = 'bar';
}

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

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class PublicProperty
{
final public string $foo = 'bar';
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\Fixture;

class PublicProperty
{
/**
* @final
*/
public string $foo = 'bar';
}

?>
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\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeFinalPropertyRector::class);
};
103 changes: 103 additions & 0 deletions rules/DowngradePhp84/Rector/Class_/DowngradeFinalPropertyRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp84\Rector\Class_;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/property-hooks#final_properties
*
* @see \Rector\Tests\DowngradePhp84\Rector\Class_\DowngradeFinalPropertyRector\DowngradeFinalPropertyRectorTest
*/
final class DowngradeFinalPropertyRector extends AbstractRector
{
private const string TAGNAME = 'final';

public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change final property to @final tag', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public final $foo = 'bar';
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @final
*/
public $foo = 'bar';
}
CODE_SAMPLE
),
]);
}

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

/**
* @param Class_|Trait_ $node
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;

foreach ($node->getProperties() as $property) {
if (! $property->isFinal()) {
continue;
}

$this->downgradeFinal($property);
$hasChanged = true;
}

if ($hasChanged) {
return $node;
}

return null;
}

private function downgradeFinal(Property $property): void
{
$property->flags &= ~Modifiers::FINAL;

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
if ($phpDocInfo->hasByName(self::TAGNAME)) {
return;
}

$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode('')));
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
}
}
Loading