-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from DaveLiddament/feature/add-rule-to-check-r…
…ules-in-extension-neon Internal: Add rule to check rules in extension neon
- Loading branch information
Showing
5 changed files
with
167 additions
and
4 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,74 @@ | ||
<?php | ||
|
||
namespace DaveLiddament\PhpstanPhpLanguageExtensions\Build\PHPStan\Rules; | ||
|
||
use Nette\Neon\Neon; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** @implements Rule<InClassNode> */ | ||
final class CheckRuleIsInExtension implements Rule | ||
{ | ||
/** @var list<string> */ | ||
private array $classes; | ||
|
||
public function __construct() | ||
{ | ||
$file = Neon::decodeFile(__DIR__.'/../../../extension.neon'); | ||
|
||
if (!is_array($file)) { | ||
throw new \Exception('Expecting neon file to be parseable'); | ||
} | ||
|
||
$services = $file['services'] ?? []; | ||
|
||
$classes = []; | ||
foreach ($services as $service) { | ||
$class = $service['class'] ?? null; | ||
if (null === $class) { | ||
continue; | ||
} | ||
$classes[] = $class; | ||
} | ||
|
||
$this->classes = $classes; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $scope->getClassReflection(); | ||
if (null === $classReflection) { | ||
return []; | ||
} | ||
|
||
if (!$classReflection->isSubclassOf(Rule::class)) { | ||
return []; | ||
} | ||
|
||
if ($classReflection->isAbstract()) { | ||
return []; | ||
} | ||
|
||
$className = $classReflection->getName(); | ||
|
||
if (str_starts_with(haystack: $className, needle: 'DaveLiddament\PhpstanPhpLanguageExtensions\Build')) { | ||
return []; | ||
} | ||
|
||
if (in_array($className, $this->classes)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message("Rule [$className] not in extension.neon.")->build(), | ||
]; | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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