Improve code quality subscriber new action#17232
Improve code quality subscriber new action#17232magento-engcom-team merged 1 commit intomagento:2.3-developfrom arnoudhgz:fix/improve-code-quality-subscriber-new-action
Conversation
|
Hi @arnoudhgz. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
There was a problem hiding this comment.
In this case redirect will not be executed. Currently this change adds regression. It's better to revert it or suggest another solution
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
We're using codaxy just as additional information, not all issues we want to be fixed. As example this one :)
There was a problem hiding this comment.
@ihor-sviziev I refactored it slightly different, using a private function instead of the if/else block
There was a problem hiding this comment.
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);There was a problem hiding this comment.
@ihor-sviziev I was indeed doubting about this one, the old way is indeed more understandable. 👍
There was a problem hiding this comment.
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);There was a problem hiding this comment.
Can we define parameter and return types there?
There was a problem hiding this comment.
can we use strict comparison there? maybe just convert to expected type before?
There was a problem hiding this comment.
Can we use strict comparison there?
There was a problem hiding this comment.
Looks like this type of exception should not be thrown. Am I wrong?
There was a problem hiding this comment.
I got the message from PHPStorm that not all the necessary @throws tags were set, so I added this with the autocomplete in PHPStorm.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@ihor-sviziev I found out that Magento actually had an email validator class, I required this one through dependency injection. This saves some reflection :)
|
Let's wait for Travis build results |
There was a problem hiding this comment.
I think it's better to use alias EmailAddressValidator or EmailValidator there to be clear
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Please revert this change. This changes changes behavior. For now we can declare strict types =1 only for new files
There was a problem hiding this comment.
@ihor-sviziev in another PR of mine, the reviewer requested to enable strict types on an existing class: #16571 (comment)
Now I am confused :(
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@ihor-sviziev thank you for discussing it, at least it is now clear for me :)
There was a problem hiding this comment.
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.
|
Hi @ihor-sviziev, thank you for the review. |
|
Hi @arnoudhgz. Thank you for your contribution. |
elseblock for returning success message to private function@throwntag and add missing@throwntagifstatement can be written on one lineaddExceptionMessageandaddSuccessMessageinstead of their deprecated counterpartsContribution checklist