Skip to content

Commit 4fa8bc9

Browse files
committed
feat(rules): Enhance ForbiddenSideEffectsFunctionLikeRule for file nodes
- Change the node type from FunctionLike to FileNode for better side-effect detection. - Refactor processNode to handle file content and extract side effects more effectively. - Introduce rawProcessNode to recursively process statements within the file. - Improve error message handling by including the start line of the node.
1 parent c457273 commit 4fa8bc9

24 files changed

+426
-275
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# phpstan-rules
22

33
> [!NOTE]
4-
> A set of phpstan/phpstan rules. - 一套 phpstan/phpstan 规则
4+
> A set of additional rules for phpstan/phpstan. - 一套针对 `phpstan/phpstan` 的附加规则
55
66
[![tests](https://github.com/guanguans/phpstan-rules/actions/workflows/tests.yml/badge.svg)](https://github.com/guanguans/phpstan-rules/actions/workflows/tests.yml)
77
[![php-cs-fixer](https://github.com/guanguans/phpstan-rules/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/guanguans/phpstan-rules/actions/workflows/php-cs-fixer.yml)

baselines/loader.neon

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

33
includes:
44
- shipmonk.checkedExceptionInCallable.neon
5+
- symplify.forbiddenFuncCall.neon
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
# total 1 error
1+
# total 3 errors
22

33
parameters:
44
ignoreErrors:
5+
-
6+
message: '#^Throwing checked exception PHPStan\\ShouldNotHappenException in arrow function\!$#'
7+
count: 1
8+
path: ../src/Rule/ForbiddenSideEffectsFunctionLikeRule.php
9+
510
-
611
message: '#^Throwing checked exception ErrorException in closure\!$#'
712
count: 1
813
path: ../src/Support/helpers.php
14+
15+
-
16+
message: '#^Throwing checked exception ReflectionException in closure\!$#'
17+
count: 1
18+
path: ../src/Support/helpers.php
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# total 2 errors
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Function "property_exists\(\)" cannot be used/left in the code$#'
7+
count: 1
8+
path: ../src/Rule/ForbiddenSideEffectsFunctionLikeRule.php
9+
10+
-
11+
message: '#^Function "property_exists\(\)" cannot be used/left in the code\: use ReflectionProvider\-\>has\*\(\) instead$#'
12+
count: 1
13+
path: ../src/Rule/ForbiddenSideEffectsFunctionLikeRule.php

composer-dependency-analyser.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@
3737
)
3838
->ignoreErrorsOnPackages(
3939
[
40-
'illuminate/support',
40+
// 'illuminate/support',
4141
],
4242
[ErrorType::UNUSED_DEPENDENCY]
4343
)
44-
->ignoreErrorsOnPackages(
45-
[
46-
'staabm/side-effects-detector',
47-
],
44+
->ignoreErrorsOnPackageAndPath(
45+
'staabm/side-effects-detector',
46+
__DIR__.'/src/Rule/ForbiddenSideEffectsFunctionLikeRule.php',
4847
[ErrorType::DEV_DEPENDENCY_IN_PROD]
4948
)
5049
->ignoreErrorsOnPackages(

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "guanguans/phpstan-rules",
3-
"description": "A set of phpstan/phpstan rules. - 一套 phpstan/phpstan 规则",
3+
"description": "A set of additional rules for phpstan/phpstan. - 一套针对 `phpstan/phpstan` 的附加规则",
44
"license": "MIT",
55
"type": "library",
66
"keywords": [
@@ -87,6 +87,9 @@
8787
"tomasvotruba/unused-public": "^2.2",
8888
"yamadashy/phpstan-friendly-formatter": "^1.2"
8989
},
90+
"suggest": {
91+
"staabm/side-effects-detector": "Required to use the rule [ForbiddenSideEffectsRule]."
92+
},
9093
"minimum-stability": "dev",
9194
"prefer-stable": true,
9295
"autoload": {
@@ -173,6 +176,7 @@
173176
"@jsonlint",
174177
"@phpmnd",
175178
"@phpstan:analyse",
179+
"@phpstan-rules:fix-neon-files",
176180
"@phpstan-rules:list-files",
177181
"@rector:process-dry-run",
178182
"@sk:check-commented-code",
@@ -199,7 +203,7 @@
199203
"@pest"
200204
],
201205
"class-leak": "@php vendor/bin/class-leak --ansi -vv",
202-
"class-leak:check": "@class-leak check src/ --skip-type=Guanguans\\PHPStanRules\\Contract\\ThrowableContract --skip-type=Guanguans\\PHPStanRules\\Rule\\ExceptionRule --skip-path=Support/",
206+
"class-leak:check": "@class-leak check src/ --skip-path=Support/",
203207
"composer-bump": [
204208
"@putenv:php",
205209
"@composer-config:disable-process-timeout",
@@ -338,6 +342,7 @@
338342
"ls -lh builds/phpstan-rules.phar",
339343
"ls -lr builds/phpstan-rules.phar"
340344
],
345+
"phpstan-rules:fix-neon-files": "Guanguans\\PHPStanRules\\Support\\ComposerScripts::fixNeonFiles",
341346
"phpstan-rules:list-files": "Guanguans\\PHPStanRules\\Support\\ComposerScripts::listFiles",
342347
"phpstan:analyse": "@phpstan analyse",
343348
"phpstan:analyse-error-format-annotate-pull-request": "@php vendor/bin/phpstan --ansi -vv 'analyse' --error-format=checkstyle | vendor/bin/cs2pr --notices-as-warnings --colorize",

config/extension.neon

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# https://github.com/ergebnis/phpstan-rules/blob/main/rules.neon
2+
# https://github.com/symplify/phpstan-rules/tree/main/config/
3+
4+
conditionalTags:
5+
Guanguans\PHPStanRules\Rule\ExceptionMustImplementNativeThrowableRule:
6+
phpstan.rules.rule: %guanguans.exceptionMustImplementNativeThrowable.enabled%
7+
Guanguans\PHPStanRules\Rule\ForbiddenSideEffectsFunctionLikeRule:
8+
phpstan.rules.rule: %guanguans.forbiddenSideEffectsFunctionLike.enabled%
9+
10+
parameters:
11+
guanguans:
12+
allRules: true
13+
exceptionMustImplementNativeThrowable:
14+
nativeThrowable: \Throwable
15+
enabled: %guanguans.allRules%
16+
forbiddenSideEffectsFunctionLike:
17+
enabled: false
18+
19+
parametersSchema:
20+
guanguans: structure([
21+
allRules: bool()
22+
exceptionMustImplementNativeThrowable: structure([
23+
nativeThrowable: string()
24+
enabled: bool(),
25+
])
26+
forbiddenSideEffectsFunctionLike: structure([
27+
enabled: bool(),
28+
])
29+
])
30+
31+
services:
32+
-
33+
class: staabm\SideEffectsDetector\SideEffectsDetector
34+
35+
-
36+
class: Guanguans\PHPStanRules\Rule\ExceptionMustImplementNativeThrowableRule
37+
arguments:
38+
nativeThrowable: %guanguans.exceptionMustImplementNativeThrowable.nativeThrowable%
39+
-
40+
class: Guanguans\PHPStanRules\Rule\ForbiddenSideEffectsFunctionLikeRule

config/rules.neon

Lines changed: 0 additions & 40 deletions
This file was deleted.

phpstan.neon.dist

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
includes:
22
- baselines/loader.neon
33
# - phpstan-baseline.neon
4-
- config/rules.neon
4+
- config/extension.neon
55

66
- vendor/spaze/phpstan-disallowed-calls/disallowed-dangerous-calls.neon
77
- vendor/spaze/phpstan-disallowed-calls/disallowed-execution-calls.neon
@@ -61,6 +61,8 @@ parameters:
6161
enabled: false
6262
noConstructorParameterWithDefaultValue:
6363
enabled: false
64+
noExtends:
65+
enabled: false
6466
guanguans:
6567
exceptionMustImplementNativeThrowable:
6668
nativeThrowable: Guanguans\PHPStanRules\Contract\ThrowableContract
@@ -134,6 +136,7 @@ parameters:
134136
# - identifier: trait.unused
135137
# - identifier: typePerfect.noMixedMethodCaller
136138

139+
- identifier: symplify.forbiddenStaticClassConstFetch
137140
- identifier: argument.type
138141
# - identifier: cast.int
139142
# - identifier: cast.string
@@ -142,7 +145,7 @@ parameters:
142145
- identifier: return.type
143146
- identifier: phpstanApi.runtimeReflection
144147
- identifier: phpstanApi.method
145-
- identifier: symplify.explicitInterfaceSuffixName
148+
# - identifier: symplify.explicitInterfaceSuffixName
146149
# - identifier: symplify.forbiddenArrayMethodCall
147150
- identifier: symplify.forbiddenNode
148151
# - identifier: symplify.forbiddenStaticClassConstFetch

rector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
use Ergebnis\Rector\Rules\Arrays\SortAssociativeArrayByKeyRector;
20-
use Guanguans\PHPStanRules\Contract\ThrowableContract;
2120
use Guanguans\RectorRules\Rector\Array_\SortListItemOfSameScalarTypeRector;
2221
use Guanguans\RectorRules\Rector\File\AddNoinspectionDocblockToFileFirstStmtRector;
2322
use Guanguans\RectorRules\Rector\Name\RenameToPsrNameRector;
@@ -42,6 +41,7 @@
4241
use Rector\DowngradePhp81\Rector\FuncCall\DowngradeArrayIsListRector;
4342
use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector;
4443
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
44+
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
4545
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
4646
use Rector\PHPUnit\Set\PHPUnitSetList;
4747
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
@@ -137,7 +137,7 @@
137137
'StaticClosureCanBeUsedInspection',
138138
],
139139
])
140-
->withConfiguredRule(NewExceptionToNewAnonymousExtendsExceptionImplementsRector::class, [ThrowableContract::class])
140+
// ->withConfiguredRule(NewExceptionToNewAnonymousExtendsExceptionImplementsRector::class, [ThrowableContract::class])
141141
->registerDecoratingNodeVisitor(ParentConnectingVisitor::class)
142142
->withConfiguredRule(RenameToPsrNameRector::class, [
143143
'MIT',
@@ -200,9 +200,8 @@ classes(static fn (string $class, string $file): bool => str_starts_with($class,
200200
WrapEncapsedVariableInCurlyBracesRector::class,
201201
])
202202
->withSkip([
203-
NewExceptionToNewAnonymousExtendsExceptionImplementsRector::class => [
204-
__DIR__.'/src/Support/helpers.php',
205-
__DIR__.'/tests/Support/HelpersTest.php',
203+
RenameParamToMatchTypeRector::class => [
204+
__DIR__.'/src/Rule/*Rule.php',
206205
],
207206
SortAssociativeArrayByKeyRector::class => [
208207
__DIR__.'/src/',

0 commit comments

Comments
 (0)