Skip to content

Commit

Permalink
refactor method organization
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-rolland committed Sep 13, 2024
1 parent 27c5254 commit 9a8c3e6
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/Core/Module/ModuleOverrideChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,22 @@ 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 ($this->hasConflictingMethod($moduleOverrideFile, $existingOverrideFile)) {
if (
$this->hasConflictingMethod($moduleOverrideContent, $existingOverrideContent)
|| $this->hasConflictingProperty($moduleOverrideContent, $existingOverrideContent)
|| $this->hasConflictingConstant($moduleOverrideContent, $existingOverrideContent)
) {
$this->errors[] = $this->translator->trans(
'The override file %1$s conflicts with an existing override in %2$s.',
[$moduleOverrideFile, $existingOverrideFile],
Expand All @@ -98,23 +107,8 @@ public function getErrors(): array
return $this->errors;
}

/*
* Checks if a module override class has a method that is already overridden by another module
*/
private function hasConflictingMethod(string $moduleOverridePath, string $existingOverridePath): bool
private function hasConflictingProperty(string $moduleOverrideContent, string $existingOverrideContent): bool
{
$moduleOverrideContent = file_get_contents($moduleOverridePath);
$existingOverrideContent = file_get_contents($existingOverridePath);

$moduleMethods = $this->getClassMethodsFromContent($moduleOverrideContent);
$existingOverrideMethods = $this->getClassMethodsFromContent($existingOverrideContent);

foreach ($moduleMethods as $method) {
if (in_array($method, $existingOverrideMethods)) {
return true;
}
}

$moduleOverrideProperties = $this->getClassPropertiesFromContent($moduleOverrideContent);
$existingOverrideProperties = $this->getClassPropertiesFromContent($existingOverrideContent);

Expand All @@ -124,6 +118,11 @@ private function hasConflictingMethod(string $moduleOverridePath, string $existi
}
}

return false;
}

private function hasConflictingConstant(string $moduleOverrideContent, string $existingOverrideContent): bool
{
$moduleOverrideConstants = $this->getClassConstantsFromContent($moduleOverrideContent);
$existingOverrideConstants = $this->getClassConstantsFromContent($existingOverrideContent);

Expand All @@ -136,6 +135,23 @@ private function hasConflictingMethod(string $moduleOverridePath, string $existi
return false;
}

/*
* Checks if a module override class has a method that is already overridden by another module
*/
private function hasConflictingMethod(string $moduleOverrideContent, string $existingOverrideContent): bool
{
$moduleMethods = $this->getClassMethodsFromContent($moduleOverrideContent);
$existingOverrideMethods = $this->getClassMethodsFromContent($existingOverrideContent);

foreach ($moduleMethods as $method) {
if (in_array($method, $existingOverrideMethods)) {
return true;
}
}

return false;
}

/*
* This function parses php file content and gets a list of methods from its content.
*/
Expand Down

0 comments on commit 9a8c3e6

Please sign in to comment.