-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 4f098c1
Showing
5 changed files
with
141 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Psalm Static Plugin | ||
|
||
This plugin helps to resolve `static::class`, `self::class` and `paren::class` statements. |
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,21 @@ | ||
{ | ||
"name": "h4ck3r31/psalm-static-plugin", | ||
"description": "Resolves static::class, self:class or parent::class references within PsalmPHP", | ||
"type": "psalm-plugin", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"H4ck3r31\\PsalmStaticPlugin\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Oliver Hader", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"ext-simplexml": "*", | ||
"vimeo/psalm": "dev-master" | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace H4ck3r31\PsalmStaticPlugin; | ||
|
||
use Psalm\Plugin\PluginEntryPointInterface; | ||
use Psalm\Plugin\RegistrationInterface; | ||
use SimpleXMLElement; | ||
|
||
class Plugin implements PluginEntryPointInterface | ||
{ | ||
public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement $config = null): void | ||
{ | ||
class_exists(SpecialClassNameAnalysisHandler::class); | ||
$registration->registerHooksFromClass(SpecialClassNameAnalysisHandler::class); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace H4ck3r31\PsalmStaticPlugin; | ||
|
||
use PhpParser\NodeTraverser; | ||
use Psalm\Plugin\EventHandler\BeforeFileAnalysisInterface; | ||
use Psalm\Plugin\EventHandler\Event\BeforeFileAnalysisEvent; | ||
|
||
class SpecialClassNameAnalysisHandler implements BeforeFileAnalysisInterface | ||
{ | ||
public static function beforeAnalyzeFile(BeforeFileAnalysisEvent $event): void | ||
{ | ||
$filePath = $event->getStatementsSource()->getFilePath(); | ||
$statements = $event->getCodebase()->getStatementsForFile($filePath); | ||
// resolves special class names (`static::class`, `self::class`, `parent::class`) | ||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor(new SpecialClassNameVisitor()); | ||
$traverser->traverse($statements); | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace H4ck3r31\PsalmStaticPlugin; | ||
|
||
use PhpParser\ErrorHandler\Throwing; | ||
use PhpParser\NameContext; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
/** | ||
* Resolves and updates `resolvedName` node attribute for special class constants | ||
* `self::class`, `static::class` and `parent::class` | ||
*/ | ||
class SpecialClassNameVisitor extends NodeVisitorAbstract | ||
{ | ||
private NameContext $nameContext; | ||
private ?Class_ $classContext = null; | ||
|
||
public function __construct() | ||
{ | ||
$this->nameContext = new NameContext(new Throwing()); | ||
} | ||
|
||
public function beforeTraverse(array $nodes): ?array | ||
{ | ||
$this->nameContext->startNamespace(); | ||
return null; | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof Namespace_) { | ||
$this->nameContext->startNamespace($node->name); | ||
} elseif ($node instanceof Class_ && $node->name !== null) { | ||
$this->classContext = $node; | ||
$this->addNamespacedName($node); | ||
} elseif ($node instanceof ClassConstFetch | ||
&& $this->classContext !== null | ||
&& $node->class instanceof Name | ||
&& $node->class->isSpecialClassName() | ||
) { | ||
$resoledName = null; | ||
$currentName = $node->class->getAttribute('resolvedName') | ||
?? $node->class->toString(); | ||
if ($currentName === 'parent') { | ||
$resoledName = $this->classContext->extends->getAttribute('resolvedName') | ||
?? $this->classContext->extends->toString(); | ||
} elseif ($currentName === 'self' || $currentName === 'static') { | ||
$resoledName = $this->classContext->namespacedName->toString(); | ||
} | ||
if ($resoledName !== null) { | ||
$node->class->setAttribute('resolvedName', $resoledName); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public function leaveNode(Node $node): ?Node | ||
{ | ||
if ($node instanceof Class_) { | ||
$this->classContext = null; | ||
} | ||
return null; | ||
} | ||
|
||
private function addNamespacedName(ClassLike $node): void { | ||
if ($node->namespacedName !== null) { | ||
return; | ||
} | ||
$node->namespacedName = Name::concat( | ||
$this->nameContext->getNamespace(), (string) $node->name); | ||
} | ||
} |