Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 config/set/downgrade-php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector;
use Rector\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
Expand All @@ -14,6 +15,7 @@
$rectorConfig->phpVersion(PhpVersion::PHP_84);
$rectorConfig->rules([
DowngradeArrayFirstLastRector::class,
DowngradeFinalPropertyPromotionRector::class,
]);

// https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_driver_specific_pdo_constants_and_methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector;

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

final class DowngradeFinalPropertyPromotionRectorTest 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,26 @@
<?php

namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture;

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

?>
-----
<?php

namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture;

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

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

namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture;

class Fixture
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class Fixture
class SkipFixtureReadonly

{
public function __construct(readonly string $id)
{
}
}

?>
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\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeFinalPropertyPromotionRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp85\Rector\Class_;

use PhpParser\Comment\Doc;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Trait_;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/final_promotion
*
* @see \Rector\Tests\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector\DowngradeFinalPropertyPromotionRectorTest
*/
final class DowngradeFinalPropertyPromotionRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change constructor final property promotion to @final annotation assign', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(
final public string $id
){}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(
/** @final */
public string $id
) {}
}
CODE_SAMPLE
),
]);
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Node\Stmt\ClassMethod

}

/**
* @param Class_|Trait_ $node
*/
public function refactor(Node $node): null
Comment thread
samsonasik marked this conversation as resolved.
Outdated
{
$constructorClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (! $constructorClassMethod instanceof ClassMethod) {
return null;
}
Comment thread
samsonasik marked this conversation as resolved.

Comment thread
samsonasik marked this conversation as resolved.
foreach ($constructorClassMethod->params as $promotedParam) {
if (($promotedParam->flags & Modifiers::FINAL) !== 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ->isPromoted()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Correct ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visibility::FINAL

$promotedParam->flags &= ~Modifiers::FINAL;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The makeNonFinal method is accepting Class_|ClassMethod, not accepting param

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should add Param as well on rector-src then


$existingDoc = $promotedParam->getDocComment();
if (! $existingDoc) {
$promotedParam->setDocComment(new Doc('/** @final */'));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use docblockUpdater instead, ensure existing doc when exists not removed, see example

private function addPhpDocTag(Property|Param $node): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
if ($phpDocInfo->hasByName(self::TAGNAME)) {
return false;
}
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode('')));
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return true;
}

}
}
Comment thread
samsonasik marked this conversation as resolved.
}

Comment thread
samsonasik marked this conversation as resolved.
return null;
Comment thread
samsonasik marked this conversation as resolved.
}
}