Skip to content

Commit

Permalink
Fix ClassPropertyAssignToConstructorPromotionRector to skip propertie…
Browse files Browse the repository at this point in the history
…s that have attributes (#468)

* Add failing test fixture for ClassPropertyAssignToConstructorPromotionRector

# Failing Test for ClassPropertyAssignToConstructorPromotionRector

Based on https://getrector.org/demo/1ebe95c8-2d22-6d0e-b6ff-7be843d783d4

The ClassPropertyAssignToConstructorPromotionRector should ignore properties that have properties linked to them. Looking at the demo it seems to have gotten rid of both properties, which I did not expect/want it to do

* Update coordinate.php.inc

reverse order of use statement and namespace

* fix: skip properties that have attributes

* copy over attrGroups from  to  to keep them

* added test case for multiple attributes

Co-authored-by: Dominik Peters <d.peters@billiger-mietwagen.de>
  • Loading branch information
Dominik Peters and Dominik Peters committed Jul 22, 2021
1 parent 99e5c8a commit 0f9dd92
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

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

use JetBrains\PhpStorm\Immutable;

final class Coordinate
{
#[Immutable]
public float $latitude;

public float $longitude;

public function __construct(float $latitude, float $longitude)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
}
}
?>
-----
<?php

declare(strict_types=1);

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

use JetBrains\PhpStorm\Immutable;

final class Coordinate
{
public function __construct(#[Immutable] public float $latitude, public float $longitude)
{
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

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

use JetBrains\PhpStorm\Immutable;

final class Coordinate
{
#[Immutable]
public float $latitude;

public function __construct(float $latitude)
{
$this->latitude = $latitude;
}
}
?>
-----
<?php

declare(strict_types=1);

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

use JetBrains\PhpStorm\Immutable;

final class Coordinate
{
public function __construct(#[Immutable] public float $latitude)
{
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

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

use JetBrains\PhpStorm\Deprecated;
use JetBrains\PhpStorm\Immutable;

final class Coordinate
{
#[Immutable]
#[Deprecated]
public float $latitude;

public function __construct(float $latitude)
{
$this->latitude = $latitude;
}
}
?>
-----
<?php

declare(strict_types=1);

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

use JetBrains\PhpStorm\Deprecated;
use JetBrains\PhpStorm\Immutable;

final class Coordinate
{
public function __construct(#[Immutable] #[Deprecated] public float $latitude)
{
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public function refactor(Node $node): ?Node
$propertyName = $this->getName($property);
$param->var->name = $propertyName;
$param->flags = $property->flags;
// Copy over attributes of the "old" property
$param->attrGroups = $property->attrGroups;
$this->processNullableType($property, $param);
}

Expand Down

0 comments on commit 0f9dd92

Please sign in to comment.