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 QRCode extends Template |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var TwoFactorSecret |
||
| 35 | */ |
||
| 36 | private $twoFactorSecret; |
||
| 37 | /** |
||
| 38 | * @var Customer |
||
| 39 | */ |
||
| 40 | private $customerGetter; |
||
| 41 | /** |
||
| 42 | * @var CustomerConfig |
||
| 43 | */ |
||
| 44 | private $customerConfig; |
||
| 45 | /** |
||
| 46 | * @var GetQrCode |
||
| 47 | */ |
||
| 48 | private $getQrCode; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * QRCode constructor. |
||
| 52 | * |
||
| 53 | * @param Template\Context $context |
||
| 54 | * @param TwoFactorSecret $twoFactorSecret |
||
| 55 | * @param Customer $customerGetter |
||
| 56 | * @param CustomerConfig $customerConfig |
||
| 57 | * @param GetQrCode $getQrCode |
||
| 58 | * @param array $data |
||
| 59 | * |
||
| 60 | * @internal param array $data |
||
| 61 | */ |
||
| 62 | 4 | View Code Duplication | public function __construct( |
| 76 | |||
| 77 | /** |
||
| 78 | * A simple getter method to return the current customer |
||
| 79 | * |
||
| 80 | * @return CustomerInterface|false |
||
| 81 | */ |
||
| 82 | 4 | public function getCustomer() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Used to check if the code should be displayed - will return false if two factor is disabled in the config, or if |
||
| 89 | * the customer does not have a code. Otherwise returns true |
||
| 90 | * |
||
| 91 | * @param CustomerInterface $customer |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | 4 | public function shouldQrCodeBeDisplayed(CustomerInterface $customer) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Used to get the customers QR Code. Will return false if they don't have one, otherwise will return the image |
||
| 110 | * string |
||
| 111 | * |
||
| 112 | * @param CustomerInterface $customer |
||
| 113 | * |
||
| 114 | * @return bool|string |
||
| 115 | */ |
||
| 116 | 2 | public function getQrCode(CustomerInterface $customer) |
|
| 120 | } |
||
| 121 |
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.