Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 36 | class Postdispatch implements ObserverInterface |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var ResponseFactory |
||
| 40 | */ |
||
| 41 | private $responseFactory; |
||
| 42 | /** |
||
| 43 | * @var UrlInterface |
||
| 44 | */ |
||
| 45 | private $url; |
||
| 46 | /** |
||
| 47 | * @var Customer |
||
| 48 | */ |
||
| 49 | private $customerGetter; |
||
| 50 | /** |
||
| 51 | * @var IsUsingTwoFactor |
||
| 52 | */ |
||
| 53 | private $isUsingTwoFactor; |
||
| 54 | /** |
||
| 55 | * @var IsVerified |
||
| 56 | */ |
||
| 57 | private $isVerified; |
||
| 58 | /** |
||
| 59 | * @var TwoFactorUrls |
||
| 60 | */ |
||
| 61 | private $twoFactorUrls; |
||
| 62 | /** |
||
| 63 | * @var Session |
||
| 64 | */ |
||
| 65 | private $customerSession; |
||
| 66 | /** |
||
| 67 | * @var CustomerAdmin |
||
| 68 | */ |
||
| 69 | private $customerAdmin; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Predispatch constructor. |
||
| 73 | * |
||
| 74 | * @param ResponseFactory $responseFactory |
||
| 75 | * @param UrlInterface $url |
||
| 76 | * @param Customer $customerGetter |
||
| 77 | * @param IsVerified $isVerified |
||
| 78 | * @param Session $customerSession |
||
| 79 | * @param IsUsingTwoFactor $isUsingTwoFactor |
||
| 80 | * @param TwoFactorUrls $twoFactorUrls |
||
| 81 | * @param CustomerAdmin $customerAdmin |
||
| 82 | */ |
||
| 83 | 40 | View Code Duplication | public function __construct( |
|
|
|||
| 84 | ResponseFactory $responseFactory, |
||
| 85 | UrlInterface $url, |
||
| 86 | Customer $customerGetter, |
||
| 87 | IsVerified $isVerified, |
||
| 88 | Session $customerSession, |
||
| 89 | IsUsingTwoFactor $isUsingTwoFactor, |
||
| 90 | TwoFactorUrls $twoFactorUrls, |
||
| 91 | CustomerAdmin $customerAdmin |
||
| 92 | ) { |
||
| 93 | 40 | $this->responseFactory = $responseFactory; |
|
| 94 | 40 | $this->url = $url; |
|
| 95 | 40 | $this->customerGetter = $customerGetter; |
|
| 96 | 40 | $this->isUsingTwoFactor = $isUsingTwoFactor; |
|
| 97 | 40 | $this->isVerified = $isVerified; |
|
| 98 | 40 | $this->twoFactorUrls = $twoFactorUrls; |
|
| 99 | 40 | $this->customerSession = $customerSession; |
|
| 100 | 40 | $this->customerAdmin = $customerAdmin; |
|
| 101 | 40 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param Observer $observer |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | 40 | View Code Duplication | public function execute(Observer $observer) |
| 125 | |||
| 126 | 40 | private function isTwoFactorEnabled() |
|
| 130 | |||
| 131 | 22 | private function shouldTheCustomerBeRedirected() |
|
| 132 | { |
||
| 133 | 22 | if ($this->areWeOnAnAllowedPage() === true) { |
|
| 134 | 8 | return false; |
|
| 135 | } |
||
| 136 | |||
| 137 | 14 | $customer = $this->customerGetter->getCustomer(); |
|
| 138 | 14 | if ($customer === false) { |
|
| 139 | 2 | return false; |
|
| 140 | } |
||
| 141 | 12 | $usingTwoFactor = $this->isUsingTwoFactor->getValue($customer); |
|
| 142 | 12 | if ($usingTwoFactor === false) { |
|
| 143 | 6 | return false; |
|
| 144 | } |
||
| 145 | |||
| 146 | 6 | return true; |
|
| 147 | } |
||
| 148 | |||
| 149 | 22 | View Code Duplication | private function areWeOnAnAllowedPage() |
| 150 | { |
||
| 151 | 22 | $twoFactorUrls = $this->twoFactorUrls; |
|
| 152 | 22 | if ($twoFactorUrls->areWeOnTheAuthenticationPage(false) === true) { |
|
| 153 | 2 | return true; |
|
| 154 | } |
||
| 155 | |||
| 156 | 20 | if ($twoFactorUrls->areWeOnTheVerificationPage(false) === true) { |
|
| 157 | 6 | return true; |
|
| 158 | } |
||
| 159 | |||
| 160 | 14 | return false; |
|
| 161 | } |
||
| 162 | |||
| 163 | 6 | private function hasTwoFactorBeenChecked() |
|
| 170 | |||
| 171 | 4 | private function redirectToTwoFactorCheck($response) |
|
| 177 | } |
||
| 178 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.