Skip to content

Commit ab820c3

Browse files
authored
Merge pull request #9249 from live627/fns
Further improves getDefinedFunctionsInFile()
2 parents 45e7e71 + 996511e commit ab820c3

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

Sources/Actions/Admin/Maintenance.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,13 +2300,21 @@ protected static function parseIntegrationHook(string $hook, string $rawData): a
23002300
protected static function getDefinedFunctionsInFile(string $file): array
23012301
{
23022302
$source = file_get_contents($file);
2303-
// token_get_all() is too slow so use a nice little regex instead.
2304-
preg_match_all('/\bnamespace\s++((?P>label)(?:\\\(?P>label))*+)\s*+;|\bclass\s++((?P>label))[\w\s]*+{|\bfunction\s++((?P>label))\s*+\(.*?\)[:\|\w\s]*+{(?(DEFINE)(?<label>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*+))/is', $source, $matches, PREG_SET_ORDER);
23052303

2304+
if (!str_contains($source, 'function')) {
2305+
return [];
2306+
}
2307+
2308+
// Remove multiline comments so regex does not
2309+
// match fake functions/classes inside them.
2310+
$source = preg_replace('~//[^\h]+|/\*.*?\*/~s', '', $source);
23062311
$functions = [];
23072312
$namespace = '';
23082313
$class = '';
23092314

2315+
// token_get_all() is too slow so use a nice little regex instead.
2316+
preg_match_all('/\b(?:namespace\s+((?P>label)(?:\\\(?P>label))*+)\s*;|(?:class\s+((?P>label))(?:[\s,]|\\\\|(?P>label))*+|function\s+((?P>label))\s*\([^)]*\)\s*(?::[^{]+)?){)(?(DEFINE)(?<label>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*+))/i', $source, $matches, PREG_SET_ORDER);
2317+
23102318
foreach ($matches as $match) {
23112319
if (!empty($match[1])) {
23122320
$namespace = $match[1] . '\\';

0 commit comments

Comments
 (0)