From 887d79e9af68b05b5f09377fa16425a8477eb636 Mon Sep 17 00:00:00 2001 From: Dimitri BOUTEILLE Date: Thu, 25 Apr 2024 23:50:20 +0200 Subject: [PATCH 1/2] #15 Fix parser with duplicate class label in file content --- src/Helpers/Parser.php | 24 ++++++++++++++++++--- tests/Helpers/ParserTest.php | 7 +++++- tests/fixtures/route-classes/source-4.php | 26 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/route-classes/source-4.php diff --git a/src/Helpers/Parser.php b/src/Helpers/Parser.php index e4528dd..7d5c847 100644 --- a/src/Helpers/Parser.php +++ b/src/Helpers/Parser.php @@ -25,10 +25,28 @@ public static function findClassName(?string $fileContent): ?string throw new \InvalidArgumentException('The content does not contain PHP code.'); } - if (preg_match('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm', $fileContent, $m)) { - return $m[1].'\\'.$m[2]; + $class = null; + $i = 0; + for (;$i < count($tokens);$i++) { + if ($tokens[$i][0] === T_CLASS) { + for ($j = $i + 1;$j < count($tokens);$j++) { + if ($tokens[$j] === '{') { + $class = $tokens[$i + 2][1]; + } + } + } } - return null; + if ($class === null || $class === '') { + return null; + } + + $namespace = null; + if (preg_match('#(^|\s)namespace(.*?)\s*;#sm', $fileContent, $m)) { + $namespace = $m[2] ?? null; + $namespace = $namespace !== null ? trim($namespace) : null; + } + + return $namespace ? $namespace . "\\" . $class : $class; } } diff --git a/tests/Helpers/ParserTest.php b/tests/Helpers/ParserTest.php index 423c46a..0988fe1 100644 --- a/tests/Helpers/ParserTest.php +++ b/tests/Helpers/ParserTest.php @@ -43,7 +43,7 @@ public static function providerFindClassName(): \Generator return $fileContent; }; - yield 'Light php file' => [ + yield 'Light php file' => [ $load('source-1.php'), 'App\Routes\MyRoute', ]; @@ -57,6 +57,11 @@ public static function providerFindClassName(): \Generator $load('source-3.php'), 'App\Routes\MyRoute', ]; + + yield 'With class string in file content' => [ + $load('source-4.php'), + 'App\Routes\MyRoute', + ]; } /** diff --git a/tests/fixtures/route-classes/source-4.php b/tests/fixtures/route-classes/source-4.php new file mode 100644 index 0000000..6f95d88 --- /dev/null +++ b/tests/fixtures/route-classes/source-4.php @@ -0,0 +1,26 @@ + + */ + +namespace App\Routes; + +use Dbout\WpRestApi\Attributes\Action; +use Dbout\WpRestApi\Attributes\Route; +use Dbout\WpRestApi\Enums\Method; + +#[Route( + namespace: 'app/v2', + route: 'document/(?P\d+)' +)] +class MyRoute +{ + #[Action(Method::GET)] + public function get(): \WP_REST_Response + { + throw new \Exception('Invalid builder class type.'); + } +} From 26e6363fb62df3baace375a55f58596d24080d95 Mon Sep 17 00:00:00 2001 From: Dimitri BOUTEILLE Date: Thu, 25 Apr 2024 23:55:14 +0200 Subject: [PATCH 2/2] #15 CS --- src/Helpers/Parser.php | 5 +++-- tests/Helpers/ParserTest.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Helpers/Parser.php b/src/Helpers/Parser.php index 7d5c847..23f7bc6 100644 --- a/src/Helpers/Parser.php +++ b/src/Helpers/Parser.php @@ -27,9 +27,10 @@ public static function findClassName(?string $fileContent): ?string $class = null; $i = 0; - for (;$i < count($tokens);$i++) { + $counter = count($tokens); + for (;$i < $counter;$i++) { if ($tokens[$i][0] === T_CLASS) { - for ($j = $i + 1;$j < count($tokens);$j++) { + for ($j = $i + 1;$j < $counter;$j++) { if ($tokens[$j] === '{') { $class = $tokens[$i + 2][1]; } diff --git a/tests/Helpers/ParserTest.php b/tests/Helpers/ParserTest.php index 0988fe1..833c374 100644 --- a/tests/Helpers/ParserTest.php +++ b/tests/Helpers/ParserTest.php @@ -58,7 +58,7 @@ public static function providerFindClassName(): \Generator 'App\Routes\MyRoute', ]; - yield 'With class string in file content' => [ + yield 'With phpdoc intro & multiple class/namespace label' => [ $load('source-4.php'), 'App\Routes\MyRoute', ];