From 845ec161775c29526d910bcad093da1890cb8439 Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 9 Jan 2023 16:40:56 +0300 Subject: [PATCH] fix php 8 findClass (#12) php 8 compatibility --- Loader/AnnotationFileLoader.php | 56 ++++++++++++++++++++++++--------- composer.json | 4 +-- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Loader/AnnotationFileLoader.php b/Loader/AnnotationFileLoader.php index 1ceba2c..11699f0 100644 --- a/Loader/AnnotationFileLoader.php +++ b/Loader/AnnotationFileLoader.php @@ -71,34 +71,60 @@ protected function findClass(string $file) $class = false; $namespace = false; $tokens = token_get_all(file_get_contents($file)); - for ($i = 0, $count = count($tokens); $i < $count; ++$i) { + + if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) { + throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the " true, \T_STRING => true]; + if (\defined('T_NAME_QUALIFIED')) { + $nsTokens[\T_NAME_QUALIFIED] = true; + } + + for ($i = 0; isset($tokens[$i]); ++$i) { $token = $tokens[$i]; - if (!is_array($token)) { + if (!isset($token[1])) { continue; } - if (true === $class && T_STRING === $token[0]) { + if (true === $class && \T_STRING === $token[0]) { return $namespace . '\\' . $token[1]; } - if (true === $namespace && T_STRING === $token[0]) { - $namespace = ''; - do { - $namespace .= $token[1]; - $token = $tokens[++$i]; - } while ($i < $count && is_array($token) && in_array($token[0], [T_NS_SEPARATOR, T_STRING])); + if (true === $namespace && isset($nsTokens[$token[0]])) { + $namespace = $token[1]; + while (isset($tokens[++$i][1], $nsTokens[$tokens[$i][0]])) { + $namespace .= $tokens[$i][1]; + } + $token = $tokens[$i]; } - if (T_CLASS === $token[0]) { - // Entity::class bug - if ($i > 0 && T_PAAMAYIM_NEKUDOTAYIM === $tokens[$i - 1][0]) { - continue; + if (\T_CLASS === $token[0]) { + // Skip usage of ::class constant and anonymous classes + $skipClassToken = false; + for ($j = $i - 1; $j > 0; --$j) { + if (!isset($tokens[$j][1])) { + if ('(' === $tokens[$j] || ',' === $tokens[$j]) { + $skipClassToken = true; + } + break; + } + + if (\T_DOUBLE_COLON === $tokens[$j][0] || \T_NEW === $tokens[$j][0]) { + $skipClassToken = true; + break; + } elseif (!\in_array($tokens[$j][0], [\T_WHITESPACE, \T_DOC_COMMENT, \T_COMMENT])) { + break; + } + } + + if (!$skipClassToken) { + $class = true; } - $class = true; } - if (T_NAMESPACE === $token[0]) { + if (\T_NAMESPACE === $token[0]) { $namespace = true; } } diff --git a/composer.json b/composer.json index 3603936..c8e295e 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,11 @@ ], "require": { "doctrine/annotations": "^1.13", - "php": "^7.4", + "php": "^7.4 || ^8.0", "psr/cache": "~1.0", "symfony/framework-bundle": "^4.4|^5.0", "symfony/validator": "^4.4|^5.0", - "twig/twig": "^2.14" + "twig/twig": "^2.14 | ^3.0" }, "autoload": { "psr-4": {