-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.0] Add method param to ConstantListClassToEnumRector (#2415)
* add skipped fixtures * skip non-scalar types * prepare parameter update * add ConstExprClassNameDecorator, add PhpDocNodeDecoratorInterface
- Loading branch information
1 parent
ba0869a
commit a73dafd
Showing
17 changed files
with
406 additions
and
34 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
13 changes: 13 additions & 0 deletions
13
packages/BetterPhpDocParser/Contract/PhpDocParser/PhpDocNodeDecoratorInterface.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\BetterPhpDocParser\Contract\PhpDocParser; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; | ||
|
||
interface PhpDocNodeDecoratorInterface | ||
{ | ||
public function decorate(PhpDocNode $phpDocNode, Node $phpNode): void; | ||
} |
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
57 changes: 57 additions & 0 deletions
57
packages/BetterPhpDocParser/PhpDocParser/ConstExprClassNameDecorator.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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\BetterPhpDocParser\PhpDocParser; | ||
|
||
use PhpParser\Node as PhpNode; | ||
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode; | ||
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode; | ||
use PHPStan\PhpDocParser\Ast\Node; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; | ||
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface; | ||
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey; | ||
use Rector\StaticTypeMapper\Naming\NameScopeFactory; | ||
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser; | ||
|
||
/** | ||
* Decorate node with fully qualified class name for const epxr, | ||
* e.g. Direction::* | ||
*/ | ||
final class ConstExprClassNameDecorator implements PhpDocNodeDecoratorInterface | ||
{ | ||
public function __construct( | ||
private readonly NameScopeFactory $nameScopeFactory, | ||
private readonly PhpDocNodeTraverser $phpDocNodeTraverser | ||
) { | ||
} | ||
|
||
public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode): void | ||
{ | ||
$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function (Node $node) use ( | ||
$phpNode | ||
): Node|null { | ||
if (! $node instanceof ConstExprNode) { | ||
return null; | ||
} | ||
|
||
$className = $this->resolveFullyQualifiedClass($node, $phpNode); | ||
if ($className === null) { | ||
return null; | ||
} | ||
|
||
$node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className); | ||
return $node; | ||
}); | ||
} | ||
|
||
private function resolveFullyQualifiedClass(ConstExprNode $constExprNode, PhpNode $phpNode): ?string | ||
{ | ||
if (! $constExprNode instanceof ConstFetchNode) { | ||
return null; | ||
} | ||
|
||
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode); | ||
return $nameScope->resolveStringName($constExprNode->className); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
...tests/Php80/Rector/Class_/ConstantListClassToEnumRector/Fixture/change_param_type.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,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
use Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Source\Gear; | ||
|
||
final class ChangeParamType | ||
{ | ||
/** | ||
* @param Gear::* $gear | ||
*/ | ||
public function changeGear(string $gear) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
use Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Source\Gear; | ||
|
||
final class ChangeParamType | ||
{ | ||
public function changeGear(\Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Source\Gear $gear) | ||
{ | ||
} | ||
} | ||
|
||
?> |
38 changes: 38 additions & 0 deletions
38
.../Php80/Rector/Class_/ConstantListClassToEnumRector/Fixture/multiple_params_change.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,38 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
use Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Source\Gear; | ||
|
||
final class MultipleParamsChange | ||
{ | ||
/** | ||
* @param string $carType | ||
* @param Gear::* $gear | ||
* @param int $speed | ||
*/ | ||
public function changeGear($carType, string $gear, $speed) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
use Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Source\Gear; | ||
|
||
final class MultipleParamsChange | ||
{ | ||
/** | ||
* @param string $carType | ||
* @param int $speed | ||
*/ | ||
public function changeGear($carType, \Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Source\Gear $gear, $speed) | ||
{ | ||
} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...hp80/Rector/Class_/ConstantListClassToEnumRector/Fixture/skip_also_other_elements.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,12 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
final class SkipAlsoOtherElements | ||
{ | ||
public const LEFT = 'left'; | ||
|
||
public const RIGHT = 'right'; | ||
|
||
protected $value; | ||
} |
10 changes: 10 additions & 0 deletions
10
...sts/Php80/Rector/Class_/ConstantListClassToEnumRector/Fixture/skip_differnet_type.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
final class SkipDifferentType | ||
{ | ||
public const LEFT = 'left'; | ||
|
||
public const RIGHT = 5; | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/Php80/Rector/Class_/ConstantListClassToEnumRector/Fixture/skip_non_public_const.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
final class SkipNonPublicConst | ||
{ | ||
public const LEFT = 'left'; | ||
|
||
protected const RIGHT = 5; | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/Php80/Rector/Class_/ConstantListClassToEnumRector/Fixture/skip_non_scalar_types.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
final class SkipNonScalarTypes | ||
{ | ||
public const LEFT = self::class; | ||
|
||
public const RIGHT = self::class; | ||
} |
13 changes: 13 additions & 0 deletions
13
...ests/Php80/Rector/Class_/ConstantListClassToEnumRector/Fixture/skip_unknown_class.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Fixture; | ||
|
||
final class SkipUnknownClass | ||
{ | ||
/** | ||
* @param AnythingNonExisting::* $gear | ||
*/ | ||
public function changeGear(string $gear) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
rules-tests/Php80/Rector/Class_/ConstantListClassToEnumRector/Source/Gear.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\ConstantListClassToEnumRector\Source; | ||
|
||
final class Gear | ||
{ | ||
public const FIRST = 'first'; | ||
|
||
public const SECOND = 'second'; | ||
} |
Oops, something went wrong.