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

Adding a validation before adding or executing layout generator class. #22324

Merged
merged 2 commits into from
Apr 26, 2019
Merged

Adding a validation before adding or executing layout generator class. #22324

merged 2 commits into from
Apr 26, 2019

Conversation

tiagosampaio
Copy link
Member

@tiagosampaio tiagosampaio commented Apr 13, 2019

Before adding or executing any layout generator it's important to validate the class before.

Fixed Issues (if relevant)

It fixes no issues.

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 on Travis CI are green)

@m2-assistant
Copy link

m2-assistant bot commented Apr 13, 2019

Hi @tiagosampaio. 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-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@magento-engcom-team
Copy link
Contributor

Hi @dmytro-ch, thank you for the review.
ENGCOM-4750 has been created to process this Pull Request

@@ -77,6 +77,10 @@ public function process(Reader\Context $readerContext, Generator\Context $genera
{
$this->buildStructure($readerContext->getScheduledStructure(), $generatorContext->getStructure());
foreach ($this->generators as $generator) {
if (!$this->validateGenerator($generator)) {
continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed Issues (if relevant)
It fixes no issues.

What kind of issue it fixes then? What happened before if corresponding interface is not implemented?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @orlangur.

If any 3rd party module overrides or include a generator in GeneratorPool and doesn't declare the methods getType() and process() the user will be given an exception. That's why I think it's a good idea to force the implementation of GeneratorInterface.

Examples:

Call to undefined method Any\Class::getType()
Call to undefined method Magento\Framework\View\Page\Config\Generator\Head::process()

Copy link
Contributor

Choose a reason for hiding this comment

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

@tiagosampaio so user already receives an exception and by this change you actually hide it. What should really be done is throwing exception in case of incorrect interface and only in addGenerators method.

Copy link
Member Author

Choose a reason for hiding this comment

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

@orlangur you're right. I'll do it.

*/
public function process(Reader\Context $readerContext, Generator\Context $generatorContext)
{
$this->buildStructure($readerContext->getScheduledStructure(), $generatorContext->getStructure());
foreach ($this->generators as $generator) {
$this->validateGenerator($generator);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please validate in addGenerators only and get rid of this method.

Copy link
Member Author

@tiagosampaio tiagosampaio Apr 18, 2019

Choose a reason for hiding this comment

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

@orlangur I believe this validation should be there too since the argument $generators can be injected via DI as well as using addGenerator method. Please let me know what you think about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Forget what I've said. It really doesn't make sense and I just made the commit.

private function validateGenerator($generator)
{
if (!($generator instanceof GeneratorInterface)) {
throw new LocalizedException(__('Generator classes must be instances of %1', GeneratorInterface::class));
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really use LocalizedException in such cases? Please grep for some similar check, I believe it should be InvalidArgumentException and no need to translate.

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

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

@tiagosampaio and now please inline the method as we need this check only once 😉

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

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

After changes are applied and all builds are green, please squash them into a single commit so that we have perfectly clean history 😉

@tiagosampaio
Copy link
Member Author

@orlangur it's done.

* @return void
*/
protected function addGenerators(array $generators)
{
foreach ($generators as $generator) {
if (!($generator instanceof GeneratorInterface)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please get rid of unnecessary parentheses.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@magento-engcom-team
Copy link
Contributor

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

@VasylShvorak
Copy link
Contributor

Hi @tiagosampaio during testing PR changes I got fatal error instead of exception error.
#22324WithFix
But I can get exception error if I add message to InvalidArgumentException as string:
throw new \InvalidArgumentException( 'Generator class must be an instance of '. GeneratorInterface::class );
#22324WithEditedCode
Could you take a look?

* @return void
*/
protected function addGenerators(array $generators)
{
foreach ($generators as $generator) {
if (!$generator instanceof GeneratorInterface) {
throw new \InvalidArgumentException(
'Generator class must be an instance of %1', GeneratorInterface::class
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, better use concatenation here (looks like you intended to use sprintf).

Commit amend + force push, please.

Copy link
Member Author

Choose a reason for hiding this comment

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

@orlangur I'm just using sprintf() now.

@magento-engcom-team
Copy link
Contributor

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

@VasylShvorak
Copy link
Contributor

✔️ QA passed

@magento-engcom-team magento-engcom-team merged commit a27c3e0 into magento:2.3-develop Apr 26, 2019
@m2-assistant
Copy link

m2-assistant bot commented Apr 26, 2019

Hi @tiagosampaio, 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants