Skip to content

Commit

Permalink
Enhancement: Extract variable
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 13, 2024
1 parent fd1aaad commit 8ad6f71
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 42 deletions.
6 changes: 4 additions & 2 deletions src/Classes/FinalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public function processNode(
return [];
}

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand Down
12 changes: 8 additions & 4 deletions src/Classes/NoExtendsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ public function processNode(
}

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand All @@ -81,11 +83,13 @@ public function processNode(
];
}

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand Down
6 changes: 4 additions & 2 deletions src/Classes/PHPUnit/Framework/TestCaseWithSuffixRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ public function processNode(
return [];
}

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand Down
6 changes: 4 additions & 2 deletions src/Closures/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithNullDefaultValue()->toString())
Expand Down
6 changes: 4 additions & 2 deletions src/Closures/NoParameterWithNullableTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithNullableTypeDeclaration()->toString())
Expand Down
6 changes: 4 additions & 2 deletions src/Functions/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public function processNode(
return [];
}

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand Down
6 changes: 4 additions & 2 deletions src/Functions/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithNullDefaultValue()->toString())
Expand Down
6 changes: 4 additions & 2 deletions src/Functions/NoParameterWithNullableTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithNullableTypeDeclaration()->toString())
Expand Down
6 changes: 4 additions & 2 deletions src/Methods/FinalInAbstractClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ public function processNode(
return [];
}

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand Down
12 changes: 8 additions & 4 deletions src/Methods/NoConstructorParameterWithDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noConstructorParameterWithDefaultValue()->toString())
Expand All @@ -80,11 +82,13 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noConstructorParameterWithDefaultValue()->toString())
Expand Down
12 changes: 8 additions & 4 deletions src/Methods/NoNullableReturnTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ public function processNode(
$classReflection = $scope->getClassReflection();

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand All @@ -53,11 +55,13 @@ public function processNode(
];
}

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand Down
12 changes: 8 additions & 4 deletions src/Methods/NoParameterWithContainerTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,29 @@ private static function createError(
Reflection\ClassReflection $classUsedInTypeDeclaration
): Rules\RuleError {
if ($classReflection->isAnonymous()) {
$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
$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(),
));
);

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithContainerTypeDeclaration()->toString())
->build();
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
$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(),
));
);

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithContainerTypeDeclaration()->toString())
Expand Down
12 changes: 8 additions & 4 deletions src/Methods/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithNullDefaultValue()->toString())
Expand All @@ -83,12 +85,14 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithNullDefaultValue()->toString())
Expand Down
12 changes: 8 additions & 4 deletions src/Methods/NoParameterWithNullableTypeDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
$message = \sprintf(
'Method %s() in anonymous class has parameter $%s with a nullable type declaration.',
$methodName,
$parameterName,
));
);

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithContainerTypeDeclaration()->toString())
Expand All @@ -79,12 +81,14 @@ public function processNode(
/** @var string $parameterName */
$parameterName = $variable->name;

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

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return $ruleErrorBuilder
->identifier(ErrorIdentifier::noParameterWithContainerTypeDeclaration()->toString())
Expand Down
6 changes: 4 additions & 2 deletions src/Methods/PrivateInFinalClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ public function processNode(
}
}

$ruleErrorBuilder = Rules\RuleErrorBuilder::message(\sprintf(
$message = \sprintf(
'Method %s::%s() is protected, but since the containing class is final, it can be private.',
$containingClass->getName(),
$methodName,
));
);

$ruleErrorBuilder = Rules\RuleErrorBuilder::message($message);

return [
$ruleErrorBuilder
Expand Down

0 comments on commit 8ad6f71

Please sign in to comment.