-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Resolve enums and classes with PSR-4 #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OpenAPIExtractor; | ||
|
|
||
| use PhpParser\Node\Stmt\ClassLike; | ||
| use PhpParser\NodeFinder; | ||
| use PhpParser\NodeTraverser; | ||
| use PhpParser\Parser; | ||
|
|
||
| /** | ||
| * Lazily resolves a class, interface, trait or enum by its fully qualified name, | ||
| * by mapping namespace prefixes to source directories, PSR-4 style. | ||
| */ | ||
| class ClassResolver { | ||
| /** @var array<string, ClassLike|false> */ | ||
| private array $cache = []; | ||
|
|
||
| /** @var array<string, string> Namespace prefix => source directory */ | ||
| private readonly array $namespaceRoots; | ||
|
|
||
| /** @param array<string, string> $namespaceRoots Namespace prefix => source directory */ | ||
| public function __construct( | ||
| private readonly Parser $astParser, | ||
| private readonly NodeTraverser $nodeTraverser, | ||
| private readonly NodeFinder $nodeFinder, | ||
| array $namespaceRoots, | ||
| ) { | ||
| $this->namespaceRoots = array_combine( | ||
| array_map(static fn (string $prefix): string => trim($prefix, '\\'), array_keys($namespaceRoots)), | ||
| array_values($namespaceRoots), | ||
| ); | ||
| } | ||
|
|
||
| /** Returns null if the class can not be found, e.g. because it is outside of the known namespace roots. */ | ||
| public function resolve(string $fqcn): ?ClassLike { | ||
| $fqcn = ltrim($fqcn, '\\'); | ||
| if (!array_key_exists($fqcn, $this->cache)) { | ||
| $this->cache[$fqcn] = $this->load($fqcn) ?? false; | ||
| } | ||
|
|
||
| $node = $this->cache[$fqcn]; | ||
| return $node !== false ? $node : null; | ||
| } | ||
|
|
||
| private function load(string $fqcn): ?ClassLike { | ||
| $path = $this->findFile($fqcn); | ||
| if ($path === null || !is_file($path)) { | ||
| return null; | ||
| } | ||
|
|
||
| $contents = file_get_contents($path); | ||
| if ($contents === false) { | ||
| return null; | ||
| } | ||
|
|
||
| /** @var ClassLike $node */ | ||
| foreach ($this->nodeFinder->findInstanceOf($this->nodeTraverser->traverse($this->astParser->parse($contents)), ClassLike::class) as $node) { | ||
| if ($node->namespacedName?->toString() === $fqcn) { | ||
| $node->setAttribute('sourceFile', $path); | ||
| return $node; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** Maps the class name to a file path via its longest matching namespace prefix. */ | ||
| private function findFile(string $fqcn): ?string { | ||
| $bestPrefix = null; | ||
| foreach (array_keys($this->namespaceRoots) as $prefix) { | ||
| if (!str_starts_with($fqcn . '\\', $prefix . '\\')) { | ||
| continue; | ||
| } | ||
| if ($bestPrefix === null || strlen($prefix) > strlen($bestPrefix)) { | ||
| $bestPrefix = $prefix; | ||
| } | ||
| } | ||
|
|
||
| if ($bestPrefix === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $relativeName = substr($fqcn, strlen($bestPrefix) + 1); | ||
| return $this->namespaceRoots[$bestPrefix] . '/' . str_replace('\\', '/', $relativeName) . '.php'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OpenAPIExtractor; | ||
|
|
||
| use PhpParser\Node\Stmt\Enum_; | ||
| use PhpParser\Node\Stmt\EnumCase; | ||
| use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode; | ||
| use PHPStan\PhpDocParser\Lexer\Lexer; | ||
| use PHPStan\PhpDocParser\Parser\PhpDocParser; | ||
| use PHPStan\PhpDocParser\Parser\TokenIterator; | ||
|
|
||
| /** Resolves a backed enum's fully qualified class name into its OpenAPI representation. */ | ||
| class EnumResolver { | ||
| /** @var array<string, OpenApiType|false> */ | ||
| private array $cache = []; | ||
|
|
||
| public function __construct( | ||
| private readonly ClassResolver $classResolver, | ||
| private readonly PhpDocParser $phpDocParser, | ||
| private readonly Lexer $lexer, | ||
| ) { | ||
| } | ||
|
|
||
| public function resolve(string $fqcn): ?OpenApiType { | ||
| $fqcn = ltrim($fqcn, '\\'); | ||
| if (!array_key_exists($fqcn, $this->cache)) { | ||
| $this->cache[$fqcn] = $this->load($fqcn) ?? false; | ||
| } | ||
|
|
||
| $enum = $this->cache[$fqcn]; | ||
| return $enum !== false ? $enum : null; | ||
| } | ||
|
|
||
| private function load(string $fqcn): ?OpenApiType { | ||
| $node = $this->classResolver->resolve($fqcn); | ||
| if (!$node instanceof Enum_) { | ||
| return null; | ||
| } | ||
|
|
||
| $path = $node->getAttribute('sourceFile', $fqcn); | ||
|
|
||
| if ($node->scalarType === null) { | ||
| Logger::debug($path, "Enum '" . $fqcn . "' is not backed and can therefore not be used as an OpenAPI type. Use 'enum " . $node->name->name . ": string' or 'enum " . $node->name->name . ": int' instead."); | ||
| return null; | ||
| } | ||
|
|
||
| $values = []; | ||
| foreach ($node->stmts as $stmt) { | ||
| if ($stmt instanceof EnumCase && $stmt->expr !== null) { | ||
| $values[] = Helpers::exprToValue($path . ': ' . $fqcn . '::' . $stmt->name->name, $stmt->expr); | ||
| } | ||
| } | ||
|
|
||
| $description = null; | ||
| $doc = $node->getDocComment()?->getText(); | ||
| if ($doc != null) { | ||
| $descriptionLines = []; | ||
| $docNodes = $this->phpDocParser->parse(new TokenIterator($this->lexer->tokenize($doc)))->children; | ||
| foreach ($docNodes as $docNode) { | ||
| if ($docNode instanceof PhpDocTextNode) { | ||
| $block = Helpers::cleanDocComment($docNode->text); | ||
| if ($block !== '') { | ||
| $descriptionLines[] = $block; | ||
| } | ||
| } | ||
| } | ||
| if ($descriptionLines !== []) { | ||
| $description = implode("\n", $descriptionLines); | ||
| } | ||
| } | ||
|
|
||
| return new OpenApiType( | ||
| context: $path, | ||
| type: $node->scalarType->name === 'int' || $node->scalarType->name === 'integer' ? 'integer' : 'string', | ||
| format: $node->scalarType->name === 'int' || $node->scalarType->name === 'integer' ? 'int64' : null, | ||
| description: $description, | ||
| enum: $values, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Notifications\Notification; | ||
|
|
||
| /** | ||
| * The priority of a notification | ||
| * | ||
| * Declared in a sub-namespace/sub-directory to confirm that enums are resolved | ||
| * by mapping their namespace to a file path instead of relying on a directory scan. | ||
| */ | ||
| enum NotificationPriority: integer { | ||
| case Low = 0; | ||
| case Normal = 1; | ||
| case High = 2; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.