Skip to content

Commit 6a20b41

Browse files
committed
refactor(rule): Rename and restructure ForbiddenSideEffectsRule
- Change namespace from `Guanguans\PHPStanRules\Rule` to `Guanguans\PHPStanRules\Rule\File` - Rename `ForbiddenSideEffectsFunctionLikeRule` to `ForbiddenSideEffectsRule` - Update node type from `FunctionLike` to `FileNode` - Adjust parameter type hint in `processNode` method
1 parent 4b0c168 commit 6a20b41

File tree

8 files changed

+73
-54
lines changed

8 files changed

+73
-54
lines changed

.php-cs-fixer-custom.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\SqruffFixer;
2929
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\TextlintFixer;
3030
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\TombiFixer;
31+
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\TyposFixer;
3132
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\XmllintFixer;
3233
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\YamlfmtFixer;
3334
use Guanguans\PhpCsFixerCustomFixers\Fixer\CommandLineTool\ZhlintFixer;
@@ -39,12 +40,6 @@
3940
use PhpCsFixer\Finder;
4041
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
4142

42-
// putenv('PHP_CS_FIXER_ENFORCE_CACHE=1');
43-
// putenv('PHP_CS_FIXER_IGNORE_ENV=1');
44-
putenv('PHP_CS_FIXER_FUTURE_MODE=1');
45-
putenv('PHP_CS_FIXER_NON_MONOLITHIC=1');
46-
putenv('PHP_CS_FIXER_PARALLEL=1');
47-
4843
return (new Config)
4944
->registerCustomFixers($fixers = Fixers::make())
5045
->setRules([
@@ -89,6 +84,7 @@
8984
JsonFixer::name() => true,
9085
ShfmtFixer::name() => true,
9186
TombiFixer::name() => true,
87+
TyposFixer::name() => true,
9288
XmllintFixer::name() => true,
9389
YamlfmtFixer::name() => true,
9490
])
@@ -113,8 +109,10 @@
113109
'/\.lock$/',
114110
'/\-lock\.json$/',
115111
// '/\.php$/',
112+
'/\.php\.inc$/',
116113
'/(?<!\.blade)\.php$/',
117-
'/zhlint\-.*\.zh_CN\.md$/',
114+
// Exclude temporary files created by `zhlint` in the current working directory.
115+
'/zhlint\-.*\..*$/',
118116
])
119117
->ignoreDotFiles(false)
120118
->ignoreUnreadableDirs(false)

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"ergebnis/php-cs-fixer-config": "^6.58",
5555
"ergebnis/rector-rules": "^1.9",
5656
"fakerphp/faker": "^1.24",
57-
"guanguans/php-cs-fixer-custom-fixers": "^1.0",
57+
"guanguans/php-cs-fixer-custom-fixers": "^1.1",
5858
"guanguans/phpstan-rules": "^1.0",
5959
"mockery/mockery": "^1.6",
6060
"nette/utils": "^3.2 || ^4.0",
@@ -193,7 +193,6 @@
193193
"@lint-md",
194194
"@peck",
195195
"@php-cs-fixer:custom-fix-dry-run",
196-
"@php-cs-fixer:custom-ln-config",
197196
"@rule-doc-generator:generate",
198197
"@typos",
199198
"@zhlint"

docs/rules-overview.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,22 @@ Update p h p stan method node param docblock from node types
114114

115115
```diff
116116
/** @noinspection ALL */
117-
namespace Guanguans\PHPStanRules\Rule;
117+
namespace Guanguans\PHPStanRules\Rule\File;
118118

119+
use Guanguans\PHPStanRules\Rule\AbstractRule;
119120
use PhpParser\Node;
120121
use PHPStan\Analyser\Scope;
121-
use PHPStan\Node\FunctionLike;
122-
use PHPStan\Rules\Rule;
122+
use PHPStan\Node\FileNode;
123123

124-
final class ForbiddenSideEffectsFunctionLikeRule extends Rule
124+
final class ForbiddenSideEffectsRule extends AbstractRule
125125
{
126126
public function getNodeType(): string
127127
{
128-
return FunctionLike::class;
128+
return FileNode::class;
129129
}
130130

131131
+ /**
132-
+ * @param \PhpParser\Node\FunctionLike $node
132+
+ * @param \PHPStan\Node\FileNode $node
133133
+ */
134134
public function processNode(Node $node, Scope $scope): array
135135
{

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ parameters:
5353
allRules: true
5454
booleansInConditions: false
5555
disallowedShortTernary: false
56+
guanguans:
57+
exceptionMustImplementNativeThrowable:
58+
nativeThrowable: Guanguans\RectorRules\Contract\ThrowableContract
59+
forbiddenSideEffects:
60+
enabled: false
5661
cognitive_complexity:
5762
class: 42
5863
function: 8

src/Rector/Class_/UpdatePHPStanMethodNodeParamDocblockFromNodeTypesRector.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ protected function codeSamples(): array
3535
new CodeSample(
3636
<<<'PHP_WRAP'
3737
/** @noinspection ALL */
38-
namespace Guanguans\PHPStanRules\Rule;
38+
namespace Guanguans\PHPStanRules\Rule\File;
3939
40+
use Guanguans\PHPStanRules\Rule\AbstractRule;
4041
use PhpParser\Node;
4142
use PHPStan\Analyser\Scope;
42-
use PHPStan\Node\FunctionLike;
43-
use PHPStan\Rules\Rule;
43+
use PHPStan\Node\FileNode;
4444
45-
final class ForbiddenSideEffectsFunctionLikeRule extends Rule
45+
final class ForbiddenSideEffectsRule extends AbstractRule
4646
{
4747
public function getNodeType(): string
4848
{
49-
return FunctionLike::class;
49+
return FileNode::class;
5050
}
5151
5252
public function processNode(Node $node, Scope $scope): array
@@ -57,22 +57,22 @@ public function processNode(Node $node, Scope $scope): array
5757
PHP_WRAP,
5858
<<<'PHP_WRAP'
5959
/** @noinspection ALL */
60-
namespace Guanguans\PHPStanRules\Rule;
60+
namespace Guanguans\PHPStanRules\Rule\File;
6161
62+
use Guanguans\PHPStanRules\Rule\AbstractRule;
6263
use PhpParser\Node;
6364
use PHPStan\Analyser\Scope;
64-
use PHPStan\Node\FunctionLike;
65-
use PHPStan\Rules\Rule;
65+
use PHPStan\Node\FileNode;
6666
67-
final class ForbiddenSideEffectsFunctionLikeRule extends Rule
67+
final class ForbiddenSideEffectsRule extends AbstractRule
6868
{
6969
public function getNodeType(): string
7070
{
71-
return FunctionLike::class;
71+
return FileNode::class;
7272
}
7373
7474
/**
75-
* @param \PhpParser\Node\FunctionLike $node
75+
* @param \PHPStan\Node\FileNode $node
7676
*/
7777
public function processNode(Node $node, Scope $scope): array
7878
{
@@ -111,6 +111,13 @@ protected function nodeTypes(\ReflectionClass $reflectionClass): array
111111
{
112112
$rule = $reflectionClass->newInstanceWithoutConstructor();
113113

114-
return method_exists($rule, 'getNodeTypes') ? $rule->getNodeTypes() : (array) $rule->getNodeType();
114+
if (method_exists($rule, $methodName = 'getNodeTypes')) {
115+
$reflectionMethod = $reflectionClass->getMethod($methodName);
116+
\PHP_VERSION_ID < 80100 and $reflectionMethod->setAccessible(true);
117+
118+
return $reflectionMethod->invoke($rule);
119+
}
120+
121+
return (array) $rule->getNodeType();
115122
}
116123
}

tests/Rector/Class_/UpdatePHPStanMethodNodeParamDocblockFromNodeTypesRector/Fixture/fixture.php.inc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22

33
/** @noinspection ALL */
4-
namespace Guanguans\PHPStanRules\Rule;
4+
namespace Guanguans\PHPStanRules\Rule\File;
55

6+
use Guanguans\PHPStanRules\Rule\AbstractRule;
67
use PhpParser\Node;
78
use PHPStan\Analyser\Scope;
8-
use PHPStan\Node\FunctionLike;
9-
use PHPStan\Rules\Rule;
9+
use PHPStan\Node\FileNode;
1010

11-
final class ForbiddenSideEffectsFunctionLikeRule extends Rule
11+
final class ForbiddenSideEffectsRule extends AbstractRule
1212
{
1313
public function getNodeType(): string
1414
{
15-
return FunctionLike::class;
15+
return FileNode::class;
1616
}
1717

1818
public function processNode(Node $node, Scope $scope): array
@@ -26,22 +26,22 @@ final class ForbiddenSideEffectsFunctionLikeRule extends Rule
2626
<?php
2727

2828
/** @noinspection ALL */
29-
namespace Guanguans\PHPStanRules\Rule;
29+
namespace Guanguans\PHPStanRules\Rule\File;
3030

31+
use Guanguans\PHPStanRules\Rule\AbstractRule;
3132
use PhpParser\Node;
3233
use PHPStan\Analyser\Scope;
33-
use PHPStan\Node\FunctionLike;
34-
use PHPStan\Rules\Rule;
34+
use PHPStan\Node\FileNode;
3535

36-
final class ForbiddenSideEffectsFunctionLikeRule extends Rule
36+
final class ForbiddenSideEffectsRule extends AbstractRule
3737
{
3838
public function getNodeType(): string
3939
{
40-
return FunctionLike::class;
40+
return FileNode::class;
4141
}
4242

4343
/**
44-
* @param \PhpParser\Node\FunctionLike $node
44+
* @param \PHPStan\Node\FileNode $node
4545
*/
4646
public function processNode(Node $node, Scope $scope): array
4747
{

tests/Rector/Class_/UpdatePHPStanMethodNodeParamDocblockFromNodeTypesRector/Fixture/fixture_union_type.php.inc

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<?php
22

33
/** @noinspection ALL */
4-
namespace Guanguans\PHPStanRules\Rule;
4+
namespace Guanguans\PHPStanRules\Rule\Class_;
55

6+
use Guanguans\PHPStanRules\Rule\AbstractMixedNodeTypeRule;
67
use PhpParser\Node;
8+
use PhpParser\Node\Expr\New_;
9+
use PhpParser\Node\Stmt\Class_;
710
use PHPStan\Analyser\Scope;
8-
use PHPStan\Node\FunctionLike;
9-
use PHPStan\Rules\Rule;
1011

11-
final class ForbiddenSideEffectsFunctionLikeRule extends Rule
12+
13+
final class ExceptionMustImplementNativeThrowableRule extends AbstractMixedNodeTypeRule
1214
{
13-
public function getNodeType(): string
15+
protected function getNodeTypes(): array
1416
{
15-
return FunctionLike::class;
17+
return [
18+
Class_::class,
19+
New_::class,
20+
];
1621
}
1722

18-
public function processNode(Node $node, Scope $scope): array
23+
protected function rawProcessNode(Node $node, Scope $scope): array
1924
{
2025
return [];
2126
}
@@ -26,24 +31,29 @@ final class ForbiddenSideEffectsFunctionLikeRule extends Rule
2631
<?php
2732

2833
/** @noinspection ALL */
29-
namespace Guanguans\PHPStanRules\Rule;
34+
namespace Guanguans\PHPStanRules\Rule\Class_;
3035

36+
use Guanguans\PHPStanRules\Rule\AbstractMixedNodeTypeRule;
3137
use PhpParser\Node;
38+
use PhpParser\Node\Expr\New_;
39+
use PhpParser\Node\Stmt\Class_;
3240
use PHPStan\Analyser\Scope;
33-
use PHPStan\Node\FunctionLike;
34-
use PHPStan\Rules\Rule;
3541

36-
final class ForbiddenSideEffectsFunctionLikeRule extends Rule
42+
43+
final class ExceptionMustImplementNativeThrowableRule extends AbstractMixedNodeTypeRule
3744
{
38-
public function getNodeType(): string
45+
protected function getNodeTypes(): array
3946
{
40-
return FunctionLike::class;
47+
return [
48+
Class_::class,
49+
New_::class,
50+
];
4151
}
4252

4353
/**
44-
* @param \PhpParser\Node\FunctionLike $node
54+
* @param \PhpParser\Node\Expr\New_|\PhpParser\Node\Stmt\Class_ $node
4555
*/
46-
public function processNode(Node $node, Scope $scope): array
56+
protected function rawProcessNode(Node $node, Scope $scope): array
4757
{
4858
return [];
4959
}

vendor-bin/php82/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"php": ">=8.2",
44
"ergebnis/composer-normalize": "^2.48",
55
"guanguans/monorepo-builder-worker": "^3.0",
6-
"illuminate/support": "^12.46",
6+
"illuminate/support": "^12.47",
77
"mrpunyapal/rector-pest": "^0.1",
88
"peckphp/peck": "^0.2",
99
"phpro/grumphp-shim": "^2.18"

0 commit comments

Comments
 (0)