Skip to content

Commit

Permalink
More code cleanup and reflection analyser regression fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed Sep 24, 2021
1 parent d2a87c5 commit 9b3b713
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
12 changes: 1 addition & 11 deletions src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace OpenApi\Analysers;

use OpenApi\Annotations\AbstractAnnotation;
use OpenApi\Context;
use OpenApi\Generator;

Expand All @@ -26,28 +25,19 @@ public function build(\Reflector $reflector, Context $context): array
return [];
}

if ($context->is('annotations') === false) {
$context->annotations = [];
}

$context->comment = $reflector->getDocComment() ?: Generator::UNDEFINED;

// no proper way to inject
Generator::$context = $context;
$annotations = [];
try {
foreach ($reflector->getAttributes() as $attribute) {
$instance = $attribute->newInstance();
if ($instance instanceof AbstractAnnotation) {
$instance->_context = $context;
}
$annotations[] = $instance;
}
} finally {
Generator::$context = null;
}

return $context->annotations = array_filter($annotations, function ($a) {
return array_filter($annotations, function ($a) {
return $a !== null;
});
}
Expand Down
25 changes: 10 additions & 15 deletions src/Analysers/ReflectionAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public function fromFqdn(string $fqdn, Analysis $analysis): Analysis
$scanner = new TokenScanner();
$fileDetails = $scanner->scanFile($filename);

require_once $filename;

$this->analyzeFqdn($fqdn, $analysis, $fileDetails[$fqdn]);

return $analysis;
Expand All @@ -85,11 +83,13 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
}

$rc = new \ReflectionClass($fqdn);
$contextType = $this->contextType($rc);
$contextType = $rc->isInterface() ? 'interface' : ($rc->isTrait() ? 'trait' : 'class');
$context = new Context([
$contextType => $rc->getShortName(),
'namespace' => $rc->getNamespaceName() ?: Generator::UNDEFINED,
'comment' => $rc->getDocComment() ?: Generator::UNDEFINED,
'line' => $rc->getStartLine(),
'annotations' => [],
], $analysis->context);

$definition = [
Expand All @@ -101,31 +101,30 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
'methods' => [],
'context' => $context,
];
foreach ($this->annotationFactories as $annotationFactory) {
$analysis->addAnnotations($annotationFactory->build($rc, $context), $context);
}

$normaliseClass = function (string $name): string {
return '\\' . $name;
};

if ($parentClass = $rc->getParentClass()) {
$definition['extends'] = $normaliseClass($parentClass->getName());
}

if ($interfaceNames = $rc->getInterfaceNames()) {
$definition['implements'] = array_map($normaliseClass, $interfaceNames);
}

if ($traitNames = $rc->getTraitNames()) {
$definition['traits'] = array_map($normaliseClass, $traitNames);
}

foreach ($this->annotationFactories as $annotationFactory) {
$analysis->addAnnotations($annotationFactory->build($rc, $context), $context);
}

foreach ($rc->getMethods() as $method) {
if (in_array($method->name, $details['methods'])) {
$definition['methods'][$method->getName()] = $ctx = new Context([
'method' => $method->getName(),
'comment' => $method->getDocComment() ?: Generator::UNDEFINED,
'line' => $method->getStartLine(),
'annotations' => [],
], $context);
foreach ($this->annotationFactories as $annotationFactory) {
$analysis->addAnnotations($annotationFactory->build($method, $ctx), $ctx);
Expand All @@ -138,6 +137,7 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
$definition['properties'][$property->getName()] = $ctx = new Context([
'property' => $property->getName(),
'comment' => $property->getDocComment() ?: Generator::UNDEFINED,
'annotations' => [],
], $context);
if ($property->isStatic()) {
$ctx->static = true;
Expand All @@ -159,9 +159,4 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)

return $analysis;
}

protected function contextType(\ReflectionClass $rc): string
{
return $rc->isInterface() ? 'interface' : ($rc->isTrait() ? 'trait' : 'class');
}
}
1 change: 1 addition & 0 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function addAnnotation($annotation, Context $context): void
if ($context->is('annotations') === false) {
$context->annotations = [];
}

if (in_array($annotation, $context->annotations, true) === false) {
$context->annotations[] = $annotation;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ protected function combine(...$args): array
if (is_array($arg)) {
$combined = array_merge($combined, $arg);
} else {
$combined[] = $arg;
$combined[] = $arg;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ public function phpdocDescription()
*/
public function phpdocContent()
{
if ($this->comment === Generator::UNDEFINED) {
return Generator::UNDEFINED;
}

$comment = preg_split('/(\n|\r\n)/', (string) $this->comment);
$comment[0] = preg_replace('/[ \t]*\\/\*\*/', '', $comment[0]); // strip '/**'
$i = count($comment) - 1;
Expand Down
26 changes: 17 additions & 9 deletions tests/Analysers/ReflectionAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace OpenApi\Tests\Analysers;

use OpenApi\Analysers\AnalyserInterface;
use OpenApi\Analysers\AnnotationFactoryInterface;
use OpenApi\Analysis;
use OpenApi\Annotations\Operation;
Expand Down Expand Up @@ -65,12 +66,21 @@ public function testTraitInheritance()
$this->assertEquals($expected, array_keys($annotationFactory->reflectors));
}

public function testApiDocBlockBasic()
public function analysers()
{
$analyser = new ReflectionAnalyser([new DocBlockAnnotationFactory()]);
return [
'docblocks-attributes' => [new ReflectionAnalyser([new DocBlockAnnotationFactory(), new AttributeAnnotationFactory()])],
'attributes-docblocks' => [new ReflectionAnalyser([new AttributeAnnotationFactory(),new DocBlockAnnotationFactory()])],
];
}

/**
* @dataProvider analysers
*/
public function testApiDocBlockBasic(AnalyserInterface $analyser)
{
$fixture = $this->fixture('Apis/DocBlocks/basic.php');
require_once $fixture;
//require_once $fixture;

$analysis = $analyser->fromFile($fixture, $this->getContext());
$analysis->process((new Generator())->getProcessors());
Expand All @@ -85,12 +95,11 @@ public function testApiDocBlockBasic()
}

/**
* @dataProvider analysers
* @requires PHP 8.1
*/
public function testApiAttributesBasic()
public function testApiAttributesBasic(AnalyserInterface $analyser)
{
$analyser = new ReflectionAnalyser([new AttributeAnnotationFactory()]);

$fixture = $this->fixture('Apis/Attributes/basic.php');
require_once $fixture;

Expand All @@ -107,12 +116,11 @@ public function testApiAttributesBasic()
}

/**
* @dataProvider analysers
* @requires PHP 8.1
*/
public function testApiMixedBasic()
public function testApiMixedBasic(AnalyserInterface $analyser)
{
$analyser = new ReflectionAnalyser([new DocBlockAnnotationFactory(), new AttributeAnnotationFactory()]);

$fixture = $this->fixture('Apis/Mixed/basic.php');
require_once $fixture;

Expand Down

0 comments on commit 9b3b713

Please sign in to comment.