Skip to content

Commit

Permalink
Merge pull request #23 from DaveLiddament/feature/add-rule-to-check-r…
Browse files Browse the repository at this point in the history
…ules-in-extension-neon

Internal: Add rule to check rules in extension neon
  • Loading branch information
DaveLiddament authored Aug 12, 2024
2 parents 8b1cf40 + ce41804 commit 2d30213
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 4 deletions.
74 changes: 74 additions & 0 deletions build/PHPStan/Rules/CheckRuleIsInExtension.php
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(),
];
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"phpunit/phpunit": "^9.6.12",
"friendsofphp/php-cs-fixer": "^3.26.1",
"php-parallel-lint/php-parallel-lint": "^1.3.2",
"dave-liddament/phpstan-rule-test-helper": "^0.3.0"
"dave-liddament/phpstan-rule-test-helper": "^0.3.0",
"nette/neon": "^3.4"
},
"license": "MIT",
"autoload": {
Expand All @@ -22,7 +23,8 @@
},
"autoload-dev": {
"psr-4": {
"DaveLiddament\\PhpstanPhpLanguageExtensions\\Tests\\": "tests/"
"DaveLiddament\\PhpstanPhpLanguageExtensions\\Tests\\": "tests/",
"DaveLiddament\\PhpstanPhpLanguageExtensions\\Build\\": "build/"
},
"classmap": [
"tests/Rules/data"
Expand Down
70 changes: 69 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ services:
tags:
- phpstan.rules.rule

-
class: DaveLiddament\PhpstanPhpLanguageExtensions\Rules\MustUseResultRule
tags:
- phpstan.rules.rule

-
class: DaveLiddament\PhpstanPhpLanguageExtensions\Rules\RestrictTraitToRule
tags:
- phpstan.rules.rule



parametersSchema:
phpLanguageExtensions: structure([
Expand Down
10 changes: 9 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ parameters:
paths:
- src
- tests
- build
excludePaths:
- tests/Rules/data
- tests/Rules/data

services:

-
class: DaveLiddament\PhpstanPhpLanguageExtensions\Build\PHPStan\Rules\CheckRuleIsInExtension
tags:
- phpstan.rules.rule

0 comments on commit 2d30213

Please sign in to comment.