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
Improve code quality subscriber new action #17232
Improve code quality subscriber new action #17232
Conversation
Hi @arnoudhgz. Thank you for your contribution
For more details, please, review the Magento Contributor Assistant documentation |
} else { | ||
$this->messageManager->addSuccess(__('Thank you for your subscription.')); | ||
if ($status == Subscriber::STATUS_NOT_ACTIVE) { | ||
$this->messageManager->addSuccessMessage(__('The confirmation request has been sent.')); |
There was a problem hiding this comment.
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
* @return void | ||
*/ | ||
protected function validateEmailFormat($email) | ||
{ | ||
if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) { | ||
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a valid email address.')); | ||
if (!(new \Zend_Validate)->is($email, EmailAddress::class)) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 :)
} | ||
} catch (\Magento\Framework\Exception\LocalizedException $e) { | ||
$this->messageManager->addException( | ||
$this->messageManager->addSuccessMessage($this->getSuccessMessage( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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. 👍
} | ||
} catch (\Magento\Framework\Exception\LocalizedException $e) { | ||
$this->messageManager->addException( | ||
$this->messageManager->addSuccessMessage($this->getSuccessMessage( |
There was a problem hiding this comment.
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);
* @param int $status | ||
* @return Phrase | ||
*/ | ||
private function getSuccessMessage($status) |
There was a problem hiding this comment.
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?
&& $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED | ||
) { | ||
throw new \Magento\Framework\Exception\LocalizedException( | ||
if ($subscriber->getId() && $subscriber->getSubscriberStatus() == Subscriber::STATUS_SUBSCRIBED) { |
There was a problem hiding this comment.
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?
*/ | ||
private function getSuccessMessage($status) | ||
{ | ||
if ($status == Subscriber::STATUS_NOT_ACTIVE) { |
There was a problem hiding this comment.
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?
@@ -98,23 +105,23 @@ protected function validateGuestSubscription() | |||
* Validates the format of the email address | |||
* | |||
* @param string $email | |||
* @throws \Magento\Framework\Exception\LocalizedException | |||
* @throws LocalizedException | |||
* @throws \Zend_Validate_Exception |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
Let's wait for Travis build results |
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Phrase; | ||
use Magento\Framework\Validator\EmailAddress; |
There was a problem hiding this comment.
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
*/ | ||
public function __construct( | ||
Context $context, | ||
SubscriberFactory $subscriberFactory, | ||
Session $customerSession, | ||
StoreManagerInterface $storeManager, | ||
CustomerUrl $customerUrl, | ||
CustomerAccountManagement $customerAccountManagement | ||
CustomerAccountManagement $customerAccountManagement, | ||
EmailAddress $emailValidator |
There was a problem hiding this comment.
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.
@@ -1,4 +1,4 @@ | |||
<?php | |||
<?php declare(strict_types=1); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :(
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
* @return void | ||
*/ | ||
protected function validateEmailAvailable($email) | ||
protected function validateEmailAvailable(string $email): void |
There was a problem hiding this comment.
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.
Hi @ihor-sviziev, thank you for the review. |
Hi @arnoudhgz. Thank you for your contribution. |
else
block for returning success message to private function@thrown
tag and add missing@thrown
tagif
statement can be written on one lineaddExceptionMessage
andaddSuccessMessage
instead of their deprecated counterpartsContribution checklist