Skip to content

Commit

Permalink
Enhancement: Return RuleError
Browse files Browse the repository at this point in the history
Slightly related to #562.
  • Loading branch information
localheinz committed Jun 26, 2023
1 parent a396086 commit e13ae4f
Show file tree
Hide file tree
Showing 22 changed files with 134 additions and 80 deletions.
10 changes: 6 additions & 4 deletions src/Classes/FinalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ public function processNode(
return [];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
$this->errorMessageTemplate,
$node->namespacedName->toString(),
));

return [
\sprintf(
$this->errorMessageTemplate,
$node->namespacedName->toString(),
),
$ruleErrorBuilder->build(),
];
}

Expand Down
22 changes: 13 additions & 9 deletions src/Classes/NoExtendsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,24 @@ public function processNode(
}

if (!isset($node->namespacedName)) {
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Anonymous class is not allowed to extend "%s".',
$extendedClassName,
));

return [
\sprintf(
'Anonymous class is not allowed to extend "%s".',
$extendedClassName,
),
$ruleErrorBuilder->build(),
];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Class "%s" is not allowed to extend "%s".',
$node->namespacedName->toString(),
$extendedClassName,
));

return [
\sprintf(
'Class "%s" is not allowed to extend "%s".',
$node->namespacedName->toString(),
$extendedClassName,
),
$ruleErrorBuilder->build(),
];
}
}
12 changes: 7 additions & 5 deletions src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ public function processNode(
return [];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Class %s extends %s, is concrete, but does not have a Test suffix.',
$fullyQualifiedClassName,
$extendedPhpunitTestCaseClassName,
));

return [
\sprintf(
'Class %s extends %s, is concrete, but does not have a Test suffix.',
$fullyQualifiedClassName,
$extendedPhpunitTestCaseClassName,
),
$ruleErrorBuilder->build(),
];
}
}
4 changes: 3 additions & 1 deletion src/Closures/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public function processNode(
return [];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Closure has a nullable return type declaration.');

return [
'Closure has a nullable return type declaration.',
$ruleErrorBuilder->build(),
];
}

Expand Down
8 changes: 5 additions & 3 deletions src/Closures/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ public function processNode(
return [];
}

return \array_map(static function (Node\Param $node): string {
return \array_map(static function (Node\Param $node): Rules\RuleError {
/** @var Node\Expr\Variable $variable */
$variable = $node->var;

/** @var string $parameterName */
$parameterName = $variable->name;

return \sprintf(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Closure has parameter $%s with null as default value.',
$parameterName,
);
));

return $ruleErrorBuilder->build();
}, $params);
}
}
8 changes: 5 additions & 3 deletions src/Closures/NoParameterWithNullableTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ public function processNode(
return [];
}

return \array_map(static function (Node\Param $node): string {
return \array_map(static function (Node\Param $node): Rules\RuleError {
/** @var Node\Expr\Variable $variable */
$variable = $node->var;

/** @var string $parameterName */
$parameterName = $variable->name;

return \sprintf(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Closure has parameter $%s with a nullable type declaration.',
$parameterName,
);
));

return $ruleErrorBuilder->build();
}, $params);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Expressions/NoCompactRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public function processNode(
return [];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Function compact() should not be used.');

return [
'Function compact() should not be used.',
$ruleErrorBuilder->build(),
];
}
}
4 changes: 3 additions & 1 deletion src/Expressions/NoErrorSuppressionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public function processNode(
Node $node,
Analyser\Scope $scope,
): array {
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Error suppression via "@" should not be used.');

return [
'Error suppression via "@" should not be used.',
$ruleErrorBuilder->build(),
];
}
}
4 changes: 3 additions & 1 deletion src/Expressions/NoEvalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public function processNode(
Node $node,
Analyser\Scope $scope,
): array {
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Language construct eval() should not be used.');

return [
'Language construct eval() should not be used.',
$ruleErrorBuilder->build(),
];
}
}
4 changes: 3 additions & 1 deletion src/Expressions/NoIssetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public function processNode(
Node $node,
Analyser\Scope $scope,
): array {
$ruleErrorBuilder = Rules\RuleErrorBuilder::message('Language construct isset() should not be used.');

return [
'Language construct isset() should not be used.',
$ruleErrorBuilder->build(),
];
}
}
4 changes: 3 additions & 1 deletion src/Files/DeclareStrictTypesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ public function processNode(
}
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message('File is missing a "declare(strict_types=1)" declaration.');

return [
'File is missing a "declare(strict_types=1)" declaration.',
$ruleErrorBuilder->build(),
];
}
}
10 changes: 6 additions & 4 deletions src/Functions/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public function processNode(
return [];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Function %s() has a nullable return type declaration.',
$node->namespacedName->toString(),
));

return [
\sprintf(
'Function %s() has a nullable return type declaration.',
$node->namespacedName->toString(),
),
$ruleErrorBuilder->build(),
];
}

Expand Down
8 changes: 5 additions & 3 deletions src/Functions/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ public function processNode(

$functionName = $node->namespacedName;

return \array_map(static function (Node\Param $node) use ($functionName): string {
return \array_map(static function (Node\Param $node) use ($functionName): Rules\RuleError {
/** @var Node\Expr\Variable $variable */
$variable = $node->var;

/** @var string $parameterName */
$parameterName = $variable->name;

return \sprintf(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Function %s() has parameter $%s with null as default value.',
$functionName,
$parameterName,
);
));

return $ruleErrorBuilder->build();
}, $params);
}
}
8 changes: 5 additions & 3 deletions src/Functions/NoParameterWithNullableTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ public function processNode(

$functionName = $node->namespacedName;

return \array_map(static function (Node\Param $node) use ($functionName): string {
return \array_map(static function (Node\Param $node) use ($functionName): Rules\RuleError {
/** @var Node\Expr\Variable $variable */
$variable = $node->var;

/** @var string $parameterName */
$parameterName = $variable->name;

return \sprintf(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Function %s() has parameter $%s with a nullable type declaration.',
$functionName,
$parameterName,
);
));

return $ruleErrorBuilder->build();
}, $params);
}

Expand Down
12 changes: 7 additions & 5 deletions src/Methods/FinalInAbstractClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ public function processNode(
return [];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Method %s::%s() is not final, but since the containing class is abstract, it should be.',
$containingClass->getName(),
$node->name->toString(),
));

return [
\sprintf(
'Method %s::%s() is not final, but since the containing class is abstract, it should be.',
$containingClass->getName(),
$node->name->toString(),
),
$ruleErrorBuilder->build(),
];
}
}
8 changes: 5 additions & 3 deletions src/Methods/NoConstructorParameterWithDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ public function processNode(
$classReflection = $scope->getClassReflection();

if ($classReflection->isAnonymous()) {
return \array_map(static function (Node\Param $node): string {
return \array_map(static function (Node\Param $node): Rules\RuleError {
/** @var Node\Expr\Variable $variable */
$variable = $node->var;

/** @var string $parameterName */
$parameterName = $variable->name;

return \sprintf(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Constructor in anonymous class has parameter $%s with default value.',
$parameterName,
);
));

return $ruleErrorBuilder->build();
}, $params);
}

Expand Down
22 changes: 13 additions & 9 deletions src/Methods/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@ public function processNode(
$classReflection = $scope->getClassReflection();

if ($classReflection->isAnonymous()) {
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Method %s() in anonymous class has a nullable return type declaration.',
$node->name->name,
));

return [
\sprintf(
'Method %s() in anonymous class has a nullable return type declaration.',
$node->name->name,
),
$ruleErrorBuilder->build(),
];
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Method %s::%s() has a nullable return type declaration.',
$classReflection->getName(),
$node->name->name,
));

return [
\sprintf(
'Method %s::%s() has a nullable return type declaration.',
$classReflection->getName(),
$node->name->name,
),
$ruleErrorBuilder->build(),
];
}

Expand Down
14 changes: 9 additions & 5 deletions src/Methods/NoParameterWithContainerTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,26 @@ private static function createError(
string $methodName,
string $parameterName,
Reflection\ClassReflection $classUsedInTypeDeclaration,
): string {
): Rules\RuleError {
if ($classReflection->isAnonymous()) {
return \sprintf(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Method %s() in anonymous class has a parameter $%s with a type declaration of %s, but containers should not be injected.',
$methodName,
$parameterName,
$classUsedInTypeDeclaration->getName(),
);
));

return $ruleErrorBuilder->build();
}

return \sprintf(
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
'Method %s::%s() has a parameter $%s with a type declaration of %s, but containers should not be injected.',
$classReflection->getName(),
$methodName,
$parameterName,
$classUsedInTypeDeclaration->getName(),
);
));

return $ruleErrorBuilder->build();
}
}

0 comments on commit e13ae4f

Please sign in to comment.