Skip to content

Commit 93a6490

Browse files
committed
Deduplicate logic
Signed-off-by: Joas Schilling <[email protected]>
1 parent b782712 commit 93a6490

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

src/Helpers.php

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Exception;
66
use PhpParser\Node;
7+
use PhpParser\Node\Arg;
8+
use PhpParser\Node\Attribute;
9+
use PhpParser\Node\AttributeGroup;
710
use PhpParser\Node\Expr\Array_;
811
use PhpParser\Node\Expr\ArrayItem;
912
use PhpParser\Node\Expr\ClassConstFetch;
@@ -141,7 +144,7 @@ static function classMethodHasAnnotationOrAttribute(ClassMethod|Class_|Node $nod
141144
return true;
142145
}
143146

144-
/** @var Node\AttributeGroup $attrGroup */
147+
/** @var AttributeGroup $attrGroup */
145148
foreach ($node->attrGroups as $attrGroup) {
146149
foreach ($attrGroup->attrs as $attr) {
147150
if ($attr->name->getLast() == $annotation) {
@@ -153,36 +156,49 @@ static function classMethodHasAnnotationOrAttribute(ClassMethod|Class_|Node $nod
153156
return false;
154157
}
155158

159+
protected static function getScopeNameFromAttributeArgument(Arg $arg, string $routeName): ?string {
160+
if ($arg->name->name === 'scope') {
161+
if ($arg->value instanceof ClassConstFetch) {
162+
if ($arg->value->class->getLast() === 'OpenAPI') {
163+
return self::getScopeNameFromConst($arg->value);
164+
}
165+
} elseif ($arg->value instanceof String_) {
166+
return $arg->value->value;
167+
} else {
168+
Logger::panic($routeName, 'Can not interpret value of scope provided in OpenAPI(scope: …) attribute. Please use string or OpenAPI::SCOPE_* constants');
169+
}
170+
}
171+
172+
return null;
173+
}
174+
175+
protected static function getScopeNameFromConst(ClassConstFetch $scope): string {
176+
return match ($scope->name->name) {
177+
'SCOPE_DEFAULT' => 'default',
178+
'SCOPE_ADMINISTRATION' => 'administration',
179+
'SCOPE_FEDERATION' => 'federation',
180+
'SCOPE_IGNORE' => 'ignore',
181+
// Fall back for future scopes assuming we follow the pattern (cut of 'SCOPE_' and lower case)
182+
default => strtolower(substr($scope->name->name, 6)),
183+
};
184+
}
185+
156186
static function getAttributeScopes(ClassMethod|Class_|Node $node, string $annotation, string $routeName): array {
157187
$scopes = [];
158188

159-
160-
/** @var Node\AttributeGroup $attrGroup */
189+
/** @var AttributeGroup $attrGroup */
161190
foreach ($node->attrGroups as $attrGroup) {
162191
foreach ($attrGroup->attrs as $attr) {
163192
if ($attr->name->getLast() === $annotation) {
164193
if (empty($attr->args)) {
165194
$scopes[] = 'default';
195+
continue;
166196
}
167197

168198
foreach ($attr->args as $arg) {
169-
if ($arg->name->name === 'scope') {
170-
if ($arg->value instanceof ClassConstFetch) {
171-
if ($arg->value->class->getLast() === 'OpenAPI') {
172-
$scopes[] = match ($arg->value->name->name) {
173-
'SCOPE_DEFAULT' => 'default',
174-
'SCOPE_ADMINISTRATION' => 'administration',
175-
'SCOPE_FEDERATION' => 'federation',
176-
'SCOPE_IGNORE' => 'ignore',
177-
// Fall back for future scopes assuming we follow the pattern (cut of 'SCOPE_' and lower case)
178-
default => strtolower(substr($arg->value->name->name, 6)),
179-
};
180-
}
181-
} elseif ($arg->value instanceof String_) {
182-
$scopes[] = $arg->value->value;
183-
} else {
184-
Logger::panic($routeName, 'Can not interpret value of scope provided in OpenAPI(scope: …) attribute. Please use string or OpenAPI::SCOPE_* constants');
185-
}
199+
$scope = self::getScopeNameFromAttributeArgument($arg, $routeName);
200+
if ($scope !== null) {
201+
$scopes[] = $scope;
186202
}
187203
}
188204
}
@@ -195,7 +211,7 @@ static function getAttributeScopes(ClassMethod|Class_|Node $node, string $annota
195211
static function getAttributeTagsByScope(ClassMethod|Class_|Node $node, string $annotation, string $routeName, string $defaultTag, string $defaultScope): array {
196212
$tags = [];
197213

198-
/** @var Node\AttributeGroup $attrGroup */
214+
/** @var AttributeGroup $attrGroup */
199215
foreach ($node->attrGroups as $attrGroup) {
200216
foreach ($attrGroup->attrs as $attr) {
201217
if ($attr->name->getLast() === $annotation) {
@@ -207,24 +223,7 @@ static function getAttributeTagsByScope(ClassMethod|Class_|Node $node, string $a
207223
$foundsTags = [];
208224
$foundScopeName = null;
209225
foreach ($attr->args as $arg) {
210-
if ($arg->name->name === 'scope') {
211-
if ($arg->value instanceof ClassConstFetch) {
212-
if ($arg->value->class->getLast() === 'OpenAPI') {
213-
$foundScopeName = match ($arg->value->name->name) {
214-
'SCOPE_DEFAULT' => 'default',
215-
'SCOPE_ADMINISTRATION' => 'administration',
216-
'SCOPE_FEDERATION' => 'federation',
217-
'SCOPE_IGNORE' => 'ignore',
218-
// Fall back for future scopes assuming we follow the pattern (cut of 'SCOPE_' and lower case)
219-
default => strtolower(substr($arg->value->name->name, 6)),
220-
};
221-
}
222-
} elseif ($arg->value instanceof String_) {
223-
$foundScopeName = $arg->value->value;
224-
} else {
225-
Logger::panic($routeName, 'Can not interpret value of scope provided in OpenAPI(scope: …) attribute. Please use string or OpenAPI::SCOPE_* constants');
226-
}
227-
}
226+
$foundScopeName = self::getScopeNameFromAttributeArgument($arg, $routeName);
228227

229228
if ($arg->name->name === 'tags') {
230229
if ($arg->value instanceof Array_) {

0 commit comments

Comments
 (0)