From 55281c56f45018818463e7a8b8c9d32d9064d63a Mon Sep 17 00:00:00 2001 From: Matthieu Rolland Date: Fri, 13 Sep 2024 15:28:32 +0200 Subject: [PATCH] replace token detection with regex --- src/Core/Module/ModuleOverrideChecker.php | 93 +++++------------------ 1 file changed, 19 insertions(+), 74 deletions(-) diff --git a/src/Core/Module/ModuleOverrideChecker.php b/src/Core/Module/ModuleOverrideChecker.php index ac7f735c96ce9..992b39c66939b 100644 --- a/src/Core/Module/ModuleOverrideChecker.php +++ b/src/Core/Module/ModuleOverrideChecker.php @@ -74,17 +74,14 @@ public function hasOverrideConflict(string $moduleOverridePath): bool $fileList[] = $file->getRelativePathname(); } - - // module has overrides, let's check override files one by one foreach ($fileList as $file) { $moduleOverrideFile = $moduleOverridePath . DIRECTORY_SEPARATOR . $file; $existingOverrideFile = $this->psOverrideDir . $file; - $moduleOverrideContent = file_get_contents($moduleOverrideFile); - $existingOverrideContent = file_get_contents($existingOverrideFile); - - if (file_exists($existingOverrideFile)) { + if (file_exists($existingOverrideFile) && file_exists($moduleOverrideFile)) { + $moduleOverrideContent = file_get_contents($moduleOverrideFile); + $existingOverrideContent = file_get_contents($existingOverrideFile); if ( $this->hasConflictingMethod($moduleOverrideContent, $existingOverrideContent) || $this->hasConflictingProperty($moduleOverrideContent, $existingOverrideContent) @@ -157,95 +154,43 @@ private function hasConflictingMethod(string $moduleOverrideContent, string $exi */ private function getClassMethodsFromContent(string $content): array { - // Get the list of tokens, see https://www.php.net/manual/en/function.token-get-all.php - $tokens = token_get_all($content); + $methodPattern = '/(public|private|protected)\s+function\s+(\w+)/'; + $methods = []; - foreach ($tokens as $index => $token) { - $methodName = null; - // filter method definitions - if (is_array($token) && $token[0] === T_FUNCTION) { - // This loop is necessary in case some additional spaces exist before the function name - for ($i = $index + 1; $i < count($tokens); ++$i) { - if (is_array($tokens[$i]) && $tokens[$i][0] === T_STRING) { - $methodName = $tokens[$i][1]; - break; - } - } - // Collect method name - if ($methodName) { - $methods[] = $methodName; // Save method name - } - } + if (preg_match_all($methodPattern, $content, $matches)) { + $methods = $matches[2]; } return $methods; } /* - * This function parses php file content and gets a list of properties from its content. - */ + * This function parses php file content and gets a list of properties from its content. + */ private function getClassPropertiesFromContent(string $content): array { - // Get the list of tokens, see https://www.php.net/manual/en/function.token-get-all.php - $tokens = token_get_all($content); + $propertyPattern = '/(public|private|protected)\s+\$(\w+)/'; $properties = []; - $inClass = false; - $inFunction = false; - - foreach ($tokens as $index => $token) { - // we only want variables that are inside a class AND outside a function - if (is_array($token) && $token[0] === T_CLASS) { - $inClass = true; - $inFunction = false; - } - - // check that we are in the beginning of a function - if (is_array($token) && $token[0] === T_FUNCTION) { - $inFunction = true; - } - - // check that this is the end of a function - if ($token === '}') { - $inFunction = false; - } - // Property name is gathered if we are inside a class and outside a function - if ($inClass && !$inFunction && is_array($token) && $token[0] === T_VARIABLE) { - $propertyName = $token[1]; // Token contains the variable name as the second element - $properties[] = $propertyName; // Add to the properties array - } + if (preg_match_all($propertyPattern, $content, $matches)) { + $properties = $matches[2]; } - return $properties; // Return the list of property names + return $properties; } /* - * This function parses php file content and gets a list of constants from its content. - */ + * This function parses php file content and gets a list of properties from its content. + */ private function getClassConstantsFromContent(string $content): array { - // Get the list of tokens, see https://www.php.net/manual/en/function.token-get-all.php - $tokens = token_get_all($content); - $constants = []; - $inClass = false; + $constantPattern = '/const\s+(\w+)/'; - foreach ($tokens as $index => $token) { - // check that we are at the beginning of a class - if (is_array($token) && $token[0] === T_CLASS) { - $inClass = true; - } + $constants = []; - // Check for constant declarations inside the class - if ($inClass && is_array($token) && $token[0] === T_CONST) { - // Loop ahead to find the constant name - for ($i = $index + 1; $i < count($tokens); ++$i) { - if (is_array($tokens[$i]) && $tokens[$i][0] === T_STRING) { - $constants[] = $tokens[$i][1]; // Save constant name - break; - } - } - } + if (preg_match_all($constantPattern, $content, $matches)) { + $constants = $matches[1]; } return $constants;