Skip to content

Commit

Permalink
Modernized rules with generics and RuleErrorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 8, 2023
1 parent cca41e8 commit bd3aa8b
Show file tree
Hide file tree
Showing 63 changed files with 310 additions and 667 deletions.
478 changes: 0 additions & 478 deletions phpstan-baseline.neon

This file was deleted.

1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ includes:
- vendor/phpstan/phpstan-phpunit/rules.neon
- rules.neon
- phar://phpstan.phar/conf/bleedingEdge.neon
- phpstan-baseline.neon

parameters:
excludePaths:
Expand Down
9 changes: 5 additions & 4 deletions src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\BooleanAndNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

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

$rightScope = $node->getRightScope();
if (!$this->helper->passesAsBoolean($rightScope, $originalNode->right)) {
$rightType = $rightScope->getType($originalNode->right);
$messages[] = sprintf(
$messages[] = RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in &&, %s given on the right side.',
$rightType->describe(VerbosityLevel::typeOnly())
);
))->build();
}

return $messages;
Expand Down
12 changes: 6 additions & 6 deletions src/Rules/BooleansInConditions/BooleanInBooleanNotRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use PhpParser\Node\Expr\BooleanNot;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements Rule<BooleanNot>
*/
class BooleanInBooleanNotRule implements Rule
{

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

/**
* @param BooleanNot $node
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->helper->passesAsBoolean($scope, $node->expr)) {
Expand All @@ -38,10 +38,10 @@ public function processNode(Node $node, Scope $scope): array
$expressionType = $scope->getType($node->expr);

return [
sprintf(
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in a negated boolean, %s given.',
$expressionType->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}

Expand Down
9 changes: 5 additions & 4 deletions src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\BooleanOrNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

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

$rightScope = $node->getRightScope();
if (!$this->helper->passesAsBoolean($rightScope, $originalNode->right)) {
$rightType = $rightScope->getType($originalNode->right);
$messages[] = sprintf(
$messages[] = RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in ||, %s given on the right side.',
$rightType->describe(VerbosityLevel::typeOnly())
);
))->build();
}

return $messages;
Expand Down
12 changes: 6 additions & 6 deletions src/Rules/BooleansInConditions/BooleanInElseIfConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use PhpParser\Node\Stmt\ElseIf_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements Rule<ElseIf_>
*/
class BooleanInElseIfConditionRule implements Rule
{

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

/**
* @param ElseIf_ $node
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->helper->passesAsBoolean($scope, $node->cond)) {
Expand All @@ -38,10 +38,10 @@ public function processNode(Node $node, Scope $scope): array
$conditionExpressionType = $scope->getType($node->cond);

return [
sprintf(
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in an elseif condition, %s given.',
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}

Expand Down
12 changes: 6 additions & 6 deletions src/Rules/BooleansInConditions/BooleanInIfConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements Rule<If_>
*/
class BooleanInIfConditionRule implements Rule
{

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

/**
* @param If_ $node
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->helper->passesAsBoolean($scope, $node->cond)) {
Expand All @@ -38,10 +38,10 @@ public function processNode(Node $node, Scope $scope): array
$conditionExpressionType = $scope->getType($node->cond);

return [
sprintf(
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in an if condition, %s given.',
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}

Expand Down
12 changes: 6 additions & 6 deletions src/Rules/BooleansInConditions/BooleanInTernaryOperatorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use PhpParser\Node\Expr\Ternary;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements Rule<Ternary>
*/
class BooleanInTernaryOperatorRule implements Rule
{

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

/**
* @param Ternary $node
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if ($node->if === null) {
Expand All @@ -42,10 +42,10 @@ public function processNode(Node $node, Scope $scope): array
$conditionExpressionType = $scope->getType($node->cond);

return [
sprintf(
RuleErrorBuilder::message(sprintf(
'Only booleans are allowed in a ternary operator condition, %s given.',
$conditionExpressionType->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}

Expand Down
8 changes: 3 additions & 5 deletions src/Rules/Cast/UselessCastRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
use PhpParser\Node\Expr\Cast;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ErrorType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements Rule<Cast>
*/
class UselessCastRule implements Rule
{

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

/**
* @param Cast $node
* @return RuleError[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$castType = $scope->getType($node);
Expand Down
20 changes: 10 additions & 10 deletions src/Rules/Classes/RequireParentConstructCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function property_exists;
use function sprintf;

/**
* @implements Rule<ClassMethod>
*/
class RequireParentConstructCallRule implements Rule
{

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

/**
* @param ClassMethod $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
Expand All @@ -52,30 +52,30 @@ public function processNode(Node $node, Scope $scope): array
if ($this->callsParentConstruct($node)) {
if ($classReflection->getParentClass() === false) {
return [
sprintf(
RuleErrorBuilder::message(sprintf(
'%s::__construct() calls parent constructor but does not extend any class.',
$classReflection->getName()
),
))->build(),
];
}

if ($this->getParentConstructorClass($classReflection) === false) {
return [
sprintf(
RuleErrorBuilder::message(sprintf(
'%s::__construct() calls parent constructor but parent does not have one.',
$classReflection->getName()
),
))->build(),
];
}
} else {
$parentClass = $this->getParentConstructorClass($classReflection);
if ($parentClass !== false) {
return [
sprintf(
RuleErrorBuilder::message(sprintf(
'%s::__construct() does not call parent constructor from %s.',
$classReflection->getName(),
$parentClass->getName()
),
))->build(),
];
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Rules/DisallowedConstructs/DisallowedBacktickRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace PHPStan\Rules\DisallowedConstructs;

use PhpParser\Node;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\ShellExec;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<ShellExec>
*/
class DisallowedBacktickRule implements Rule
{

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

/**
* @param Empty_ $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [
'Backtick operator is not allowed. Use shell_exec() instead.',
RuleErrorBuilder::message('Backtick operator is not allowed. Use shell_exec() instead.')
->build(),
];
}

Expand Down
11 changes: 6 additions & 5 deletions src/Rules/DisallowedConstructs/DisallowedEmptyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
use PhpParser\Node\Expr\Empty_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Empty_>
*/
class DisallowedEmptyRule implements Rule
{

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

/**
* @param Empty_ $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [
'Construct empty() is not allowed. Use more strict comparison.',
RuleErrorBuilder::message('Construct empty() is not allowed. Use more strict comparison.')
->build(),
];
}

Expand Down
Loading

0 comments on commit bd3aa8b

Please sign in to comment.