Skip to content

Commit

Permalink
Modernize rules with RuleErrorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 28, 2023
1 parent 8f1dab2 commit 7e7b5bd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/Rules/Symfony/ContainerInterfacePrivateServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Symfony\ServiceMap;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ObjectType;
Expand All @@ -32,9 +32,6 @@ public function getNodeType(): string
return MethodCall::class;
}

/**
* @return (string|RuleError)[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
Expand Down Expand Up @@ -72,7 +69,10 @@ public function processNode(Node $node, Scope $scope): array
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && !$service->isPublic()) {
return [sprintf('Service "%s" is private.', $serviceId)];
return [
RuleErrorBuilder::message(sprintf('Service "%s" is private.', $serviceId))
->build(),
];
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Rules/Symfony/ContainerInterfaceUnknownServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Symfony\ServiceMap;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Symfony\Helper;
Expand Down Expand Up @@ -36,9 +36,6 @@ public function getNodeType(): string
return MethodCall::class;
}

/**
* @return (string|RuleError)[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
Expand Down Expand Up @@ -73,7 +70,9 @@ public function processNode(Node $node, Scope $scope): array
$service = $this->serviceMap->getService($serviceId);
$serviceIdType = $scope->getType($node->getArgs()[0]->value);
if ($service === null && !$scope->getType(Helper::createMarkerNode($node->var, $serviceIdType, $this->printer))->equals($serviceIdType)) {
return [sprintf('Service "%s" is not registered in the container.', $serviceId)];
return [
RuleErrorBuilder::message(sprintf('Service "%s" is not registered in the container.', $serviceId))->build(),
];
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/Rules/Symfony/InvalidArgumentDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\IntegerType;
Expand All @@ -30,9 +30,6 @@ public function getNodeType(): string
return MethodCall::class;
}

/**
* @return (string|RuleError)[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if (!(new ObjectType('Symfony\Component\Console\Command\Command'))->isSuperTypeOf($scope->getType($node->var))->yes()) {
Expand Down Expand Up @@ -62,12 +59,22 @@ public function processNode(Node $node, Scope $scope): array

// not an array
if (($mode & 4) !== 4 && !(new UnionType([new StringType(), new NullType()]))->isSuperTypeOf($defaultType)->yes()) {
return [sprintf('Parameter #4 $default of method Symfony\Component\Console\Command\Command::addArgument() expects string|null, %s given.', $defaultType->describe(VerbosityLevel::typeOnly()))];
return [
RuleErrorBuilder::message(sprintf(
'Parameter #4 $default of method Symfony\Component\Console\Command\Command::addArgument() expects string|null, %s given.',
$defaultType->describe(VerbosityLevel::typeOnly())
))->build(),
];
}

// is array
if (($mode & 4) === 4 && !(new UnionType([new ArrayType(new IntegerType(), new StringType()), new NullType()]))->isSuperTypeOf($defaultType)->yes()) {
return [sprintf('Parameter #4 $default of method Symfony\Component\Console\Command\Command::addArgument() expects array<int, string>|null, %s given.', $defaultType->describe(VerbosityLevel::typeOnly()))];
return [
RuleErrorBuilder::message(sprintf(
'Parameter #4 $default of method Symfony\Component\Console\Command\Command::addArgument() expects array<int, string>|null, %s given.',
$defaultType->describe(VerbosityLevel::typeOnly())
))->build(),
];
}

return [];
Expand Down
20 changes: 14 additions & 6 deletions src/Rules/Symfony/InvalidOptionDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand All @@ -32,9 +32,6 @@ public function getNodeType(): string
return MethodCall::class;
}

/**
* @return (string|RuleError)[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
if (!(new ObjectType('Symfony\Component\Console\Command\Command'))->isSuperTypeOf($scope->getType($node->var))->yes()) {
Expand Down Expand Up @@ -66,13 +63,24 @@ public function processNode(Node $node, Scope $scope): array
if (($mode & 8) !== 8) {
$checkType = new UnionType([new StringType(), new IntegerType(), new NullType(), new BooleanType()]);
if (!$checkType->isSuperTypeOf($defaultType)->yes()) {
return [sprintf('Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects %s, %s given.', $checkType->describe(VerbosityLevel::typeOnly()), $defaultType->describe(VerbosityLevel::typeOnly()))];
return [
RuleErrorBuilder::message(sprintf(
'Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects %s, %s given.',
$checkType->describe(VerbosityLevel::typeOnly()),
$defaultType->describe(VerbosityLevel::typeOnly())
))->build(),
];
}
}

// is array
if (($mode & 8) === 8 && !(new UnionType([new ArrayType(new MixedType(), new StringType()), new NullType()]))->isSuperTypeOf($defaultType)->yes()) {
return [sprintf('Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array<string>|null, %s given.', $defaultType->describe(VerbosityLevel::typeOnly()))];
return [
RuleErrorBuilder::message(sprintf(
'Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array<string>|null, %s given.',
$defaultType->describe(VerbosityLevel::typeOnly())
))->build(),
];
}

return [];
Expand Down
7 changes: 2 additions & 5 deletions src/Rules/Symfony/UndefinedArgumentRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Symfony\ConsoleApplicationResolver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Symfony\Helper;
Expand Down Expand Up @@ -39,9 +39,6 @@ public function getNodeType(): string
return MethodCall::class;
}

/**
* @return (string|RuleError)[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
Expand Down Expand Up @@ -78,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
if ($scope->getType(Helper::createMarkerNode($node->var, $argType, $this->printer))->equals($argType)) {
continue;
}
$errors[] = sprintf('Command "%s" does not define argument "%s".', $name, $argName);
$errors[] = RuleErrorBuilder::message(sprintf('Command "%s" does not define argument "%s".', $name, $argName))->build();
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/Rules/Symfony/UndefinedOptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Symfony\ConsoleApplicationResolver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Symfony\Helper;
Expand Down Expand Up @@ -39,9 +39,6 @@ public function getNodeType(): string
return MethodCall::class;
}

/**
* @return (string|RuleError)[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
Expand Down Expand Up @@ -78,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
if ($scope->getType(Helper::createMarkerNode($node->var, $optType, $this->printer))->equals($optType)) {
continue;
}
$errors[] = sprintf('Command "%s" does not define option "%s".', $name, $optName);
$errors[] = RuleErrorBuilder::message(sprintf('Command "%s" does not define option "%s".', $name, $optName))->build();
}
}

Expand Down

0 comments on commit 7e7b5bd

Please sign in to comment.