From 18a4b3644d5c32af635743941fd9645feadda4cc Mon Sep 17 00:00:00 2001 From: Geert Broekmans Date: Wed, 21 Feb 2024 11:21:43 +0100 Subject: [PATCH] Fix shouldBeNamed assertion not functioning on second run --- .../Declaration/DeclarationAssertion.php | 75 ++++++++----------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/src/Rule/Assertion/Declaration/DeclarationAssertion.php b/src/Rule/Assertion/Declaration/DeclarationAssertion.php index 94e90a77..1e16e528 100644 --- a/src/Rule/Assertion/Declaration/DeclarationAssertion.php +++ b/src/Rule/Assertion/Declaration/DeclarationAssertion.php @@ -43,19 +43,41 @@ public function __construct( */ public function processNode(Node $node, Scope $scope): array { - if (!$this->ruleApplies($scope)) { + $subject = $scope->getClassReflection(); + if ($subject === null) { return []; } - $meetsDeclaration = $this->meetsDeclaration($node, $scope, $this->getParams()); + $applicableStatements = array_filter( + $this->statements, + static function (array $statement) use ($subject): bool { + [$ruleName, $selector, $subjectExcludes, $tips, $params] = $statement; - return $this->validateGetErrors($scope, $meetsDeclaration); - } + if ($subject->isBuiltin() || !$selector->matches($subject)) { + return false; + } + foreach ($subjectExcludes as $exclude) { + if ($exclude->matches($subject)) { + return false; + } + } - // TODO: This is a temporary hack, the 'statement' concept needs to be reworked - public function getParams(): array - { - return $this->statements[0][4] ?? []; + return true; + } + ); + + return array_reduce( + $applicableStatements, + function (array $errors, array $statement) use ($node, $scope, $subject): array { + [$ruleName, $selector, $subjectExcludes, $tips, $params] = $statement; + + $meetsDeclaration = $this->meetsDeclaration($node, $scope, $statement[4]); + array_push($errors, ...$this->applyValidation($ruleName, $subject, $meetsDeclaration, $tips, $params)); + + return $errors; + }, + [] + ); } public function prepareMessage(string $ruleName, string $message): string @@ -77,41 +99,4 @@ abstract protected function getMessage(string $ruleName, string $subject, array * @return array */ abstract protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips, array $params = []): array; - - protected function ruleApplies(Scope $scope): bool - { - if (!$scope->isInClass()) { - return false; - } - - return $scope->getClassReflection() !== null; - } - - /** - * @return array - * @throws ShouldNotHappenException - */ - protected function validateGetErrors(Scope $scope, bool $meetsDeclaration): array - { - $errors = []; - $subject = $scope->getClassReflection(); - if ($subject === null) { - throw new ShouldNotHappenException(); - } - - foreach ($this->statements as [$ruleName, $selector, $subjectExcludes, $tips, $params]) { - if ($subject->isBuiltin() || !$selector->matches($subject)) { - continue; - } - foreach ($subjectExcludes as $exclude) { - if ($exclude->matches($subject)) { - continue 2; - } - } - - array_push($errors, ...$this->applyValidation($ruleName, $subject, $meetsDeclaration, $tips, $params)); - } - - return $errors; - } }