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

Improve code quality subscriber new action #17232

Merged
merged 1 commit into from Aug 19, 2018
Merged

Improve code quality subscriber new action #17232

merged 1 commit into from Aug 19, 2018

Conversation

arnoudhgz
Copy link
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
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

} else {
$this->messageManager->addSuccess(__('Thank you for your subscription.'));
if ($status == Subscriber::STATUS_NOT_ACTIVE) {
$this->messageManager->addSuccessMessage(__('The confirmation request has been sent.'));
Copy link
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

* @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)) {
Copy link
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
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
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 :)

}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addException(
$this->messageManager->addSuccessMessage($this->getSuccessMessage(
Copy link
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
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
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. 👍

}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addException(
$this->messageManager->addSuccessMessage($this->getSuccessMessage(
Copy link
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);

* @param int $status
* @return Phrase
*/
private function getSuccessMessage($status)
Copy link
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?

&& $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
) {
throw new \Magento\Framework\Exception\LocalizedException(
if ($subscriber->getId() && $subscriber->getSubscriberStatus() == Subscriber::STATUS_SUBSCRIBED) {
Copy link
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? maybe just convert to expected type before?

*/
private function getSuccessMessage($status)
{
if ($status == Subscriber::STATUS_NOT_ACTIVE) {
Copy link
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?

@@ -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
Copy link
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
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
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
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
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
Contributor

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;
Copy link
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

*/
public function __construct(
Context $context,
SubscriberFactory $subscriberFactory,
Session $customerSession,
StoreManagerInterface $storeManager,
CustomerUrl $customerUrl,
CustomerAccountManagement $customerAccountManagement
CustomerAccountManagement $customerAccountManagement,
EmailAddress $emailValidator
Copy link
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.

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
Copy link
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
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
Contributor

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
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
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 :)

* @return void
*/
protected function validateEmailAvailable($email)
protected function validateEmailAvailable(string $email): void
Copy link
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
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
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.

None yet

4 participants