Skip to content

Commit

Permalink
add parseClassAttributes and parseMethodAttributes and update return …
Browse files Browse the repository at this point in the history
…type
  • Loading branch information
chriskapp committed Dec 29, 2024
1 parent 5afb756 commit 5bd1192
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Parser/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
use PSX\Schema\Parser\ContextInterface;
use PSX\Schema\SchemaManagerInterface;
use PSX\Schema\Type;
use PSX\Schema\TypeInterface;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;

/**
* Attribute
Expand Down Expand Up @@ -76,7 +77,7 @@ public function parse(string $schema, ?ContextInterface $context = null): Specif
$controller = new ReflectionClass(str_replace('.', '\\', $schema));
$basePath = dirname($controller->getFileName());

$rootMeta = Meta::fromAttributes($controller->getAttributes());
$rootMeta = $this->parseClassAttributes($controller);
$specification = new Specification();

$this->parseMethods($controller, $specification, $basePath, $rootMeta);
Expand All @@ -97,7 +98,7 @@ public function parse(string $schema, ?ContextInterface $context = null): Specif
private function parseMethods(ReflectionClass $controller, SpecificationInterface $specification, string $basePath, Meta $rootMeta): void
{
foreach ($controller->getMethods() as $method) {
$meta = Meta::fromAttributes($method->getAttributes());
$meta = $this->parseMethodAttributes($method);
$meta->merge($rootMeta);

if ($meta->isExcluded()) {
Expand Down Expand Up @@ -251,6 +252,16 @@ public function getResponsesInRange(Meta $meta, int $start, int $end): array
return $result;
}

protected function parseClassAttributes(ReflectionClass $controller): Meta
{
return Meta::fromAttributes($controller->getAttributes());
}

protected function parseMethodAttributes(ReflectionMethod $method): Meta
{
return Meta::fromAttributes($method->getAttributes());
}

/**
* @throws InvalidSchemaException
*/
Expand All @@ -267,7 +278,7 @@ private function getBodySchema(Attr\SchemaAbstract $annotation, DefinitionsInter
return Type\Factory\PropertyTypeFactory::getReference($schema->getRoot());
}

private function getParameter(Attr\ParamAbstract $param): TypeInterface
private function getParameter(Attr\ParamAbstract $param): Type\PropertyTypeAbstract
{
$type = match ($param->type) {
Type::INTEGER => Type\Factory\PropertyTypeFactory::getInteger(),
Expand Down Expand Up @@ -338,7 +349,7 @@ private function getSchemaFromTypeHint(?\ReflectionType $type): string|ContentTy
return null;
}

private function inspectTypeHints(\ReflectionMethod $method, Meta $meta): void
private function inspectTypeHints(ReflectionMethod $method, Meta $meta): void
{
$missingPathNames = $this->getMissingPathNames($meta);
$pathNames = $this->getParamNames($meta->getPathParams());
Expand Down Expand Up @@ -414,7 +425,7 @@ private function inspectTypeHints(\ReflectionMethod $method, Meta $meta): void
/**
* @throws ParserException
*/
private function getFromAttribute(\ReflectionParameter $parameter, \ReflectionMethod $method): Attr\PathParam|Attr\QueryParam|Attr\HeaderParam|Attr\Incoming|null
private function getFromAttribute(ReflectionParameter $parameter, ReflectionMethod $method): Attr\PathParam|Attr\QueryParam|Attr\HeaderParam|Attr\Incoming|null
{
$attributes = $parameter->getAttributes();
foreach ($attributes as $attribute) {
Expand Down

0 comments on commit 5bd1192

Please sign in to comment.