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 |
||
| 47 | class Postdispatch implements ObserverInterface |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var ResponseFactory |
||
| 51 | */ |
||
| 52 | private $responseFactory; |
||
| 53 | /** |
||
| 54 | * @var UrlInterface |
||
| 55 | */ |
||
| 56 | private $url; |
||
| 57 | /** |
||
| 58 | * @var Customer |
||
| 59 | */ |
||
| 60 | private $customerGetter; |
||
| 61 | /** |
||
| 62 | * @var IsUsingTwoFactor |
||
| 63 | */ |
||
| 64 | private $isUsingTwoFactor; |
||
| 65 | /** |
||
| 66 | * @var IsVerified |
||
| 67 | */ |
||
| 68 | private $isVerified; |
||
| 69 | /** |
||
| 70 | * @var Session |
||
| 71 | */ |
||
| 72 | private $customerSession; |
||
| 73 | /** |
||
| 74 | * @var CustomerAdmin |
||
| 75 | */ |
||
| 76 | private $customerAdmin; |
||
| 77 | /** |
||
| 78 | * @var Fetcher |
||
| 79 | */ |
||
| 80 | private $fetcher; |
||
| 81 | /** |
||
| 82 | * @var Checker |
||
| 83 | */ |
||
| 84 | private $checker; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Predispatch constructor. |
||
| 88 | * |
||
| 89 | * @param ResponseFactory $responseFactory |
||
| 90 | * @param UrlInterface $url |
||
| 91 | * @param Customer $customerGetter |
||
| 92 | * @param IsVerified $isVerified |
||
| 93 | * @param Session $customerSession |
||
| 94 | * @param IsUsingTwoFactor $isUsingTwoFactor |
||
| 95 | * @param CustomerAdmin $customerAdmin |
||
| 96 | * @param Fetcher $fetcher |
||
| 97 | * @param Checker $checker |
||
| 98 | */ |
||
| 99 | 27 | public function __construct( |
|
| 120 | |||
| 121 | /** |
||
| 122 | * This is the observer method. It *now* listens for the controller_front_send_response_before event, and really |
||
| 123 | * should be renamed |
||
| 124 | * |
||
| 125 | * @TODO: Rename the class so it matches the event |
||
| 126 | * |
||
| 127 | * @param Observer $observer |
||
| 128 | * |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | 27 | View Code Duplication | public function execute(Observer $observer) |
| 148 | |||
| 149 | /** |
||
| 150 | * This checks to see if the customer is on a page that shouldn't be redirected, if we actually have a customer, and |
||
| 151 | * if so does that customer have two fact enabled. Very similar checks are done in the admin observer and this is |
||
| 152 | * one of the methods that I want to refactor, once the test coverage is high enough to let me do this with |
||
| 153 | * confidence |
||
| 154 | * |
||
| 155 | * @TODO: Refactor this |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | 17 | private function shouldTheCustomerBeRedirected() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Checks if we are on the authentication or verification page. This code is duplicated in the admin observer, other |
||
| 179 | * than forAdmin flag and can be refactored |
||
| 180 | * |
||
| 181 | * @TODO: move this to either the Checker class or somewhere else |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | 17 | View Code Duplication | private function areWeOnAnAllowedPage() |
| 198 | |||
| 199 | /** |
||
| 200 | * Checks the session to see if the verification flag has been set. Can be refactored |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | 5 | private function hasTwoFactorBeenChecked() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Redirects the customer to two factor authentication page, i.e. where they need to enter in their code./ |
||
| 214 | * |
||
| 215 | * @param $response |
||
| 216 | */ |
||
| 217 | 3 | private function redirectToTwoFactorCheck($response) |
|
| 223 | } |
||
| 224 |
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.