-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Downgrade] [PHP 8.0] Add DowngradePropertyPromotionToConstructorProp…
…ertyAssignRector (#4762) * [Downgrade] [PHP 8.0] Add DowngradePropertyPromotionToConstructorPropertyAssignRector * fix parent * various fixes for PHP 8.0 tests * [ci-review] Rector Rectify * Update rules/downgrade-php80/tests/Rector/Class_/DowngradePropertyPromotionToConstructorPropertyAssignRector/DowngradePropertyPromotionToConstructorPropertyAssignRectorTest.php Co-authored-by: rector-bot <tomas@getrector.org>
- Loading branch information
1 parent
1f5876e
commit 09f17a7
Showing
16 changed files
with
286 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
/vendor | ||
composer.lock | ||
stubs | ||
|
||
# PHPStorm meta files | ||
.idea/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ty/tests/Rector/Assign/CombinedAssignRector/Source/AbstractSomeClassSoParentCanBeUsed.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Assign\CombinedAssignRector\Source; | ||
|
||
class AbstractSomeClassSoParentCanBeUsed | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...e-php80/src/Rector/Class_/DowngradePropertyPromotionToConstructorPropertyAssignRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Property; | ||
use Rector\Core\PhpParser\Node\Manipulator\ClassInsertManipulator; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\MethodName; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see https://wiki.php.net/rfc/constructor_promotion | ||
* | ||
* @see \Rector\DowngradePhp80\Tests\Rector\Class_\DowngradePropertyPromotionToConstructorPropertyAssignRector\DowngradePropertyPromotionToConstructorPropertyAssignRectorTest | ||
*/ | ||
final class DowngradePropertyPromotionToConstructorPropertyAssignRector extends AbstractRector | ||
{ | ||
/** | ||
* @var ClassInsertManipulator | ||
*/ | ||
private $classInsertManipulator; | ||
|
||
public function __construct(ClassInsertManipulator $classInsertManipulator) | ||
{ | ||
$this->classInsertManipulator = $classInsertManipulator; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change constructor property promotion to property asssign', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function __construct(public float $value = 0.0) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public float $value; | ||
public function __construct(float $value = 0.0) | ||
{ | ||
$this->value = $value; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$promotedParams = $this->resolvePromotedParams($node); | ||
if ($promotedParams === []) { | ||
return null; | ||
} | ||
|
||
$properties = $this->addPropertiesFromParams($promotedParams, $node); | ||
|
||
$this->addPropertyAssignsToConstructorClassMethod($properties, $node); | ||
|
||
foreach ($promotedParams as $promotedParam) { | ||
$promotedParam->flags = 0; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* @return Param[] | ||
*/ | ||
private function resolvePromotedParams(Class_ $class): array | ||
{ | ||
$constructorClassMethod = $class->getMethod(MethodName::CONSTRUCT); | ||
if ($constructorClassMethod === null) { | ||
return []; | ||
} | ||
|
||
$promotedParams = []; | ||
|
||
foreach ($constructorClassMethod->params as $param) { | ||
if ($param->flags === 0) { | ||
continue; | ||
} | ||
|
||
$promotedParams[] = $param; | ||
} | ||
|
||
return $promotedParams; | ||
} | ||
|
||
/** | ||
* @param Param[] $promotedParams | ||
* @return Property[] | ||
*/ | ||
private function addPropertiesFromParams(array $promotedParams, Class_ $class): array | ||
{ | ||
$properties = $this->createPropertiesFromParams($promotedParams); | ||
$this->classInsertManipulator->addPropertiesToClass($class, $properties); | ||
|
||
return $properties; | ||
} | ||
|
||
/** | ||
* @param Property[] $properties | ||
*/ | ||
private function addPropertyAssignsToConstructorClassMethod(array $properties, Class_ $class): void | ||
{ | ||
$assigns = []; | ||
|
||
foreach ($properties as $property) { | ||
$propertyName = $this->getName($property); | ||
$assign = $this->nodeFactory->createPropertyAssignment($propertyName); | ||
$assigns[] = new Expression($assign); | ||
} | ||
|
||
/** @var ClassMethod $constructorClassMethod */ | ||
$constructorClassMethod = $class->getMethod(MethodName::CONSTRUCT); | ||
$constructorClassMethod->stmts = array_merge($assigns, (array) $constructorClassMethod->stmts); | ||
} | ||
|
||
/** | ||
* @param Param[] $params | ||
* @return Property[] | ||
*/ | ||
private function createPropertiesFromParams(array $params): array | ||
{ | ||
$properties = []; | ||
|
||
foreach ($params as $param) { | ||
/** @var string $name */ | ||
$name = $this->getName($param->var); | ||
|
||
$property = $this->nodeFactory->createProperty($name); | ||
$property->flags = $param->flags; | ||
$property->type = $param->type; | ||
|
||
$properties[] = $property; | ||
} | ||
|
||
return $properties; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...rPropertyAssignRector/DowngradePropertyPromotionToConstructorPropertyAssignRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\Tests\Rector\Class_\DowngradePropertyPromotionToConstructorPropertyAssignRector; | ||
|
||
use Iterator; | ||
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionToConstructorPropertyAssignRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradePropertyPromotionToConstructorPropertyAssignRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @requires PHP 8.0 | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return DowngradePropertyPromotionToConstructorPropertyAssignRector::class; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...lass_/DowngradePropertyPromotionToConstructorPropertyAssignRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Rector\DowngradePhp80\Tests\Rector\Class_\DowngradePropertyPromotionToConstructorPropertyAssignRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function __construct(public float $value = 0.0) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\DowngradePhp80\Tests\Rector\Class_\DowngradePropertyPromotionToConstructorPropertyAssignRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public float $value; | ||
public function __construct(float $value = 0.0) | ||
{ | ||
$this->value = $value; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.