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 |
||
| 33 | abstract class AbstractController extends Action |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var CustomerAdmin |
||
| 38 | */ |
||
| 39 | private $customerAdmin; |
||
| 40 | /** |
||
| 41 | * @var Customer |
||
| 42 | */ |
||
| 43 | private $customerGetter; |
||
| 44 | /** |
||
| 45 | * @var Redirect |
||
| 46 | */ |
||
| 47 | private $redirectAction; |
||
| 48 | /** |
||
| 49 | * @var CustomerInterface|false |
||
| 50 | */ |
||
| 51 | private $customerModel; |
||
| 52 | /** |
||
| 53 | * @var IsUsingTwoFactor |
||
| 54 | */ |
||
| 55 | private $isUsingTwoFactor; |
||
| 56 | /** |
||
| 57 | * @var Fetcher |
||
| 58 | */ |
||
| 59 | private $fetcher; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * AbstractController constructor. |
||
| 63 | * |
||
| 64 | * @param Context $context |
||
| 65 | * @param CustomerAdmin $customerAdmin |
||
| 66 | * @param Customer $customerGetter |
||
| 67 | * @param Fetcher $fetcher |
||
| 68 | * @param IsUsingTwoFactor $isUsingTwoFactor |
||
| 69 | */ |
||
| 70 | 6 | View Code Duplication | public function __construct( |
| 83 | |||
| 84 | /** |
||
| 85 | * The controllers should only be run if the following conditions are met: |
||
| 86 | * |
||
| 87 | * - Two Factor Authentication is enabled for the store |
||
| 88 | * - There is a customer |
||
| 89 | * - That customer is using Two Factor Authentication |
||
| 90 | * |
||
| 91 | * If all of these conditions are met, then the method will return true, otherwise a redirect will be created and |
||
| 92 | * the method will return false |
||
| 93 | * |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | 6 | public function shouldActionBeRun() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the redirect action generated by the shouldActionBeRun method |
||
| 121 | * |
||
| 122 | * @return Redirect |
||
| 123 | */ |
||
| 124 | 3 | public function getRedirectAction() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Used to create a redirect action to a specific page |
||
| 131 | * |
||
| 132 | * @param string $path - The path the the customer should be redirected to |
||
| 133 | * |
||
| 134 | * @return Redirect |
||
| 135 | */ |
||
| 136 | 5 | public function redirect($path) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Used to fetch the customer from the session |
||
| 146 | * |
||
| 147 | * @return CustomerInterface|false |
||
| 148 | */ |
||
| 149 | 5 | public function getCustomer() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @return Fetcher |
||
| 160 | */ |
||
| 161 | 3 | public function getUrlFetcher() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * A wrapper method around the Customer::isTwoFactorEnabled method |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | 6 | private function isEnabled() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * A wrapper method around the IsUsingTwoFactor::getValue method |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 4 | private function isCustomerUsingTwoFactor() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * If Two Factor Authentication is disabled redirect the customer to the home page |
||
| 190 | * |
||
| 191 | * @return Redirect |
||
| 192 | */ |
||
| 193 | 1 | private function handleDisabled() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * If there isn't a customer in the session then redirect the user to the login page |
||
| 200 | * |
||
| 201 | * @return Redirect |
||
| 202 | */ |
||
| 203 | 1 | private function handleMissingCustomer() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Redirect customers that are not using Two Factor Authentication to the home page |
||
| 212 | * |
||
| 213 | * @return Redirect |
||
| 214 | */ |
||
| 215 | 1 | private function handleNonOptInCustomer() |
|
| 219 | } |
||
| 220 |
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.