Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Cleanup left over helper functions #56

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -303,7 +303,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