Skip to content

Commit

Permalink
feat: add an option to skip renaming promoted property (#5457)
Browse files Browse the repository at this point in the history
* feat: add an option to skip renaming promoted property

* fix: update code sample

* fix: update code sample

* feat: add generic test case
  • Loading branch information
GaryPEGEOT committed Jan 11, 2024
1 parent f24b01a commit ac072bc
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

class Fixture
{
public float $x;
public float $y;
public float $z;

public function __construct(
float $x = 0.0,
float $y = 0.0,
float $z = 0.0
) {
$this->x = $x;
$this->y = $y;
$this->z = $z;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

class Fixture
{
public function __construct(public float $x = 0.0, public float $y = 0.0, public float $z = 0.0)
{
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\Fixer;
use Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\SkipParentPublic as BaseSkipParentPublic;

final class SkipWhenNameIsDifferent
{
private Fixer $fixer;

public function __construct(Fixer $notSameName)
{
$this->fixer = $notSameName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;

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

final class RenamePropertyDisabledTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureRenamingDisabled');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule_disabled_renaming.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ClassPropertyAssignToConstructorPromotionRector::class, [
ClassPropertyAssignToConstructorPromotionRector::RENAME_PROPERTY => false,
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect
*/
public const INLINE_PUBLIC = 'inline_public';

/**
* @api
* @var string
*/
public const RENAME_PROPERTY = 'rename_property';

/**
* Default to false, which only apply changes:
*
Expand All @@ -62,6 +68,11 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect
*/
private bool $inlinePublic = false;

/**
* Set to false will skip property promotion when parameter and property have different names.
*/
private bool $renameProperty = true;

public function __construct(
private readonly PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver,
private readonly VariableRenamer $variableRenamer,
Expand Down Expand Up @@ -107,6 +118,7 @@ public function __construct(
,
[
ClassPropertyAssignToConstructorPromotionRector::INLINE_PUBLIC => false,
ClassPropertyAssignToConstructorPromotionRector::RENAME_PROPERTY => true,
]
),
]
Expand All @@ -115,7 +127,8 @@ public function __construct(

public function configure(array $configuration): void
{
$this->inlinePublic = $configuration[self::INLINE_PUBLIC] ?? (bool) current($configuration);
$this->inlinePublic = $configuration[self::INLINE_PUBLIC] ?? false;
$this->renameProperty = $configuration[self::RENAME_PROPERTY] ?? true;
}

/**
Expand Down Expand Up @@ -166,6 +179,15 @@ public function refactor(Node $node): ?Node
continue;
}

$paramName = $this->getName($param);

// rename also following calls
$propertyName = $this->getName($property->props[0]);

if (! $this->renameProperty && $paramName !== $propertyName) {
continue;
}

// remove property from class
$propertyStmtKey = $property->getAttribute(AttributeKey::STMT_KEY);
unset($node->stmts[$propertyStmtKey]);
Expand All @@ -174,11 +196,6 @@ public function refactor(Node $node): ?Node
$assignStmtPosition = $promotionCandidate->getStmtPosition();
unset($constructClassMethod->stmts[$assignStmtPosition]);

$paramName = $this->getName($param);

// rename also following calls
$propertyName = $this->getName($property->props[0]);

/** @var string $oldName */
$oldName = $this->getName($param->var);
$this->variableRenamer->renameVariableInFunctionLike($constructClassMethod, $oldName, $propertyName, null);
Expand Down

0 comments on commit ac072bc

Please sign in to comment.