Skip to content

Commit

Permalink
Handle abstract functions (#1369)
Browse files Browse the repository at this point in the history
Keep track of abstract functions and handle them the same as in interfaces
  • Loading branch information
DerManoMann authored Dec 21, 2022
1 parent b8f7731 commit e505bce
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/Analysers/TokenScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected function scanTokens(array $tokens): array
$units = [];
$uses = [];
$isInterface = false;
$isAbstractFunction = false;
$namespace = '';
$currentName = null;
$unitLevel = 0;
Expand Down Expand Up @@ -65,6 +66,12 @@ protected function scanTokens(array $tokens): array
}

switch ($token[0]) {
case T_ABSTRACT:
if (count($stack)) {
$isAbstractFunction = true;
}
break;

case T_CURLY_OPEN:
case T_DOLLAR_OPEN_CURLY_BRACES:
$stack[] = $token[1];
Expand Down Expand Up @@ -151,7 +158,7 @@ protected function scanTokens(array $tokens): array

if (($unitLevel + 1) == count($stack) && $currentName) {
$units[$currentName]['methods'][] = $token[1];
if (!$isInterface) {
if (!$isInterface && !$isAbstractFunction) {
// more nesting
$units[$currentName]['properties'] = array_merge(
$units[$currentName]['properties'],
Expand All @@ -161,6 +168,7 @@ protected function scanTokens(array $tokens): array
} else {
// no function body
$this->skipTo($tokens, ';');
$isAbstractFunction = false;
}
}
break;
Expand Down
25 changes: 23 additions & 2 deletions tests/Analysers/TokenScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ class TokenScannerTest extends OpenApiTestCase
{
public function scanCases(): iterable
{
if (\PHP_VERSION_ID >= 80100) {
yield 'abstract' => [
'PHP/AbstractKeyword.php',
[
'OpenApi\Tests\Fixtures\PHP\AbstractKeyword' => [
'uses' => [
'Property' => 'OpenApi\Attributes\Property',
],
'interfaces' => [],
'traits' => [],
'enums' => [],
'methods' => ['stuff', 'other', 'another'],
'properties' => [],
],
],
];
}
if (\PHP_VERSION_ID >= 80100) {
yield 'basic' => [
'Apis/DocBlocks/basic.php',
Expand Down Expand Up @@ -224,7 +241,9 @@ public function scanCases(): iterable
'PHP/Php8NamedArguments.php',
[
'OpenApi\\Tests\\Fixtures\\PHP\\Php8NamedArguments' => [
'uses' => [],
'uses' => [
'OA' => 'OpenApi\Annotations',
],
'interfaces' => [],
'traits' => [],
'enums' => [],
Expand All @@ -238,7 +257,9 @@ public function scanCases(): iterable
'PHP/AnonymousFunctions.php',
[
'OpenApi\\Tests\\Fixtures\\PHP\\AnonymousFunctions' => [
'uses' => [],
'uses' => [
'OA' => 'OpenApi\Annotations',
],
'interfaces' => [],
'traits' => [],
'enums' => [],
Expand Down
25 changes: 25 additions & 0 deletions tests/Fixtures/PHP/AbstractKeyword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\PHP;

use OpenApi\Attributes\Property;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
abstract class AbstractKeyword
{
#[Property(property: 'stuff')]
abstract public function stuff(string $name, array $numbers): bool;

#[Property(property: 'other')]
public function other(): string
{
return 'other';
}

#[Property(property: 'another')]
abstract public function another(): void;
}
4 changes: 3 additions & 1 deletion tests/Fixtures/PHP/AnonymousFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace OpenApi\Tests\Fixtures\PHP;

use OpenApi\Annotations as OA;

/**
* @OA\Info(title="Foobar", version="1.0")
*/
Expand Down Expand Up @@ -61,7 +63,7 @@ public function dollarCurly1(string $key = 'xx')
{
preg_replace("/:{$key}/", 'y', 'abx');

$this->shortFn();
return $this->shortFn();
}

public function dollarCurly2(string $key = 'xx')
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/PHP/Php8AttrMix.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace OpenApi\Tests\Fixtures\PHP;

use OpenApi\Annotations as OA;

/**
* @OA\Schema
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/PHP/Php8NamedArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace OpenApi\Tests\Fixtures\PHP;

use OpenApi\Annotations as OA;

/**
* @OA\Schema
*/
Expand Down

0 comments on commit e505bce

Please sign in to comment.