From 0646af98e57b17d4368280ee20a258b3074a4f09 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 30 Jun 2023 15:56:37 +0200 Subject: [PATCH] Fix type errors --- composer.json | 2 +- src/ClassMapGenerator.php | 6 +++--- src/PhpFileParser.php | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 0e8fdda..59aa759 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "require": { "php": "^7.2 || ^8.0", "symfony/finder": "^4.4 || ^5.3 || ^6 || ^7", - "composer/pcre": "^2 || ^3" + "composer/pcre": "^2.1 || ^3.1" }, "require-dev": { "symfony/phpunit-bridge": "^5", diff --git a/src/ClassMapGenerator.php b/src/ClassMapGenerator.php index f347960..4fc4e0e 100644 --- a/src/ClassMapGenerator.php +++ b/src/ClassMapGenerator.php @@ -174,7 +174,7 @@ public function scanPaths($path, string $excluded = null, string $autoloadType = } $classes = PhpFileParser::findClasses($filePath); - if ('classmap' !== $autoloadType && isset($namespace, $basePath)) { + if ('classmap' !== $autoloadType && isset($namespace)) { $classes = $this->filterByNamespace($classes, $filePath, $namespace, $autoloadType, $basePath); // if no valid class was found in the file then we do not mark it as scanned as it might still be matched by another rule later @@ -291,7 +291,7 @@ private static function normalizePath(string $path) } // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive: - if (Preg::isMatch('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) { + if (Preg::isMatchStrictGroups('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) { $prefix = $match[1]; $path = substr($path, \strlen($prefix)); } @@ -313,7 +313,7 @@ private static function normalizePath(string $path) } // ensure c: is normalized to C: - $prefix = Preg::replaceCallback('{(^|://)[a-z]:$}i', function (array $m) { return strtoupper($m[0]); }, $prefix); + $prefix = Preg::replaceCallbackStrictGroups('{(?:^|://)[a-z]:$}i', function (array $m) { return strtoupper($m[0]); }, $prefix); return $prefix.$absolute.implode('/', $parts); } diff --git a/src/PhpFileParser.php b/src/PhpFileParser.php index fa9b4e3..b678aee 100644 --- a/src/PhpFileParser.php +++ b/src/PhpFileParser.php @@ -52,8 +52,8 @@ public static function findClasses(string $path): array } // return early if there is no chance of matching anything in this file - Preg::matchAll('{\b(?:class|interface|trait'.$extraTypes.')\s}i', $contents, $matches); - if (!$matches) { + Preg::matchAllStrictGroups('{\b(?:class|interface|trait'.$extraTypes.')\s}i', $contents, $matches); + if (0 === \count($matches)) { return array(); } @@ -76,6 +76,7 @@ public static function findClasses(string $path): array $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', (string) $matches['nsname'][$i]) . '\\'; } else { $name = $matches['name'][$i]; + assert(is_string($name)); // skip anon classes extending/implementing if ($name === 'extends' || $name === 'implements') { continue; @@ -83,7 +84,7 @@ public static function findClasses(string $path): array if ($name[0] === ':') { // This is an XHP class, https://github.com/facebook/xhp $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1); - } elseif (strtolower($matches['type'][$i]) === 'enum') { + } elseif (strtolower((string) $matches['type'][$i]) === 'enum') { // something like: // enum Foo: int { HERP = '123'; } // The regex above captures the colon, which isn't part of