Skip to content

Commit 3ad2c55

Browse files
committed
fix: Add classmap autoloading and create Rule interface for PHPStan
- Included "stub/" directory in autoload-dev classmap for better autoloading during development - Added new Rule interface in stub/phpstan/phpstan/src/Rules/Rule.php to define custom PHPStan rules and improve static analysis capabilities
1 parent 08bf5a3 commit 3ad2c55

File tree

7 files changed

+77
-14
lines changed

7 files changed

+77
-14
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/baselines/ export-ignore
3838
#/docs/ export-ignore
3939
#/examples/ export-ignore
40-
/stubs/ export-ignore
40+
/stub/ export-ignore
4141
/tests/ export-ignore
4242
/vendor-bin/ export-ignore
4343
/vendor/ export-ignore

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@
103103
"autoload-dev": {
104104
"psr-4": {
105105
"Guanguans\\PHPStanRulesTests\\": "tests/"
106-
}
106+
},
107+
"classmap": [
108+
"stub/"
109+
]
107110
},
108111
"config": {
109112
"allow-plugins": {
@@ -400,7 +403,7 @@
400403
"rector:process-only-dry-run": "@rector:process-only --dry-run",
401404
"roave-backward-compatibility-check": [
402405
"@putenv:php",
403-
"$PHP82 vendor/bin/roave-backward-compatibility-check --ansi -vv"
406+
"$PHP82 vendor/bin/roave-backward-compatibility-check --install-development-dependencies --ansi -vv"
404407
],
405408
"roave-backward-compatibility-check:format-github-actions": "@roave-backward-compatibility-check --format=github-actions",
406409
"rule-doc-generator": [

config/rules.neon

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ parameters:
2525
forbiddenSideEffects:
2626
enabled: %guanguans.allRules%
2727

28-
conditionalTags:
29-
Guanguans\PHPStanRules\Rule\Class_\ExceptionMustImplementNativeThrowableRule:
30-
phpstan.rules.rule: %guanguans.exceptionMustImplementNativeThrowable.enabled%
31-
# Guanguans\PHPStanRules\Rule\File\ForbiddenSideEffectsRule:
32-
# phpstan.rules.rule: %guanguans.forbiddenSideEffects.enabled%
33-
3428
services:
3529
-
3630
class: Guanguans\PHPStanRules\Rule\Class_\ExceptionMustImplementNativeThrowableRule
@@ -40,3 +34,9 @@ services:
4034
# class: staabm\SideEffectsDetector\SideEffectsDetector
4135
# -
4236
# class: Guanguans\PHPStanRules\Rule\File\ForbiddenSideEffectsRule
37+
38+
conditionalTags:
39+
Guanguans\PHPStanRules\Rule\Class_\ExceptionMustImplementNativeThrowableRule:
40+
phpstan.rules.rule: %guanguans.exceptionMustImplementNativeThrowable.enabled%
41+
# Guanguans\PHPStanRules\Rule\File\ForbiddenSideEffectsRule:
42+
# phpstan.rules.rule: %guanguans.forbiddenSideEffects.enabled%

config/rules.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
}
2020

2121
return [
22-
'conditionalTags' => [
23-
ForbiddenSideEffectsRule::class => [
24-
'phpstan.rules.rule' => '%guanguans.forbiddenSideEffects.enabled%',
25-
],
26-
],
2722
'services' => [
2823
[
2924
'class' => SideEffectsDetector::class,
@@ -32,4 +27,9 @@
3227
'class' => ForbiddenSideEffectsRule::class,
3328
],
3429
],
30+
'conditionalTags' => [
31+
ForbiddenSideEffectsRule::class => [
32+
'phpstan.rules.rule' => '%guanguans.forbiddenSideEffects.enabled%',
33+
],
34+
],
3535
];

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ classes(static fn (string $class, string $file): bool => str_starts_with($class,
203203
__DIR__.'/src/Rule/*Rule.php',
204204
],
205205
SortAssociativeArrayByKeyRector::class => [
206+
__DIR__.'/config/',
206207
__DIR__.'/src/',
207208
__DIR__.'/tests/',
208209
],
File renamed without changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2026 guanguans<ityaozm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*
11+
* @see https://github.com/guanguans/phpstan-rules
12+
*/
13+
14+
namespace PHPStan\Rules;
15+
16+
use PhpParser\Node;
17+
use PHPStan\Analyser\Scope;
18+
19+
if (interface_exists('PHPStan\Rules\Rule')) {
20+
return;
21+
}
22+
23+
/**
24+
* @see https://github.com/Roave/BackwardCompatibilityCheck
25+
* @see https://github.com/symplify/phpstan-rules/blob/main/stubs/
26+
* @see vendor/phpstan/phpstan/phpstan.phar/src/Rules/Rule.php
27+
*
28+
* This is the interface custom rules implement. To register it in the configuration file
29+
* use the `phpstan.rules.rule` service tag:
30+
*
31+
* ```
32+
* services:
33+
* -
34+
* class: App\MyRule
35+
* tags:
36+
* - phpstan.rules.rule
37+
* ```
38+
*
39+
* Learn more: https://phpstan.org/developing-extensions/rules
40+
*
41+
* @api
42+
*
43+
* @template TNodeType of Node
44+
*/
45+
interface Rule
46+
{
47+
/**
48+
* @return class-string<TNodeType>
49+
*/
50+
public function getNodeType(): string;
51+
52+
/**
53+
* @param TNodeType $node
54+
* @param \PHPStan\Analyser\NodeCallbackInvoker&\PHPStan\Analyser\Scope $scope
55+
*
56+
* @return list<IdentifierRuleError>
57+
*/
58+
public function processNode(Node $node, Scope $scope): array;
59+
}

0 commit comments

Comments
 (0)