Skip to content

Commit bd3aa8b

Browse files
committed
Modernized rules with generics and RuleErrorBuilder
1 parent cca41e8 commit bd3aa8b

File tree

63 files changed

+310
-667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+310
-667
lines changed

phpstan-baseline.neon

-478
This file was deleted.

phpstan.neon

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ includes:
44
- vendor/phpstan/phpstan-phpunit/rules.neon
55
- rules.neon
66
- phar://phpstan.phar/conf/bleedingEdge.neon
7-
- phpstan-baseline.neon
87

98
parameters:
109
excludePaths:

src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\BooleanAndNode;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\VerbosityLevel;
1011
use function sprintf;
1112

@@ -34,19 +35,19 @@ public function processNode(Node $node, Scope $scope): array
3435
$messages = [];
3536
if (!$this->helper->passesAsBoolean($scope, $originalNode->left)) {
3637
$leftType = $scope->getType($originalNode->left);
37-
$messages[] = sprintf(
38+
$messages[] = RuleErrorBuilder::message(sprintf(
3839
'Only booleans are allowed in &&, %s given on the left side.',
3940
$leftType->describe(VerbosityLevel::typeOnly())
40-
);
41+
))->build();
4142
}
4243

4344
$rightScope = $node->getRightScope();
4445
if (!$this->helper->passesAsBoolean($rightScope, $originalNode->right)) {
4546
$rightType = $rightScope->getType($originalNode->right);
46-
$messages[] = sprintf(
47+
$messages[] = RuleErrorBuilder::message(sprintf(
4748
'Only booleans are allowed in &&, %s given on the right side.',
4849
$rightType->describe(VerbosityLevel::typeOnly())
49-
);
50+
))->build();
5051
}
5152

5253
return $messages;

src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use PhpParser\Node\Expr\BooleanNot;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\VerbosityLevel;
1011
use function sprintf;
1112

13+
/**
14+
* @implements Rule<BooleanNot>
15+
*/
1216
class BooleanInBooleanNotRule implements Rule
1317
{
1418

@@ -25,10 +29,6 @@ public function getNodeType(): string
2529
return BooleanNot::class;
2630
}
2731

28-
/**
29-
* @param BooleanNot $node
30-
* @return string[] errors
31-
*/
3232
public function processNode(Node $node, Scope $scope): array
3333
{
3434
if ($this->helper->passesAsBoolean($scope, $node->expr)) {
@@ -38,10 +38,10 @@ public function processNode(Node $node, Scope $scope): array
3838
$expressionType = $scope->getType($node->expr);
3939

4040
return [
41-
sprintf(
41+
RuleErrorBuilder::message(sprintf(
4242
'Only booleans are allowed in a negated boolean, %s given.',
4343
$expressionType->describe(VerbosityLevel::typeOnly())
44-
),
44+
))->build(),
4545
];
4646
}
4747

src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\BooleanOrNode;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\VerbosityLevel;
1011
use function sprintf;
1112

@@ -34,19 +35,19 @@ public function processNode(Node $node, Scope $scope): array
3435
$messages = [];
3536
if (!$this->helper->passesAsBoolean($scope, $originalNode->left)) {
3637
$leftType = $scope->getType($originalNode->left);
37-
$messages[] = sprintf(
38+
$messages[] = RuleErrorBuilder::message(sprintf(
3839
'Only booleans are allowed in ||, %s given on the left side.',
3940
$leftType->describe(VerbosityLevel::typeOnly())
40-
);
41+
))->build();
4142
}
4243

4344
$rightScope = $node->getRightScope();
4445
if (!$this->helper->passesAsBoolean($rightScope, $originalNode->right)) {
4546
$rightType = $rightScope->getType($originalNode->right);
46-
$messages[] = sprintf(
47+
$messages[] = RuleErrorBuilder::message(sprintf(
4748
'Only booleans are allowed in ||, %s given on the right side.',
4849
$rightType->describe(VerbosityLevel::typeOnly())
49-
);
50+
))->build();
5051
}
5152

5253
return $messages;

src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use PhpParser\Node\Stmt\ElseIf_;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\VerbosityLevel;
1011
use function sprintf;
1112

13+
/**
14+
* @implements Rule<ElseIf_>
15+
*/
1216
class BooleanInElseIfConditionRule implements Rule
1317
{
1418

@@ -25,10 +29,6 @@ public function getNodeType(): string
2529
return ElseIf_::class;
2630
}
2731

28-
/**
29-
* @param ElseIf_ $node
30-
* @return string[] errors
31-
*/
3232
public function processNode(Node $node, Scope $scope): array
3333
{
3434
if ($this->helper->passesAsBoolean($scope, $node->cond)) {
@@ -38,10 +38,10 @@ public function processNode(Node $node, Scope $scope): array
3838
$conditionExpressionType = $scope->getType($node->cond);
3939

4040
return [
41-
sprintf(
41+
RuleErrorBuilder::message(sprintf(
4242
'Only booleans are allowed in an elseif condition, %s given.',
4343
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
44-
),
44+
))->build(),
4545
];
4646
}
4747

src/Rules/BooleansInConditions/BooleanInIfConditionRule.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use PhpParser\Node\Stmt\If_;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\VerbosityLevel;
1011
use function sprintf;
1112

13+
/**
14+
* @implements Rule<If_>
15+
*/
1216
class BooleanInIfConditionRule implements Rule
1317
{
1418

@@ -25,10 +29,6 @@ public function getNodeType(): string
2529
return If_::class;
2630
}
2731

28-
/**
29-
* @param If_ $node
30-
* @return string[] errors
31-
*/
3232
public function processNode(Node $node, Scope $scope): array
3333
{
3434
if ($this->helper->passesAsBoolean($scope, $node->cond)) {
@@ -38,10 +38,10 @@ public function processNode(Node $node, Scope $scope): array
3838
$conditionExpressionType = $scope->getType($node->cond);
3939

4040
return [
41-
sprintf(
41+
RuleErrorBuilder::message(sprintf(
4242
'Only booleans are allowed in an if condition, %s given.',
4343
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
44-
),
44+
))->build(),
4545
];
4646
}
4747

src/Rules/BooleansInConditions/BooleanInTernaryOperatorRule.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use PhpParser\Node\Expr\Ternary;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\VerbosityLevel;
1011
use function sprintf;
1112

13+
/**
14+
* @implements Rule<Ternary>
15+
*/
1216
class BooleanInTernaryOperatorRule implements Rule
1317
{
1418

@@ -25,10 +29,6 @@ public function getNodeType(): string
2529
return Ternary::class;
2630
}
2731

28-
/**
29-
* @param Ternary $node
30-
* @return string[] errors
31-
*/
3232
public function processNode(Node $node, Scope $scope): array
3333
{
3434
if ($node->if === null) {
@@ -42,10 +42,10 @@ public function processNode(Node $node, Scope $scope): array
4242
$conditionExpressionType = $scope->getType($node->cond);
4343

4444
return [
45-
sprintf(
45+
RuleErrorBuilder::message(sprintf(
4646
'Only booleans are allowed in a ternary operator condition, %s given.',
4747
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
48-
),
48+
))->build(),
4949
];
5050
}
5151

src/Rules/Cast/UselessCastRule.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
use PhpParser\Node\Expr\Cast;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9-
use PHPStan\Rules\RuleError;
109
use PHPStan\Rules\RuleErrorBuilder;
1110
use PHPStan\Type\ErrorType;
1211
use PHPStan\Type\GeneralizePrecision;
1312
use PHPStan\Type\VerbosityLevel;
1413
use function sprintf;
1514

15+
/**
16+
* @implements Rule<Cast>
17+
*/
1618
class UselessCastRule implements Rule
1719
{
1820

@@ -29,10 +31,6 @@ public function getNodeType(): string
2931
return Cast::class;
3032
}
3133

32-
/**
33-
* @param Cast $node
34-
* @return RuleError[] errors
35-
*/
3634
public function processNode(Node $node, Scope $scope): array
3735
{
3836
$castType = $scope->getType($node);

src/Rules/Classes/RequireParentConstructCallRule.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
1111
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
1212
use PHPStan\Rules\Rule;
13+
use PHPStan\Rules\RuleErrorBuilder;
1314
use PHPStan\ShouldNotHappenException;
1415
use function property_exists;
1516
use function sprintf;
1617

18+
/**
19+
* @implements Rule<ClassMethod>
20+
*/
1721
class RequireParentConstructCallRule implements Rule
1822
{
1923

@@ -22,10 +26,6 @@ public function getNodeType(): string
2226
return ClassMethod::class;
2327
}
2428

25-
/**
26-
* @param ClassMethod $node
27-
* @return string[]
28-
*/
2929
public function processNode(Node $node, Scope $scope): array
3030
{
3131
if (!$scope->isInClass()) {
@@ -52,30 +52,30 @@ public function processNode(Node $node, Scope $scope): array
5252
if ($this->callsParentConstruct($node)) {
5353
if ($classReflection->getParentClass() === false) {
5454
return [
55-
sprintf(
55+
RuleErrorBuilder::message(sprintf(
5656
'%s::__construct() calls parent constructor but does not extend any class.',
5757
$classReflection->getName()
58-
),
58+
))->build(),
5959
];
6060
}
6161

6262
if ($this->getParentConstructorClass($classReflection) === false) {
6363
return [
64-
sprintf(
64+
RuleErrorBuilder::message(sprintf(
6565
'%s::__construct() calls parent constructor but parent does not have one.',
6666
$classReflection->getName()
67-
),
67+
))->build(),
6868
];
6969
}
7070
} else {
7171
$parentClass = $this->getParentConstructorClass($classReflection);
7272
if ($parentClass !== false) {
7373
return [
74-
sprintf(
74+
RuleErrorBuilder::message(sprintf(
7575
'%s::__construct() does not call parent constructor from %s.',
7676
$classReflection->getName(),
7777
$parentClass->getName()
78-
),
78+
))->build(),
7979
];
8080
}
8181
}

src/Rules/DisallowedConstructs/DisallowedBacktickRule.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace PHPStan\Rules\DisallowedConstructs;
44

55
use PhpParser\Node;
6-
use PhpParser\Node\Expr\Empty_;
76
use PhpParser\Node\Expr\ShellExec;
87
use PHPStan\Analyser\Scope;
98
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
1010

11+
/**
12+
* @implements Rule<ShellExec>
13+
*/
1114
class DisallowedBacktickRule implements Rule
1215
{
1316

@@ -16,14 +19,11 @@ public function getNodeType(): string
1619
return ShellExec::class;
1720
}
1821

19-
/**
20-
* @param Empty_ $node
21-
* @return string[]
22-
*/
2322
public function processNode(Node $node, Scope $scope): array
2423
{
2524
return [
26-
'Backtick operator is not allowed. Use shell_exec() instead.',
25+
RuleErrorBuilder::message('Backtick operator is not allowed. Use shell_exec() instead.')
26+
->build(),
2727
];
2828
}
2929

src/Rules/DisallowedConstructs/DisallowedEmptyRule.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
use PhpParser\Node\Expr\Empty_;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
910

11+
/**
12+
* @implements Rule<Empty_>
13+
*/
1014
class DisallowedEmptyRule implements Rule
1115
{
1216

@@ -15,14 +19,11 @@ public function getNodeType(): string
1519
return Empty_::class;
1620
}
1721

18-
/**
19-
* @param Empty_ $node
20-
* @return string[]
21-
*/
2222
public function processNode(Node $node, Scope $scope): array
2323
{
2424
return [
25-
'Construct empty() is not allowed. Use more strict comparison.',
25+
RuleErrorBuilder::message('Construct empty() is not allowed. Use more strict comparison.')
26+
->build(),
2627
];
2728
}
2829

0 commit comments

Comments
 (0)