Skip to content

Commit

Permalink
Merge pull request #56 from nextcloud/refactor/left-over-helper-funct…
Browse files Browse the repository at this point in the history
…ions

refactor: Cleanup left over helper functions
  • Loading branch information
nickvergessen authored Dec 21, 2023
2 parents f00db4c + 0949211 commit 4720109
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
18 changes: 4 additions & 14 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ $tags = [];
$definitions = [];
$definitionsPath = $sourceDir . "/ResponseDefinitions.php";
if (file_exists($definitionsPath)) {
foreach ($nodeFinder->findInstanceOf(loadAST($definitionsPath), Class_::class) as $node) {
foreach ($nodeFinder->findInstanceOf($astParser->parse(file_get_contents($definitionsPath)), Class_::class) as $node) {
$doc = $node->getDocComment()?->getText();
if ($doc != null) {
$docNodes = $phpDocParser->parse(new TokenIterator($lexer->tokenize($doc)))->children;
Expand All @@ -148,7 +148,7 @@ if (file_exists($definitionsPath)) {
}
}
foreach (array_keys($definitions) as $name) {
$schemas[cleanSchemaName($name)] = OpenApiType::resolve("Response definitions", $definitions, $definitions[$name])->toArray($openapiVersion);
$schemas[Helpers::cleanSchemaName($name)] = OpenApiType::resolve("Response definitions", $definitions, $definitions[$name])->toArray($openapiVersion);
}
} else {
Logger::debug("Response definitions", "No response definitions were loaded");
Expand All @@ -170,7 +170,7 @@ foreach ($capabilitiesFiles as $path) {
/**
* @var Class_ $node
*/
foreach ($nodeFinder->findInstanceOf(loadAST($path), Class_::class) as $node) {
foreach ($nodeFinder->findInstanceOf($astParser->parse(file_get_contents($path)), Class_::class) as $node) {
$implementsCapability = count(array_filter($node->implements, fn (Name $name) => $name->getLast() == "ICapability")) > 0;
$implementsPublicCapability = count(array_filter($node->implements, fn (Name $name) => $name->getLast() == "IPublicCapability")) > 0;
if (!$implementsCapability && !$implementsPublicCapability) {
Expand Down Expand Up @@ -268,7 +268,7 @@ foreach ($parsedRoutes as $key => $value) {
$controllerName = ucfirst(str_replace("_", "", ucwords(explode("#", $routeName)[0], "_")));
$controllerClass = null;
/** @var Class_ $class */
foreach ($nodeFinder->findInstanceOf(loadAST($sourceDir . "/Controller/" . $controllerName . "Controller.php"), Class_::class) as $class) {
foreach ($nodeFinder->findInstanceOf($astParser->parse(file_get_contents($sourceDir . "/Controller/" . $controllerName . "Controller.php")), Class_::class) as $class) {
if ($class->name == $controllerName . "Controller") {
$controllerClass = $class;
break;
Expand Down Expand Up @@ -701,13 +701,3 @@ if ($useTags) {
}

file_put_contents($out, json_encode($openapi, Helpers::jsonFlags()));

function cleanSchemaName(string $name): string {
global $readableAppID;
return substr($name, strlen($readableAppID));
}

function loadAST(string $filename): array {
global $astParser;
return $astParser->parse(file_get_contents($filename));
}
5 changes: 5 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ public static function classMethodHasAnnotationOrAttribute(ClassMethod|Class_|No

return false;
}

public static function cleanSchemaName(string $name): string {
global $readableAppID;
return substr($name, strlen($readableAppID));
}
}
2 changes: 1 addition & 1 deletion src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private static function resolveIdentifier(string $context, array $definitions, s
default => (function () use ($context, $definitions, $name) {
if (array_key_exists($name, $definitions)) {
return new OpenApiType(
ref: "#/components/schemas/" . cleanSchemaName($name),
ref: "#/components/schemas/" . Helpers::cleanSchemaName($name),
);
}
Logger::panic($context, "Unable to resolve OpenAPI type for identifier '" . $name . "'");
Expand Down

0 comments on commit 4720109

Please sign in to comment.