-
-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7871e1
commit 160034e
Showing
5 changed files
with
172 additions
and
6 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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
<?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\ValueObject\PhpDocAttributeKey; | ||
use Rector\Core\Configuration\CurrentNodeProvider; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
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 | ||
{ | ||
public function __construct( | ||
private CurrentNodeProvider $currentNodeProvider, | ||
private NameScopeFactory $nameScopeFactory, | ||
private PhpDocNodeTraverser $phpDocNodeTraverser | ||
) { | ||
} | ||
|
||
public function decorate(PhpDocNode $phpDocNode): void | ||
{ | ||
$phpNode = $this->currentNodeProvider->getNode(); | ||
|
||
if (! $phpNode instanceof PhpNode) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function (Node $node) use ( | ||
$phpNode | ||
): int|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 $node): ?string | ||
{ | ||
if (! $constExprNode instanceof ConstFetchNode) { | ||
return null; | ||
} | ||
|
||
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($node); | ||
return $nameScope->resolveStringName($constExprNode->className); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...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,33 @@ | ||
<?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(Gear $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'; | ||
} |
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