From 41c03aa742657473335fa53e2d44adcf3b97a97d Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Wed, 29 Jun 2022 13:40:06 +1200 Subject: [PATCH] Handle '&' used for return by reference (#1274) --- src/Analysers/TokenScanner.php | 4 ++++ tests/Analysers/TokenScannerTest.php | 18 ++++++++++++++++++ tests/Fixtures/PHP/References.php | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 tests/Fixtures/PHP/References.php diff --git a/src/Analysers/TokenScanner.php b/src/Analysers/TokenScanner.php index 8d916e2cf..126eb00a7 100644 --- a/src/Analysers/TokenScanner.php +++ b/src/Analysers/TokenScanner.php @@ -144,6 +144,10 @@ protected function scanTokens(array $tokens): array case T_FUNCTION: $token = $this->nextToken($tokens); + if ((!is_array($token) && '&' == $token) + || (defined('T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG') && T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG == $token[0])) { + $token = $this->nextToken($tokens); + } if (($unitLevel + 1) == count($stack) && $currentName) { $units[$currentName]['methods'][] = $token[1]; diff --git a/tests/Analysers/TokenScannerTest.php b/tests/Analysers/TokenScannerTest.php index 7e5bdfd3b..63004b9af 100644 --- a/tests/Analysers/TokenScannerTest.php +++ b/tests/Analysers/TokenScannerTest.php @@ -77,6 +77,24 @@ public function scanCases() ]; } + yield 'references' => [ + 'PHP/References.php', + [ + 'OpenApi\Tests\Fixtures\PHP\References' => [ + 'uses' => [ + 'OA' => 'OpenApi\Annotations', + ], + 'interfaces' => [], + 'traits' => [], + 'enums' => [], + 'methods' => [ + 'return_ref', + ], + 'properties' => [], + ], + ], + ]; + yield 'php7' => [ 'PHP/php7.php', [], diff --git a/tests/Fixtures/PHP/References.php b/tests/Fixtures/PHP/References.php new file mode 100644 index 000000000..a313da236 --- /dev/null +++ b/tests/Fixtures/PHP/References.php @@ -0,0 +1,25 @@ +