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