diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index edea3b31c7..c8cba5cba2 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -85,36 +85,11 @@ parameters: count: 1 path: src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php - - - message: "#^Parameter \\#3 \\$subject of function preg_replace expects array\\|string, string\\|null given\\.$#" - count: 4 - path: src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php - - - - message: "#^Parameter \\#1 \\$str of function substr expects string, string\\|null given\\.$#" - count: 3 - path: src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php - - - - message: "#^Parameter \\#1 \\$haystack of function strrpos expects string, string\\|null given\\.$#" - count: 1 - path: src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php - - message: "#^Only booleans are allowed in an if condition, int\\|false given\\.$#" count: 1 path: src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php - - - message: "#^Parameter \\#2 \\$subject of function preg_match expects string, string\\|null given\\.$#" - count: 1 - path: src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php - - - - message: "#^Parameter \\#2 \\$subject of function preg_match_all expects string, string\\|null given\\.$#" - count: 1 - path: src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 1 diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php index 20d9804832..0fbc2b41a8 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php @@ -198,39 +198,75 @@ private function findSymbols(string $file): array return ['classes' => [], 'functions' => []]; } - if (!preg_match('{\b(?:class|interface|trait|function)\s}i', $contents)) { + $found = []; + if (stripos($contents, 'class') !== false) { + $found[] = 'class'; + } + if (stripos($contents, 'interface') !== false) { + $found[] = 'interface'; + } + if (stripos($contents, 'trait') !== false) { + $found[] = 'trait'; + } + if (stripos($contents, 'function') !== false) { + $found[] = 'function'; + } + if ($found === []) { + return ['classes' => [], 'functions' => []]; + } + if (!preg_match('{\b(?:' . \implode('|', $found) . ')\s}i', $contents)) { return ['classes' => [], 'functions' => []]; } // strip heredocs/nowdocs - $contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)(?:\s*)\\2(?=\s+|[;,.)])}s', 'null', $contents); + if (strpos($contents, '<<<') !== false) { + $contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)(?:\s*)\\2(?=\s+|[;,.)])}s', 'null', $contents); + assert(is_string($contents)); + } + // strip strings - $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents); + $contents = preg_replace('{"[^"]*+(?:[^"]*+)*+"|\'[^\']*+(?:[^\']*+)*+\'}', 'null', $contents); + assert(is_string($contents)); + // strip leading non-php code if needed - if (substr($contents, 0, 2) !== ' [], 'functions' => []]; } } + // strip non-php blocks in the file - $contents = preg_replace('{\?>(?:[^<]++|<(?!\?))*+<\?}s', '?>') !== false) { + $contents = preg_replace('{\?>(?:[^<]++|<(?!\?))*+<\?}', '?>'); if ($pos !== false && strpos(substr($contents, $pos), '])(?Pnamespace) (?P\s++[a-z_\x7f-\xff][a-z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-z_\x7f-\xff][a-z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;]'; + } else { + $namespaceRegex = ''; } preg_match_all('{ - (?: - \b(?])(?Pclass|interface|trait|function) \s++ (?P[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+) - | \b(?])(?Pnamespace) (?P\s++[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;] - ) - }ix', $contents, $matches); + (?: + \b(?])(?P' . \implode('|', $found) . ') \s+ (?P[a-z_\x7f-\xff:][a-z0-9_\x7f-\xff:\-]*+) + ' . $namespaceRegex . ' + ) + }ix', $contents, $matches); $classes = []; $functions = [];