| Conditions | 11 |
| Paths | 9 |
| Total Lines | 60 |
| Lines | 5 |
| Ratio | 8.33 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 83 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 84 | { |
||
| 85 | $this->detectMagento($output); |
||
| 86 | if ($this->initMagento()) { |
||
| 87 | $questionHelper = $this->getHelperSet()->get('question'); |
||
| 88 | $email = $this->getHelper('parameter')->askEmail($input, $output); |
||
|
|
|||
| 89 | |||
| 90 | // Password |
||
| 91 | $password = $input->getArgument('password'); |
||
| 92 | View Code Duplication | if ($password === null) { |
|
| 93 | $question = new Question('<question>Password:</question>'); |
||
| 94 | $question->setHidden(true); |
||
| 95 | $password = $questionHelper->ask($input, $output, $question); |
||
| 96 | } |
||
| 97 | |||
| 98 | $website = $this->getHelper('parameter')->askWebsite($input, $output); |
||
| 99 | |||
| 100 | $changePassword = function () use ($email, $website, $password) { |
||
| 101 | // @see \Magento\Framework\Session\SessionManager::isSessionExists Hack to prevent session problems |
||
| 102 | @session_start(); |
||
| 103 | |||
| 104 | // Fix for proxy which does not respect "emulateAreaCode". |
||
| 105 | /** @var \Magento\Theme\Model\View\Design $design */ |
||
| 106 | $design = $this->getObjectManager()->get(\Magento\Theme\Model\View\Design::class); |
||
| 107 | $design->setArea('frontend'); |
||
| 108 | |||
| 109 | $customer = $this->customerRegistry->retrieveByEmail($email, $website->getId()); |
||
| 110 | $passwordHash = $this->encryptor->getHash($password, true); |
||
| 111 | |||
| 112 | if ($customer->getId()) { |
||
| 113 | $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId()); |
||
| 114 | |||
| 115 | $customer->setRpToken($passwordHash ? null : $customerSecure->getRpToken()); |
||
| 116 | $customer->setRpTokenCreatedAt($passwordHash ? null : $customerSecure->getRpTokenCreatedAt()); |
||
| 117 | $customer->setPasswordHash($passwordHash ?: $customerSecure->getPasswordHash()); |
||
| 118 | |||
| 119 | $customer->setFailuresNum($customerSecure->getFailuresNum()); |
||
| 120 | $customer->setFirstFailure($customerSecure->getFirstFailure()); |
||
| 121 | $customer->setLockExpires($customerSecure->getLockExpires()); |
||
| 122 | } elseif ($passwordHash) { |
||
| 123 | $customer->setPasswordHash($passwordHash); |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->customerResource->save($customer); |
||
| 127 | |||
| 128 | if ($passwordHash && $customer->getId()) { |
||
| 129 | $this->customerRegistry->remove($customer->getId()); |
||
| 130 | } |
||
| 131 | }; |
||
| 132 | |||
| 133 | try { |
||
| 134 | $this->state->setAreaCode(Area::AREA_FRONTEND); |
||
| 135 | $this->state->emulateAreaCode(Area::AREA_FRONTEND, $changePassword); |
||
| 136 | |||
| 137 | $output->writeln('<info>Password successfully changed</info>'); |
||
| 138 | } catch (Exception $e) { |
||
| 139 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: