Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Codebase to Use Symfony Filesystem Instead of Native PHP Functions #1109

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

M0rgan01
Copy link
Contributor

@M0rgan01 M0rgan01 commented Jan 9, 2025

Questions Answers
Description? see beelow
Type? refactor
BC breaks? no
Deprecations? no
Fixed ticket? -
Sponsor company -
How to test? -

This pull request refactors the codebase to replace the usage of native PHP filesystem functions (e.g., file_exists, unlink, mkdir, etc.) with the Symfony Filesystem component. This change improves code readability, consistency, and reliability by leveraging Symfony's robust and well-tested API for filesystem operations.

Key changes include:

  • Replacing direct calls to native filesystem functions with equivalent methods from the Symfony Filesystem component.
  • Injecting the Symfony Filesystem service where applicable to ensure better testability and adherence to dependency injection principles.
  • Updating existing tests to account for the refactored implementation.

This refactor aligns with best practices and enhances compatibility with modern Symfony projects.

@M0rgan01 M0rgan01 force-pushed the Filesystem-refacto branch 7 times, most recently from d9b2502 to 0c9d13d Compare January 9, 2025 17:07
@M0rgan01 M0rgan01 added this to the 7.0.0 milestone Jan 9, 2025
@M0rgan01 M0rgan01 force-pushed the Filesystem-refacto branch 2 times, most recently from 210af31 to 52308ff Compare January 10, 2025 09:08
@M0rgan01 M0rgan01 added the Blocked Status: The issue is blocked by another task label Jan 10, 2025
@M0rgan01 M0rgan01 changed the title [WIP] Symfony FileSystem refacto Refactor Codebase to Use Symfony Filesystem Instead of Native PHP Functions Jan 10, 2025
@M0rgan01 M0rgan01 marked this pull request as ready for review January 10, 2025 09:17
@M0rgan01 M0rgan01 closed this Jan 10, 2025
@M0rgan01 M0rgan01 reopened this Jan 10, 2025
@PrestaShop PrestaShop deleted a comment from sonarqubecloud bot Jan 10, 2025
@M0rgan01 M0rgan01 removed the Blocked Status: The issue is blocked by another task label Jan 10, 2025
Comment on lines 67 to 73

if ($filesystem->exists($filePath)) {
if ($this->container->getUpdateConfiguration()->isChannelOnline()) {
try {
$filesystem->remove($filePath);
$this->logger->debug($this->translator->trans('%s removed', [$filePath]));
} catch (Exception $e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but I think you can use IOExceptionInterface from symfony filesystem.

Comment on lines 82 to 87
try {
$filesystem->remove($latestPath);
$this->logger->debug($this->translator->trans('%s removed', [$latestPath]));
} catch (Exception $e) {
$this->logger->debug($this->translator->trans('Please remove %s by FTP', [$latestPath]));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do the same as other try catch what do you think about make a method for it ?

$this->logger->debug($this->translator->trans('Please remove %s by FTP', [$filePath]));
}
} else {
$this->logger->debug($this->translator->trans('Please remove %s by FTP', [$filePath]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we log remove by ftp if we are not on channelOnline ? I'm not sure but I think something is rong her on the refacto.

$this->next = TaskName::TASK_ERROR;
$this->logger->error($this->translator->trans('Error while creating directory %s.', [$dest]));
$this->logger->error($this->translator->trans('Error while creating directory %s: %s.', [$dest, $e->getMessage()]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but I think something is wrong with the translation %s: %s

$this->next = TaskName::TASK_ERROR;
$this->logger->error($this->translator->trans('Error while copying file %s', [$file]));
$this->logger->error($this->translator->trans('Error while copying file %s: %s', [$file, $e->getMessage()]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for translation her

@M0rgan01 M0rgan01 force-pushed the Filesystem-refacto branch 3 times, most recently from 8c6447e to 5af8958 Compare January 10, 2025 14:44
@ga-devfront
Copy link
Contributor

image

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
4.5% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Comment on lines +68 to +73
if (!$this->filesystem->exists($path)) {
try {
$this->filesystem->mkdir($path);
} catch (IOException $e) {
throw new IOException($this->translator->trans('Unable to create directory %s: %s', [$path, $e->getMessage()]));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check anymore if the directory already exists. If it does, Filesystem will ignore the creation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Reopened
Development

Successfully merging this pull request may close these issues.

3 participants