Skip to content

Improve code quality subscriber new action#17232

Merged
magento-engcom-team merged 1 commit intomagento:2.3-developfrom
arnoudhgz:fix/improve-code-quality-subscriber-new-action
Aug 19, 2018
Merged

Improve code quality subscriber new action#17232
magento-engcom-team merged 1 commit intomagento:2.3-developfrom
arnoudhgz:fix/improve-code-quality-subscriber-new-action

Conversation

@arnoudhgz
Copy link
Copy Markdown
Contributor

@arnoudhgz arnoudhgz commented Jul 28, 2018

  • Refactor else block for returning success message to private function
  • Remove not thrown @thrown tag and add missing @thrown tag
  • Use import statements for classes
  • Because imports are being used an if statement can be written on one line
  • Use addExceptionMessage and addSuccessMessage instead of their deprecated counterparts

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)

@magento-engcom-team magento-engcom-team added Partner: MediaCT Pull Request is created by partner MediaCT partners-contribution Pull Request is created by Magento Partner labels Jul 28, 2018
@magento-engcom-team
Copy link
Copy Markdown
Contributor

Hi @arnoudhgz. 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 {$VERSION} instance - deploy vanilla Magento instance

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this case redirect will not be executed. Currently this change adds regression. It's better to revert it or suggest another solution

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In all the places Magento uses Zend_Validate::is(), but in this case you suggest to replace it with object creation.
https://github.com/magento/magento2/search?p=3&q=Zend_Validate&unscoped_q=Zend_Validate
In general I agree that using static methods not the best practice, but it's better to change it in all places, also not sure if this variant is really optimal.
If you would like to propose replacement to object creation - feel free to create separate PR, but in scope of this PR - please revert this change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback, I only changed it because Codacy mentioned it as a new issue from my commit (because of the import statements).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We're using codaxy just as additional information, not all issues we want to be fixed. As example this one :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ihor-sviziev I refactored it slightly different, using a private function instead of the if/else block

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're doing few thing at once, it's hard to understand what's going on there. Previously is was more simple to understand.

Please add following code as separate line (as it was before)

$status = $this->_subscriberFactory->create()->subscribe($email);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ihor-sviziev I was indeed doubting about this one, the old way is indeed more understandable. 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're doing few thing at once, it's hard to understand what's going on there. Previously is was more simple to understand.

Please add following code as separate line (as it was before)

$status = $this->_subscriberFactory->create()->subscribe($email);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we define parameter and return types there?

Copy link
Copy Markdown
Contributor

@ihor-sviziev ihor-sviziev Aug 8, 2018

Choose a reason for hiding this comment

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

can we use strict comparison there? maybe just convert to expected type before?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use strict comparison there?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this type of exception should not be thrown. Am I wrong?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I got the message from PHPStorm that not all the necessary @throws tags were set, so I added this with the autocomplete in PHPStorm.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AFAIK \Zend_Validate_Exception could be thrown in case if validator wasn't found. So this method actually can't throw such exceptions.
Please remove it.
Everything else looks good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In vendor/magento/zendframework1/library/Zend/Validate.php in the static is method, not all exceptions are being caught. So I should leave the @throws tag or write a try/catch.

Please see: https://github.com/magento/zf1/blob/48ac5e38c591afefb95e0d7127d00fe52d0fbb0f/library/Zend/Validate.php#L195

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ihor-sviziev I found out that Magento actually had an email validator class, I required this one through dependency injection. This saves some reflection :)

@ihor-sviziev
Copy link
Copy Markdown
Contributor

Let's wait for Travis build results

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's better to use alias EmailAddressValidator or EmailValidator there to be clear

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

According to https://devdocs.magento.com/guides/v2.2/contributor-guide/backward-compatible-development/
Please make this parameter optional and get object from ObjectManager as fallback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please revert this change. This changes changes behavior. For now we can declare strict types =1 only for new files

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ihor-sviziev in another PR of mine, the reviewer requested to enable strict types on an existing class: #16571 (comment)

Now I am confused :(

Copy link
Copy Markdown
Contributor

@ihor-sviziev ihor-sviziev Aug 10, 2018

Choose a reason for hiding this comment

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

Ok. Sorry for confusing you. I will discuss it with Maintainers and we'll decide what to do in such cases. For now no need to change strict types declaration and arguments for methods, but need to fix optional parameter in constructor

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After discussion - we decided that we can't use strict types for existing classes as it's not backward compatible change. The same for method arguments and return types in methods that already exists.
#16571 (comment)

Please adjust your code.
Thx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ihor-sviziev thank you for discussing it, at least it is now clear for me :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please revert adding types of arguments and return types for all methods that were existing there. It's signature change, that is not backward compatible

+ Refactor `else` block for returning success message to private function
+ Use import statements for classes
+ Because imports are being used an `if` statement can be written on one line
+ Use `addExceptionMessage` and `addSuccessMessage` instead of their deprecated counterparts
+ Include with DI the `Magento\Framework\Validator\EmailAddress` class
which can be used to validate the email address. Therefore the static
function call to the `is` method on the `Zend_Validate` class is not
needed anymore.
@magento-engcom-team magento-engcom-team added this to the Release: 2.3.0 milestone Aug 14, 2018
@magento-engcom-team
Copy link
Copy Markdown
Contributor

Hi @ihor-sviziev, thank you for the review.
ENGCOM-2788 has been created to process this Pull Request

@magento-engcom-team magento-engcom-team merged commit 6603a24 into magento:2.3-develop Aug 19, 2018
@magento-engcom-team
Copy link
Copy Markdown
Contributor

Hi @arnoudhgz. Thank you for your contribution.
We will aim to release these changes as part of 2.3.0.
Please check the release notes for final confirmation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: Newsletter Partner: MediaCT Pull Request is created by partner MediaCT partners-contribution Pull Request is created by Magento Partner Progress: accept Release Line: 2.3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants