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 |
||
| 31 | class Fetcher |
||
| 32 | { |
||
| 33 | const CUSTOMER_AUTHENTICATION_URL = 'twofactor/customerlogin/index'; |
||
| 34 | const CUSTOMER_VERIFICATION_URL = 'twofactor/customerlogin/verify'; |
||
| 35 | const CUSTOMER_ACCOUNT_URL = 'customer/account/index'; |
||
| 36 | const CUSTOMER_LOGIN_URL = 'customer/account/login'; |
||
| 37 | |||
| 38 | const ADMIN_AUTHENTICATION_URL = 'twofactor/adminlogin/index'; |
||
| 39 | const ADMIN_VERIFICATION_URL = 'twofactor/adminlogin/verify'; |
||
| 40 | const ADMIN_DASHBOARD_URL = 'admin/dashboard/index'; |
||
| 41 | const ADMIN_LOGIN_URL = 'admin/index/index'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var UrlInterface |
||
| 45 | */ |
||
| 46 | private $url; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Fetcher constructor. |
||
| 50 | * |
||
| 51 | * @param UrlInterface $url |
||
| 52 | */ |
||
| 53 | 64 | public function __construct(UrlInterface $url) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Used to get either the admin or customer two factor authentication URL |
||
| 60 | * |
||
| 61 | * @param bool $forAdmin - true for the admin URL, false for the customer URL |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 30 | View Code Duplication | public function getAuthenticationUrl($forAdmin = false) |
| 72 | |||
| 73 | /** |
||
| 74 | * Used to get either the admin or customer two factor verification URL |
||
| 75 | * |
||
| 76 | * @param bool $forAdmin - true for the admin URL, false for the customer URL |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | 24 | View Code Duplication | public function getVerificationUrl($forAdmin = false) |
| 87 | |||
| 88 | /** |
||
| 89 | * Used to get the URL for the customer account page |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | 2 | public function getCustomerAccountUrl() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Used to get the URL for the admin dashboard page |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 6 | public function getAdminDashboardUrl() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Used to get the customer login URL |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | 2 | public function getCustomerLogInUrl() |
|
| 117 | |||
| 118 | 4 | public function getAdminLogInUrl() |
|
| 122 | /** |
||
| 123 | * This is used to actually get the URL |
||
| 124 | * |
||
| 125 | * @param $path |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | 40 | private function getUrl($path) |
|
| 132 | } |
||
| 133 |
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.