Skip to content

Commit 5edccce

Browse files
committed
remove abstract symplify rule
1 parent cd84fff commit 5edccce

File tree

8 files changed

+93
-148
lines changed

8 files changed

+93
-148
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ includes:
2828
- vendor/symplify/phpstan-rules/config/naming-rules.neon
2929
- vendor/symplify/phpstan-rules/config/regex-rules.neon
3030
- vendor/symplify/phpstan-rules/config/static-rules.neon
31+
32+
# project specific
3133
- vendor/symplify/phpstan-rules/config/rector-rules.neon
34+
- vendor/symplify/phpstan-rules/config/doctrine-rules.neon
35+
- vendor/symplify/phpstan-rules/config/symfony-rules.neon
3236
```
3337
3438
<br>

phpstan.neon

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ parameters:
2323
- */Fixture/*
2424

2525
ignoreErrors:
26-
# needless generics
27-
# - '#Class Symplify\\PHPStanRules\\(.*?)Rule implements generic interface PHPStan\\Rules\\Rule but does not specify its types\: TNodeType#'
28-
2926
- '#Method Symplify\\PHPStanRules\\Reflection\\ReflectionParser\:\:parseNativeClassReflection\(\) has parameter \$reflectionClass with generic class ReflectionClass but does not specify its types\: T#'
3027

3128
# overly detailed
@@ -47,8 +44,7 @@ parameters:
4744
message: '#Function is_a\(\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine#'
4845
path: src/Rules/SeeAnnotationToTestRule.php
4946

50-
# handle next
51-
- '#Method Symplify\\PHPStanRules\\Rules\\AbstractSymplifyRule\:\:processNode\(\) should return list<PHPStan\\Rules\\IdentifierRuleError> but returns array<PHPStan\\Rules\\RuleError\|string>#'
52-
5347
# used in tests
5448
- '#Public constant "Symplify\\PHPStanRules\\(.*?)Rule\:\:ERROR_MESSAGE" is never used#'
49+
50+
- '#Although PHPStan\\Node\\InClassNode is covered by backward compatibility promise, this instanceof assumption might break because (.*?) not guaranteed to always stay the same#'

src/Contract/ManyNodeRuleInterface.php

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

src/Enum/RuleIdentifier.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,11 @@ final class RuleIdentifier
170170
* @var string
171171
*/
172172
public const PHPUNIT_NO_DOCUMENT_MOCKING = 'phpunit.noDocumentMocking';
173+
174+
/**
175+
* @var string
176+
*/
177+
public const NO_DYNAMIC_NAME = 'symplify.noDynamicName';
178+
179+
public const NO_REFERENCE = 'symplify.noReference';
173180
}

src/Rules/AbstractSymplifyRule.php

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

src/Rules/NoDynamicNameRule.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
use PhpParser\Node\Expr\StaticPropertyFetch;
1515
use PhpParser\Node\Identifier;
1616
use PHPStan\Analyser\Scope;
17+
use PHPStan\Rules\Rule;
1718
use PHPStan\Rules\RuleErrorBuilder;
19+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1820
use Symplify\PHPStanRules\TypeAnalyzer\CallableTypeAnalyzer;
1921

2022
/**
2123
* @see \Symplify\PHPStanRules\Tests\Rules\NoDynamicNameRule\NoDynamicNameRuleTest
24+
*
25+
* @implements Rule<Node>
2226
*/
23-
final class NoDynamicNameRule extends AbstractSymplifyRule
27+
final class NoDynamicNameRule implements Rule
2428
{
2529
/**
2630
* @var string
@@ -32,25 +36,13 @@ public function __construct(
3236
) {
3337
}
3438

35-
/**
36-
* @return array<class-string<Node>>
37-
*/
38-
public function getNodeTypes(): array
39+
public function getNodeType(): string
3940
{
40-
return [
41-
MethodCall::class,
42-
StaticCall::class,
43-
FuncCall::class,
44-
StaticPropertyFetch::class,
45-
PropertyFetch::class,
46-
ClassConstFetch::class,
47-
];
41+
// trick to allow multiple node types
42+
return Node::class;
4843
}
4944

50-
/**
51-
* @param MethodCall|StaticCall|FuncCall|StaticPropertyFetch|PropertyFetch|ClassConstFetch $node
52-
*/
53-
public function process(Node $node, Scope $scope): array
45+
public function processNode(Node $node, Scope $scope): array
5446
{
5547
if ($node instanceof ClassConstFetch || $node instanceof StaticPropertyFetch) {
5648
if (! $node->class instanceof Expr) {
@@ -65,17 +57,30 @@ public function process(Node $node, Scope $scope): array
6557
return [];
6658
}
6759

68-
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];
69-
}
60+
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
61+
->identifier(RuleIdentifier::NO_DYNAMIC_NAME)
62+
->build();
7063

71-
if (! $node->name instanceof Expr) {
72-
return [];
64+
return [$ruleError];
7365
}
7466

75-
if ($this->callableTypeAnalyzer->isClosureOrCallableType($scope, $node->name)) {
76-
return [];
67+
if ($node instanceof MethodCall || $node instanceof StaticCall || $node instanceof FuncCall || $node instanceof PropertyFetch) {
68+
69+
if (! $node->name instanceof Expr) {
70+
return [];
71+
}
72+
73+
if ($this->callableTypeAnalyzer->isClosureOrCallableType($scope, $node->name)) {
74+
return [];
75+
}
76+
77+
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
78+
->identifier(RuleIdentifier::NO_DYNAMIC_NAME)
79+
->build();
80+
81+
return [$ruleError];
7782
}
7883

79-
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];
84+
return [];
8085
}
8186
}

src/Rules/NoReferenceRule.php

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
use PhpParser\Node\Stmt\Foreach_;
1616
use PhpParser\Node\Stmt\Function_;
1717
use PHPStan\Analyser\Scope;
18-
use PHPStan\Rules\RuleError;
18+
use PHPStan\Rules\IdentifierRuleError;
19+
use PHPStan\Rules\Rule;
1920
use PHPStan\Rules\RuleErrorBuilder;
21+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
2022
use Symplify\PHPStanRules\ParentClassMethodNodeResolver;
21-
use function array_map;
2223

2324
/**
2425
* @see \Symplify\PHPStanRules\Tests\Rules\NoReferenceRule\NoReferenceRuleTest
26+
*
27+
* @implements Rule<\PhpParser\Node>
2528
*/
26-
final class NoReferenceRule extends AbstractSymplifyRule
29+
final class NoReferenceRule implements Rule
2730
{
2831
/**
2932
* @var string
@@ -35,69 +38,65 @@ public function __construct(
3538
) {
3639
}
3740

38-
public function getNodeTypes(): array
41+
public function getNodeType(): string
3942
{
40-
return [
41-
ClassMethod::class,
42-
Function_::class,
43-
AssignRef::class,
44-
Arg::class,
45-
Foreach_::class,
46-
ArrayItem::class,
47-
ArrowFunction::class,
48-
Closure::class,
49-
];
43+
return Node::class;
5044
}
5145

52-
/**
53-
* @param ClassMethod|Function_|AssignRef|Arg|Foreach_|ArrayItem|ArrowFunction|Closure $node
54-
*/
55-
public function process(Node $node, Scope $scope): array
46+
public function processNode(Node $node, Scope $scope): array
5647
{
5748
$errorMessages = [];
5849

5950
if ($node instanceof AssignRef) {
60-
$errorMessages[] = self::ERROR_MESSAGE;
61-
} elseif ($node->byRef) {
62-
$errorMessages[] = self::ERROR_MESSAGE;
51+
return [$this->createRuleError()];
52+
}
53+
54+
if (! $node instanceof Closure && ! $node instanceof ArrowFunction && ! $node instanceof Function_ && ! $node instanceof ClassMethod && ! $node instanceof Arg && ! $node instanceof Foreach_ && ! $node instanceof ArrayItem) {
55+
return [];
56+
}
57+
58+
if ($node->byRef) {
59+
$errorMessages[] = $this->createRuleError();
6360
}
6461

65-
$paramErrorMessage = $this->collectParamErrorMessages($node, $scope);
66-
$errorMessages = array_merge($errorMessages, $paramErrorMessage);
62+
if ($node instanceof Function_ || $node instanceof ClassMethod) {
63+
$paramErrorMessage = $this->collectParamErrorMessages($node, $scope);
64+
$errorMessages = array_merge($errorMessages, $paramErrorMessage);
65+
}
6766

68-
return array_map(
69-
static fn ($message): RuleError => RuleErrorBuilder::message($message)->build(),
70-
array_unique($errorMessages),
71-
);
67+
return $errorMessages;
7268
}
7369

7470
/**
75-
* @return string[]
71+
* @return list<IdentifierRuleError>
7672
*/
77-
private function collectParamErrorMessages(Node $node, Scope $scope): array
73+
private function collectParamErrorMessages(Function_|ClassMethod $functionLike, Scope $scope): array
7874
{
79-
if (! $node instanceof Function_ && ! $node instanceof ClassMethod) {
80-
return [];
81-
}
82-
8375
// has parent method? → skip it as enforced by parent
84-
$methodName = (string) $node->name;
76+
$methodName = (string) $functionLike->name;
8577

8678
$parentClassMethod = $this->parentClassMethodNodeResolver->resolveParentClassMethod($scope, $methodName);
8779
if ($parentClassMethod instanceof ClassMethod) {
8880
return [];
8981
}
9082

9183
$errorMessages = [];
92-
foreach ($node->params as $param) {
84+
foreach ($functionLike->params as $param) {
9385
/** @var Param $param */
9486
if (! $param->byRef) {
9587
continue;
9688
}
9789

98-
$errorMessages[] = self::ERROR_MESSAGE;
90+
$errorMessages[] = $this->createRuleError();
9991
}
10092

10193
return $errorMessages;
10294
}
95+
96+
private function createRuleError(): IdentifierRuleError
97+
{
98+
return RuleErrorBuilder::message(self::ERROR_MESSAGE)
99+
->identifier(RuleIdentifier::NO_REFERENCE)
100+
->build();
101+
}
103102
}

src/Rules/PreferredClassRule.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
use PHPStan\Node\InClassNode;
1616
use PHPStan\Reflection\ClassReflection;
1717
use PHPStan\Rules\IdentifierRuleError;
18+
use PHPStan\Rules\Rule;
1819
use PHPStan\Rules\RuleErrorBuilder;
1920
use Symplify\PHPStanRules\Enum\RuleIdentifier;
2021

2122
/**
2223
* @see \Symplify\PHPStanRules\Tests\Rules\PreferredClassRule\PreferredClassRuleTest
24+
*
25+
* @implements Rule<Node>
2326
*/
24-
final class PreferredClassRule extends AbstractSymplifyRule
27+
final class PreferredClassRule implements Rule
2528
{
2629
/**
2730
* @var string
@@ -36,15 +39,7 @@ public function __construct(
3639
) {
3740
}
3841

39-
public function getNodeTypes(): array
40-
{
41-
return [New_::class, Name::class, InClassNode::class, StaticCall::class, Instanceof_::class];
42-
}
43-
44-
/**
45-
* @param New_|Name|InClassNode|StaticCall|Instanceof_ $node
46-
*/
47-
public function process(Node $node, Scope $scope): array
42+
public function processNode(Node $node, Scope $scope): array
4843
{
4944
if ($node instanceof New_) {
5045
return $this->processNew($node);
@@ -58,11 +53,20 @@ public function process(Node $node, Scope $scope): array
5853
return $this->processExprWithClass($node);
5954
}
6055

61-
return $this->processClassName($node->toString());
56+
if ($node instanceof Name) {
57+
return $this->processClassName($node->toString());
58+
}
59+
60+
return [];
61+
}
62+
63+
public function getNodeType(): string
64+
{
65+
return Node::class;
6266
}
6367

6468
/**
65-
* @return IdentifierRuleError[]
69+
* @return list<IdentifierRuleError>
6670
*/
6771
private function processNew(New_ $new): array
6872
{

0 commit comments

Comments
 (0)