Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Anonymous class node has valid class name
- Loading branch information
1 parent
f217e5d
commit e05ed66
Showing
5 changed files
with
78 additions
and
7 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
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
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,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Broker\Broker; | ||
use PHPStan\Rules\Rule; | ||
|
||
class AnonymousClassNameRule implements Rule | ||
{ | ||
|
||
/** @var Broker */ | ||
private $broker; | ||
|
||
public function __construct(Broker $broker) | ||
{ | ||
$this->broker = $broker; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Class_::class; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
* @param Scope $scope | ||
* @return string[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$className = isset($node->namespacedName) | ||
? (string) $node->namespacedName | ||
: (string) $node->name; | ||
try { | ||
$this->broker->getClass($className); | ||
} catch (\PHPStan\Broker\ClassNotFoundException $e) { | ||
return ['not found']; | ||
} | ||
|
||
return ['found']; | ||
} | ||
|
||
} |
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,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
class AnonymousClassNameRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$broker = $this->createBroker(); | ||
return new AnonymousClassNameRule($broker); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/anonymous-class-name.php'], [ | ||
[ | ||
'found', | ||
6, | ||
], | ||
]); | ||
} | ||
|
||
} |