Skip to content

Commit

Permalink
replace token detection with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-rolland committed Sep 13, 2024
1 parent bc54297 commit 55281c5
Showing 1 changed file with 19 additions and 74 deletions.
93 changes: 19 additions & 74 deletions src/Core/Module/ModuleOverrideChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 55281c5

Please sign in to comment.