Skip to content
Closed
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 phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ parameters:
- '#Register "Rector\\Php81\\Rector\\ClassMethod\\NewInInitializerRector" service to "php81\.php" config set#'

- '#Register "Rector\\Php80\\Rector\\NotIdentical\\MbStrContainsRector" service to "php80\.php" config set#'

- '#Register "Rector\\Php85\\Rector\\Class_\\FinalPropertyPromotionRector" service to "php85\.php" config set#'

# closure detailed
- '#Method Rector\\Config\\RectorConfig\:\:singleton\(\) has parameter \$concrete with no signature specified for Closure#'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector;

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

final class FinalPropertyPromotionRectorTest 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,28 @@
<?php

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

use Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Source\ParentClass;

class ChildExtendsParent extends ParentClass
{
public function __construct(
/** @final */
public string $idd
){}
}
?>
-----
<?php

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

use Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Source\ParentClass;

class ChildExtendsParent extends ParentClass
{
public function __construct(
final public string $idd
){}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

class FinalPropertyPromotion
{
public function __construct(
/** @final */
public string $id
){}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

class FinalPropertyPromotion
{
public function __construct(
final public string $id
){}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

class FinalPropertyPromotion
{
public function __construct(
/** @final */
$id
){}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

class FinalPropertyPromotion
{
public function __construct(
/** @final */
public readonly string $id
){}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

class FinalPropertyPromotion
{
public function __construct(
final public readonly string $id
){}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

class FinalPropertyPromotion
{
public function __construct(
final public string $id
){}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

$obj = new class {
public function __construct(
/** @final */
public string $id
){}
};
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture;

final class FinalPropertyPromotion
{
public function __construct(
/** @final */
public string $id
){}
}

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

namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Source;

class ParentClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\Class_\FinalPropertyPromotionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(FinalPropertyPromotionRector::class);

};
120 changes: 120 additions & 0 deletions rules/Php85/Rector/Class_/FinalPropertyPromotionRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace Rector\Php85\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\Visibility;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://wiki.php.net/rfc/final_promotion
* @see \Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\FinalPropertyPromotionRectorTest
*/
final class FinalPropertyPromotionRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var string
*/
private const TAGNAME = 'final';

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

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add native final promoted properties in non-final class to avoid child to override the promoted properties based on `@final` tag', [
new CodeSample(
<<<'CODE_SAMPLE'
public function __construct(
/** @final */
public string $id
) {}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
public function __construct(
final public string $id
) {}
CODE_SAMPLE
),
]);
}

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

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isFinal()) {
return null;
}

if ($node->isAnonymous()) {
return null;
}

$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);

if (! $constructClassMethod instanceof ClassMethod) {
return null;
}

$hasChanged = false;
$params = $constructClassMethod->getParams();

foreach ($params as $param) {
if (! $param->isPromoted()) {
continue;
}

if ($this->visibilityManipulator->hasVisibility($param, Visibility::FINAL)) {
continue;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($param);

if (! $phpDocInfo->hasByName(self::TAGNAME)) {
continue;
}
$hasChanged = true;
$this->visibilityManipulator->makeFinal($param);
$phpDocInfo->removeByName(self::TAGNAME);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($param);
}

if($hasChanged){
return $node;
}
return null;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::FINAL_PROPERTY_PROMOTION;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,12 @@ final class PhpVersionFeature
*/
public const DEPRECATED_NULL_DEBUG_INFO_RETURN = PhpVersion::PHP_85;

/**
* @see https://wiki.php.net/rfc/attributes-on-constants
* @var int
*/
public const FINAL_PROPERTY_PROMOTION = PhpVersion::PHP_85;

/**
* @see https://wiki.php.net/rfc/attributes-on-constants
* @var int
Expand Down
Loading