Skip to content

Refactor moveFileFromTmp to not use a static method call #27581

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

Merged

Conversation

dmanners
Copy link
Contributor

@dmanners dmanners commented Apr 4, 2020

Description (*)

Currently the class \Magento\Catalog\Model\ImageUploader has a static call to Uploader::getNewFileName

In this PR I plan to:

  • Create new class for the method getNewFileName,
  • Refactor the \Magento\Catalog\Model\ImageUploader class to use this new class,

Related Pull Requests

Fixed Issues (if relevant)

Manual testing scenarios (*)

  1. Upload an image to a category with a unique name and check that name has not been changed,
  2. Upload an image to a category with a nonunique name and check that the name has been updated to be unique,

Questions or comments

I am not too sure on the naming of the new class but would be happy to update it if needed.
I am also not sure about if I need an optional injection of my new class to the ImageUploader class.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] Refactor moveFileFromTmp to not use a static method call #29598: Refactor moveFileFromTmp to not use a static method call

@m2-assistant
Copy link

m2-assistant bot commented Apr 4, 2020

Hi @dmanners. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

Copy link
Contributor

@rogyar rogyar left a comment

Choose a reason for hiding this comment

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

Hi @dmanners. Thanks a lot for your collaboration. Please, check my comments below regarding some minor adjustments.

@rogyar rogyar added Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Progress: pending review and removed Progress: needs update labels Apr 4, 2020
@dmanners
Copy link
Contributor Author

dmanners commented Apr 4, 2020

Thanks @rogyar I have made the changes required.

I could consider also removing all usages of the old static method as this is used in multiple locations. Though I was thinking about doing this step first then doing the replacments later on.

@rogyar
Copy link
Contributor

rogyar commented Apr 8, 2020

Hi, @dmanners. Thank you for the adjustments.

If you take a look at the failing static tests, you will see that the ImageUploader class has more than 10 constructor parameters. Basically, there were 10 parameters before your change, but after the file change we have this particular test red and most likely it will be a roadblock for merging the PR.

If you have some time, I would suggest some refactoring of the ImageUploader class. The main idea is to decouple some functional parts into separate class/classes. It does not necessarily mean that in that way we will increase the constructor parameters count. We may just split this class into 2 or more and reduce number of direct dependencies in that way.

Please, let me know if you need any help with that.

Thank you!

@dmanners
Copy link
Contributor Author

dmanners commented Apr 8, 2020

Hi @rogyar honestly I cannot see the right place for improvement here without simply removing the logger that is only used for one exception but not the other exception.

If you had any ideas that would be greatly appreciated.

@rogyar
Copy link
Contributor

rogyar commented Apr 12, 2020

Hi @dmanners. We could create a separate class like ImageUploaderConfigProvider and move all logic and properties related to baseTmpPath, basePath, allowedExtensions and allowedMimeTypes there. Then inject this config provider into the constructor. But we need to take care of all existing virtual types for the image uploader.

@dmanners dmanners force-pushed the refactor-moveFileFromTmp branch from c81b343 to 4072bc8 Compare April 13, 2020 11:14
@@ -216,7 +216,7 @@
<argument name="instanceName" xsi:type="string">Magento\Catalog\Model\System\Config\Source\Inputtype</argument>
</arguments>
</virtualType>
<virtualType name="Magento\Catalog\CategoryImageUpload" type="Magento\Catalog\Model\ImageUploader">
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could only find this one usage of the virtual type though I thought there would be more.

/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$this->imageUploader = $this->objectManager->create(
\Magento\Catalog\Model\ImageUploader::class,
$this->configProvider = $this->objectManager->create(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not too sure if this is the desired way to do this or if I can inject the virtual type from the di?

*
* @return string[]
*/
public function getAllowedExtensions()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is 2.4 a breaking change? Am I allowed to remove these? I could not see any usaged in place for them apart from in the class.

@dmanners
Copy link
Contributor Author

Hi @rogyar since we have now added in all the actual classes that this is dependant on we are getting the following:

PHP Code Mess has found error(s):
/var/www/html/app/code/Magento/Catalog/Model/ImageUploader.php:14	The class ImageUploader has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.

Failed asserting that 2 matches expected 0.

What I could suggest is to extract the usage of the store manager and the building of the media url into a ImageUploadURLBuilder class. Though that will only fix this mess warning if I can then remove the storemanager from the constructor. Is that ok with the current release setup or would that break too much compatibility issues?

@rogyar
Copy link
Contributor

rogyar commented Apr 21, 2020

Hi @dmanners. That's one of the trickiest things in the legacy code refactoring. You try to add some minor adjustment but then you realize that you need to rebuild a huge part of the logic. I believe you know how it works :)

I feel really sorry that you had to spend so much time on that but I don't think that we need to spend more tilting at windmills. Let's do the following. I would ask you to revert the last iteration of refactoring and get back to the place where we had 10 constructor arguments. We will try to convince the gatekeepers that it's the case when we should bypass the test.

Once again, sorry for your time. I wish I could predict such cases and sometimes I can but the legacy code is still stronger than me :(

@dmanners
Copy link
Contributor Author

dmanners commented May 5, 2020

Hi @rogyar sorry for the slow update. I actually had some changes that I had not pushed before your original message on reverting some commits. Could you please take a look over the current code and let me know which stuff I would need to put back :D

@rogyar
Copy link
Contributor

rogyar commented May 11, 2020

Hi @dmanners. As far as I see, we need all commits prior to b2e726f57326d0dc6e7a5418996b16e9db8fabc7 (including this one).

Thank you

@engcom-Charlie
Copy link
Contributor

@magento run all tests

@rogyar
Copy link
Contributor

rogyar commented Sep 6, 2020

Hi @engcom-Charlie @engcom-Delta. I'm not sure what is the reason, but I don't see the particular failing Static/Unit tests in the system. Only a pack of skipped tests.

@engcom-Charlie
Copy link
Contributor

@magento run all tests

@engcom-Charlie
Copy link
Contributor

Hi, @sidolov can you please help to understand why the unit tests and static tests are falling? In the report, there are no errors.
Thank you.

@sidolov
Copy link
Contributor

sidolov commented Sep 22, 2020

@engcom-Charlie

PHP Fatal error:  Declaration of Magento\Framework\Test\Unit\File\NameTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /var/www/html/lib/internal/Magento/Framework/Test/Unit/File/NameTest.php on line 33

@engcom-Charlie
Copy link
Contributor

@magento run all tests

@engcom-Charlie
Copy link
Contributor

@magento run all tests

@magento-engcom-team
Copy link
Contributor

Hi @rogyar, thank you for the review.
ENGCOM-8031 has been created to process this Pull Request

@engcom-Delta
Copy link
Contributor

✔️ QA passed
Result is same as here

@engcom-Delta engcom-Delta added the QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope label Sep 28, 2020
@engcom-Delta
Copy link
Contributor

Note: Automation tests are passed

@m2-assistant
Copy link

m2-assistant bot commented Sep 30, 2020

Hi @dmanners, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: test coverage Component: Catalog Component: File Component: Test Event: Global-Contribution-Day Priority: P3 May be fixed according to the position in the backlog. Progress: accept QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope Release Line: 2.4 Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Issue] Refactor moveFileFromTmp to not use a static method call
6 participants