diff --git a/src/Classes/FinalRule.php b/src/Classes/FinalRule.php index 63c5fd42..2d985397 100644 --- a/src/Classes/FinalRule.php +++ b/src/Classes/FinalRule.php @@ -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(), ]; } diff --git a/src/Classes/NoExtendsRule.php b/src/Classes/NoExtendsRule.php index 6d0ebd56..c2ae51d1 100644 --- a/src/Classes/NoExtendsRule.php +++ b/src/Classes/NoExtendsRule.php @@ -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(), ]; } } diff --git a/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php b/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php index 1ce042d4..78b38ffa 100644 --- a/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php +++ b/src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php @@ -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(), ]; } } diff --git a/src/Closures/NoNullableReturnTypeDeclarationRule.php b/src/Closures/NoNullableReturnTypeDeclarationRule.php index 09c8bb00..485854a1 100644 --- a/src/Closures/NoNullableReturnTypeDeclarationRule.php +++ b/src/Closures/NoNullableReturnTypeDeclarationRule.php @@ -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(), ]; } diff --git a/src/Closures/NoParameterWithNullDefaultValueRule.php b/src/Closures/NoParameterWithNullDefaultValueRule.php index 52c89bda..57ef62c6 100644 --- a/src/Closures/NoParameterWithNullDefaultValueRule.php +++ b/src/Closures/NoParameterWithNullDefaultValueRule.php @@ -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); } } diff --git a/src/Closures/NoParameterWithNullableTypeDeclarationRule.php b/src/Closures/NoParameterWithNullableTypeDeclarationRule.php index 07e14c59..bddf4a4d 100644 --- a/src/Closures/NoParameterWithNullableTypeDeclarationRule.php +++ b/src/Closures/NoParameterWithNullableTypeDeclarationRule.php @@ -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); } diff --git a/src/Expressions/NoCompactRule.php b/src/Expressions/NoCompactRule.php index 81a4b92f..e218baaf 100644 --- a/src/Expressions/NoCompactRule.php +++ b/src/Expressions/NoCompactRule.php @@ -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(), ]; } } diff --git a/src/Expressions/NoErrorSuppressionRule.php b/src/Expressions/NoErrorSuppressionRule.php index 0360bdfc..f6c821af 100644 --- a/src/Expressions/NoErrorSuppressionRule.php +++ b/src/Expressions/NoErrorSuppressionRule.php @@ -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(), ]; } } diff --git a/src/Expressions/NoEvalRule.php b/src/Expressions/NoEvalRule.php index 17384300..b57279a3 100644 --- a/src/Expressions/NoEvalRule.php +++ b/src/Expressions/NoEvalRule.php @@ -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(), ]; } } diff --git a/src/Expressions/NoIssetRule.php b/src/Expressions/NoIssetRule.php index 9a47eba2..1b16b9d9 100644 --- a/src/Expressions/NoIssetRule.php +++ b/src/Expressions/NoIssetRule.php @@ -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(), ]; } } diff --git a/src/Files/DeclareStrictTypesRule.php b/src/Files/DeclareStrictTypesRule.php index aa57d5ec..aa9baeab 100644 --- a/src/Files/DeclareStrictTypesRule.php +++ b/src/Files/DeclareStrictTypesRule.php @@ -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(), ]; } } diff --git a/src/Functions/NoNullableReturnTypeDeclarationRule.php b/src/Functions/NoNullableReturnTypeDeclarationRule.php index 27aacd2e..d384dfc2 100644 --- a/src/Functions/NoNullableReturnTypeDeclarationRule.php +++ b/src/Functions/NoNullableReturnTypeDeclarationRule.php @@ -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(), ]; } diff --git a/src/Functions/NoParameterWithNullDefaultValueRule.php b/src/Functions/NoParameterWithNullDefaultValueRule.php index 1e2a237a..05cfe26b 100644 --- a/src/Functions/NoParameterWithNullDefaultValueRule.php +++ b/src/Functions/NoParameterWithNullDefaultValueRule.php @@ -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); } } diff --git a/src/Functions/NoParameterWithNullableTypeDeclarationRule.php b/src/Functions/NoParameterWithNullableTypeDeclarationRule.php index 96871938..8c88b432 100644 --- a/src/Functions/NoParameterWithNullableTypeDeclarationRule.php +++ b/src/Functions/NoParameterWithNullableTypeDeclarationRule.php @@ -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); } diff --git a/src/Methods/FinalInAbstractClassRule.php b/src/Methods/FinalInAbstractClassRule.php index b4a583b7..f943c27c 100644 --- a/src/Methods/FinalInAbstractClassRule.php +++ b/src/Methods/FinalInAbstractClassRule.php @@ -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(), ]; } } diff --git a/src/Methods/NoConstructorParameterWithDefaultValueRule.php b/src/Methods/NoConstructorParameterWithDefaultValueRule.php index 437dca82..8224f2d4 100644 --- a/src/Methods/NoConstructorParameterWithDefaultValueRule.php +++ b/src/Methods/NoConstructorParameterWithDefaultValueRule.php @@ -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); } diff --git a/src/Methods/NoNullableReturnTypeDeclarationRule.php b/src/Methods/NoNullableReturnTypeDeclarationRule.php index 4039b9ca..fb7035cf 100644 --- a/src/Methods/NoNullableReturnTypeDeclarationRule.php +++ b/src/Methods/NoNullableReturnTypeDeclarationRule.php @@ -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(), ]; } diff --git a/src/Methods/NoParameterWithContainerTypeDeclarationRule.php b/src/Methods/NoParameterWithContainerTypeDeclarationRule.php index a6ed60f8..0b707a59 100644 --- a/src/Methods/NoParameterWithContainerTypeDeclarationRule.php +++ b/src/Methods/NoParameterWithContainerTypeDeclarationRule.php @@ -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(); } } diff --git a/src/Methods/NoParameterWithNullDefaultValueRule.php b/src/Methods/NoParameterWithNullDefaultValueRule.php index 0e952f89..7bfd4126 100644 --- a/src/Methods/NoParameterWithNullDefaultValueRule.php +++ b/src/Methods/NoParameterWithNullDefaultValueRule.php @@ -60,36 +60,40 @@ public function processNode( $classReflection = $scope->getClassReflection(); if ($classReflection->isAnonymous()) { - return \array_map(static function (Node\Param $node) use ($methodName): string { + return \array_map(static function (Node\Param $node) use ($methodName): Rules\RuleError { /** @var Node\Expr\Variable $variable */ $variable = $node->var; /** @var string $parameterName */ $parameterName = $variable->name; - return \sprintf( + $ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf( 'Method %s() in anonymous class has parameter $%s with null as default value.', $methodName, $parameterName, - ); + )); + + return $ruleErrorBuilder->build(); }, $params); } $className = $classReflection->getName(); - return \array_map(static function (Node\Param $node) use ($className, $methodName): string { + return \array_map(static function (Node\Param $node) use ($className, $methodName): Rules\RuleError { /** @var Node\Expr\Variable $variable */ $variable = $node->var; /** @var string $parameterName */ $parameterName = $variable->name; - return \sprintf( + $ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf( 'Method %s::%s() has parameter $%s with null as default value.', $className, $methodName, $parameterName, - ); + )); + + return $ruleErrorBuilder->build(); }, $params); } } diff --git a/src/Methods/NoParameterWithNullableTypeDeclarationRule.php b/src/Methods/NoParameterWithNullableTypeDeclarationRule.php index 165a9922..437fb97c 100644 --- a/src/Methods/NoParameterWithNullableTypeDeclarationRule.php +++ b/src/Methods/NoParameterWithNullableTypeDeclarationRule.php @@ -56,36 +56,40 @@ public function processNode( $classReflection = $scope->getClassReflection(); if ($classReflection->isAnonymous()) { - return \array_map(static function (Node\Param $node) use ($methodName): string { + return \array_map(static function (Node\Param $node) use ($methodName): Rules\RuleError { /** @var Node\Expr\Variable $variable */ $variable = $node->var; /** @var string $parameterName */ $parameterName = $variable->name; - return \sprintf( + $ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf( 'Method %s() in anonymous class has parameter $%s with a nullable type declaration.', $methodName, $parameterName, - ); + )); + + return $ruleErrorBuilder->build(); }, $params); } $className = $classReflection->getName(); - return \array_map(static function (Node\Param $node) use ($className, $methodName): string { + return \array_map(static function (Node\Param $node) use ($className, $methodName): Rules\RuleError { /** @var Node\Expr\Variable $variable */ $variable = $node->var; /** @var string $parameterName */ $parameterName = $variable->name; - return \sprintf( + $ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf( 'Method %s::%s() has parameter $%s with a nullable type declaration.', $className, $methodName, $parameterName, - ); + )); + + return $ruleErrorBuilder->build(); }, $params); } diff --git a/src/Methods/PrivateInFinalClassRule.php b/src/Methods/PrivateInFinalClassRule.php index 6b985f51..5e21d74b 100644 --- a/src/Methods/PrivateInFinalClassRule.php +++ b/src/Methods/PrivateInFinalClassRule.php @@ -65,12 +65,14 @@ public function processNode( } } + $ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf( + 'Method %s::%s() is protected, but since the containing class is final, it can be private.', + $containingClass->getName(), + $methodName, + )); + return [ - \sprintf( - 'Method %s::%s() is protected, but since the containing class is final, it can be private.', - $containingClass->getName(), - $methodName, - ), + $ruleErrorBuilder->build(), ]; } } diff --git a/src/Statements/NoSwitchRule.php b/src/Statements/NoSwitchRule.php index 280b64ea..1d711e53 100644 --- a/src/Statements/NoSwitchRule.php +++ b/src/Statements/NoSwitchRule.php @@ -28,8 +28,10 @@ public function processNode( Node $node, Analyser\Scope $scope, ): array { + $ruleErrorBuilder = Rules\RuleErrorBuilder::message('Control structures using switch should not be used.'); + return [ - 'Control structures using switch should not be used.', + $ruleErrorBuilder->build(), ]; } }