Skip to content

Commit 22ce946

Browse files
authored
[DowngradePhp80] Handle on trait on DowngradePropertyPromotionRector (#282)
1 parent 9650da2 commit 22ce946

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector\Fixture;
4+
5+
trait OnTrait
6+
{
7+
public function __construct(public float $value = 0.0)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector\Fixture;
17+
18+
trait OnTrait
19+
{
20+
public float $value = 0.0;
21+
public function __construct(float $value = 0.0)
22+
{
23+
$this->value = $value;
24+
}
25+
}
26+
27+
?>

rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PhpParser\Node\Stmt\Expression;
1616
use PhpParser\Node\Stmt\Property;
17+
use PhpParser\Node\Stmt\Trait_;
1718
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
1819
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1920
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
@@ -75,11 +76,11 @@ public function __construct(float $value = 0.0)
7576
*/
7677
public function getNodeTypes(): array
7778
{
78-
return [Class_::class];
79+
return [Class_::class, Trait_::class];
7980
}
8081

8182
/**
82-
* @param Class_ $node
83+
* @param Class_|Trait_ $node
8384
*/
8485
public function refactor(Node $node): ?Node
8586
{
@@ -163,7 +164,7 @@ private function setParamAttrGroupAsComment(Param $param): void
163164
private function resolvePropertiesFromPromotedParams(
164165
ClassMethod $classMethod,
165166
array $promotedParams,
166-
Class_ $class
167+
Class_|Trait_ $class
167168
): array {
168169
$properties = $this->createPropertiesFromParams($classMethod, $promotedParams);
169170
$class->stmts = array_merge($properties, $class->stmts);
@@ -177,7 +178,7 @@ private function resolvePropertiesFromPromotedParams(
177178
*/
178179
private function addPropertyAssignsToConstructorClassMethod(
179180
array $properties,
180-
Class_ $class,
181+
Class_|Trait_ $class,
181182
array $oldComments
182183
): void {
183184
$assigns = [];

0 commit comments

Comments
 (0)