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

Merged
merged 2 commits into from
Jan 29, 2025

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? the refactoring of this PR affects all file-related operations, such as directory creation, copying or file deletion. It is therefore necessary to ensure that the operations on the files during the various processes have not had any degradation.

Description

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 ?

classes/Task/Update/UpdateComplete.php Show resolved Hide resolved
$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

Copy link
Member

Choose a reason for hiding this comment

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

It looks fine to me, each value of the array will replace an occurrence of %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

Comment on lines 68 to 72
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.

@M0rgan01 M0rgan01 force-pushed the Filesystem-refacto branch 2 times, most recently from 4486a1c to dc6d365 Compare January 13, 2025 09:25
Quetzacoalt91
Quetzacoalt91 previously approved these changes Jan 13, 2025
@M0rgan01 M0rgan01 force-pushed the Filesystem-refacto branch 13 times, most recently from b92d741 to db4db76 Compare January 24, 2025 09:19
@M0rgan01 M0rgan01 marked this pull request as draft January 24, 2025 09:20
@M0rgan01 M0rgan01 removed the wip label Jan 24, 2025
@M0rgan01 M0rgan01 marked this pull request as ready for review January 24, 2025 10:54
@M0rgan01 M0rgan01 closed this Jan 24, 2025
@M0rgan01 M0rgan01 reopened this Jan 24, 2025
Copy link

Quality Gate Failed Quality Gate failed

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

See analysis details on SonarQube Cloud

@paulnoelcholot paulnoelcholot self-assigned this Jan 27, 2025
@paulnoelcholot
Copy link

Blocked by : https://forge.prestashop.com/browse/SUE-308

Copy link
Contributor

@AureRita AureRita left a comment

Choose a reason for hiding this comment

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

Hi @M0rgan01

Thank you for your PR, I tested it and it seems to works as you can see :

1109_1788_820.mp4

I didn't see any op cache issue on this PR, thank you for this coreection

Tested from :
1.7.8.8 to 8.2
8.0.4 to 8.2
8.2 to 9.0.0
8.0.4 to 9.0.0

Because the PR seems to works as expected, It's QA ✔️

Thank you

@M0rgan01 M0rgan01 merged commit 967666f into PrestaShop:dev Jan 29, 2025
42 of 61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

6 participants