Skip to content

Commit

Permalink
Use match instead of early return
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMuller committed Dec 11, 2024
1 parent 5e3f5cb commit 7c009f1
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/Rules/NamingClasses/EloquentApiResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,42 @@ public function processNode(Node $node, Scope $scope): array

$classReflection = $this->reflectionProvider->getClass($nameSpacedName);

if ($classReflection->isSubclassOf(ResourceCollection::class)) {
if (Str::endsWith($nameSpacedName, 'ResourceCollection')) {
return [];
}

$name = str($node->name instanceof Node\Identifier ? $node->name->toString() : $nameSpacedName)
->replace(['Collection', 'Resource'], '')
->append('ResourceCollection');

return [
RuleErrorBuilder::message(
"Eloquent resource collection {$nameSpacedName} must be named with a `ResourceCollection` suffix, such as {$name}."
)
->tip($this->tip())
->identifier('hihaho.naming.classes.eloquentApiResourceCollections')
->build(),
];
}
return match (true) {
$classReflection->isSubclassOf(ResourceCollection::class) => $this->processResourceCollection($node, $nameSpacedName),
$classReflection->isSubclassOf(JsonResource::class) => $this->processResource($node, $nameSpacedName),
default => [],
};
}

if (Str::endsWith($nameSpacedName, 'Resource')) {
/**
* @return list<\PHPStan\Rules\IdentifierRuleError>
*/
private function processResourceCollection(Class_ $node, string $nameSpacedName): array
{
if (Str::endsWith($nameSpacedName, 'ResourceCollection')) {
return [];
}

if (! $classReflection->isSubclassOf(JsonResource::class)) {
$name = str($node->name instanceof Node\Identifier ? $node->name->toString() : $nameSpacedName)
->replace(['Collection', 'Resource'], '')
->append('ResourceCollection');

return [
RuleErrorBuilder::message(
"Eloquent resource collection {$nameSpacedName} must be named with a `ResourceCollection` suffix, such as {$name}."
)
->tip($this->tip())
->identifier('hihaho.naming.classes.eloquentApiResourceCollections')
->build(),
];
}

/**
* @return list<\PHPStan\Rules\IdentifierRuleError>
*/
private function processResource(Class_ $node, string $nameSpacedName): array
{
if (Str::endsWith($nameSpacedName, 'Resource')) {
return [];
}

Expand Down

0 comments on commit 7c009f1

Please sign in to comment.