Skip to content

Commit 96ef5b1

Browse files
committed
feat(rules): Add PHPStan configuration for neon file handling
- Introduced PHPStan configuration to include 'config/extension.neon'. - Updated composer scripts to include commands for fixing and listing neon files. - Enhanced the README with instructions for running the new PHPStan commands.
1 parent 87fa6a9 commit 96ef5b1

31 files changed

+315
-238
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
/baselines/ export-ignore
3838
#/docs/ export-ignore
3939
#/examples/ export-ignore
40+
/stubs/ export-ignore
4041
/tests/ export-ignore
4142
/vendor-bin/ export-ignore
4243
/vendor/ export-ignore

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,31 @@
2121
composer require guanguans/phpstan-rules --dev --ansi -v
2222
```
2323

24-
## Usage
24+
If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!
2525

26-
### :monocle_face: [Rules Overview](docs/rules-overview.md)
26+
<details>
27+
<summary>Manual installation</summary>
2728

28-
### In your rector configuration register rules
29+
If you don't want to use `phpstan/extension-installer`, include rules.neon in your project's PHPStan config:
2930

30-
```php
31-
<?php
31+
```neon
32+
includes:
33+
- vendor/guanguans/phpstan-rules/config/rules.neon
3234
```
35+
</details>
36+
37+
## Usage
38+
39+
Parameter configuration refer to the parameter section the configuration file [[config/rules.neon](config/rules.neon)].
40+
41+
You can also refer to the configuration file [tests/Rule/.../.../config/configured_rule.neon] in the tests directory.
3342

3443
## Composer scripts
3544

3645
```shell
3746
composer checks:required
3847
composer php-cs-fixer:fix
48+
composer phpstan-rules:fix-neon-files
3949
composer test
4050
```
4151

baselines/loader.neon

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# total 5 errors
1+
# total 8 errors
22

33
includes:
4-
- shipmonk.checkedExceptionInCallable.neon
4+
- phpstanApi.runtimeReflection.neon
5+
- rector.noInstanceOfStaticReflection.neon
56
- symplify.forbiddenFuncCall.neon
7+
- symplify.forbiddenStaticClassConstFetch.neon
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# total 4 errors
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Function is_a\(\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\. Use objects retrieved from ReflectionProvider instead\.$#'
7+
count: 1
8+
path: ../src/Rule/Class_/ExceptionMustImplementNativeThrowableRule.php
9+
10+
-
11+
message: '#^Function is_subclass_of\(\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\. Use objects retrieved from ReflectionProvider instead\.$#'
12+
count: 3
13+
path: ../src/Rule/Class_/ExceptionMustImplementNativeThrowableRule.php
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# total 1 error
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Instead of "instanceof/is_a\(\)" use ReflectionProvider service or "\(new ObjectType\(\<desired_type\>\)\)\-\>isSuperTypeOf\(\<element_type\>\)" for static reflection to work$#'
7+
count: 1
8+
path: ../src/Rule/Class_/ExceptionMustImplementNativeThrowableRule.php

baselines/shipmonk.checkedExceptionInCallable.neon

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# total 1 error
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Avoid static access of constants, as they can change value\. Use interface and contract method instead$#'
7+
count: 1
8+
path: ../src/Rule/AbstractRule.php

composer-dependency-analyser.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,19 @@
3535
],
3636
[ErrorType::SHADOW_DEPENDENCY],
3737
)
38-
->ignoreErrorsOnPackages(
39-
[
40-
// 'illuminate/support',
41-
],
42-
[ErrorType::UNUSED_DEPENDENCY]
43-
)
44-
->ignoreErrorsOnPackageAndPath(
45-
'staabm/side-effects-detector',
46-
__DIR__.'/src/Rule/File/ForbiddenSideEffectsRule.php',
47-
[ErrorType::DEV_DEPENDENCY_IN_PROD]
48-
)
4938
->ignoreErrorsOnPackages(
5039
[
5140
/**
5241
* @see https://github.com/phpstan/phpstan-src/tree/2.1.x/build/
53-
* @see vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/
42+
* @see vendor/phpstan/phpstan/phpstan.phar/vendor/
5443
*/
5544
'illuminate/collections',
56-
// 'rector/rector',
57-
// 'symfony/polyfill-php80',
58-
// 'symfony/polyfill-php81',
59-
// 'webmozart/assert',
6045
'nikic/php-parser',
6146
],
6247
[ErrorType::SHADOW_DEPENDENCY]
48+
)
49+
->ignoreErrorsOnPackageAndPath(
50+
'staabm/side-effects-detector',
51+
__DIR__.'/src/Rule/File/ForbiddenSideEffectsRule.php',
52+
[ErrorType::DEV_DEPENDENCY_IN_PROD]
6353
);

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@
138138
"pre-commit": [
139139
"composer checks:required"
140140
]
141+
},
142+
"phpstan": {
143+
"includes": [
144+
"config/rules.neon"
145+
]
141146
}
142147
},
143148
"scripts": {
@@ -176,8 +181,6 @@
176181
"@jsonlint",
177182
"@phpmnd",
178183
"@phpstan:analyse",
179-
"@phpstan-rules:fix-neon-files",
180-
"@phpstan-rules:list-files",
181184
"@rector:process-dry-run",
182185
"@sk:check-commented-code",
183186
"@sk:check-conflicts",
@@ -200,6 +203,8 @@
200203
"checks:required": [
201204
"@putenv:xdebug-off",
202205
"@php-cs-fixer:fix-dry-run",
206+
"@phpstan-rules:fix-neon-files",
207+
"@phpstan-rules:list-files",
203208
"@pest"
204209
],
205210
"class-leak": "@php vendor/bin/class-leak --ansi -vv",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# https://github.com/symplify/phpstan-rules/tree/main/config/
33

44
conditionalTags:
5-
Guanguans\PHPStanRules\Rule\New_\ExceptionMustImplementNativeThrowableRule:
5+
Guanguans\PHPStanRules\Rule\Class_\ExceptionMustImplementNativeThrowableRule:
66
phpstan.rules.rule: %guanguans.exceptionMustImplementNativeThrowable.enabled%
77
Guanguans\PHPStanRules\Rule\File\ForbiddenSideEffectsRule:
88
phpstan.rules.rule: %guanguans.forbiddenSideEffects.enabled%
@@ -33,7 +33,7 @@ services:
3333
class: staabm\SideEffectsDetector\SideEffectsDetector
3434

3535
-
36-
class: Guanguans\PHPStanRules\Rule\New_\ExceptionMustImplementNativeThrowableRule
36+
class: Guanguans\PHPStanRules\Rule\Class_\ExceptionMustImplementNativeThrowableRule
3737
arguments:
3838
nativeThrowable: %guanguans.exceptionMustImplementNativeThrowable.nativeThrowable%
3939
-

0 commit comments

Comments
 (0)