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 | class SaveBefore implements ObserverInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var IsUsingTwoFactor |
||
| 37 | */ |
||
| 38 | private $isUsingTwoFactor; |
||
| 39 | /** |
||
| 40 | * @var TwoFactorSecret |
||
| 41 | */ |
||
| 42 | private $twoFactorSecret; |
||
| 43 | /** |
||
| 44 | * @var Secret |
||
| 45 | */ |
||
| 46 | private $secret; |
||
| 47 | /** |
||
| 48 | * @var Session |
||
| 49 | */ |
||
| 50 | private $session; |
||
| 51 | /** |
||
| 52 | * @var IsVerified |
||
| 53 | */ |
||
| 54 | private $isVerified; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * SaveBefore constructor. |
||
| 58 | * |
||
| 59 | * @param IsUsingTwoFactor $isUsingTwoFactor |
||
| 60 | * @param TwoFactorSecret $twoFactorSecret |
||
| 61 | * @param Secret $secret |
||
| 62 | * @param Session $session |
||
| 63 | * @param IsVerified $isVerified |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function __construct( |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param Observer $observer |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | 4 | public function execute(Observer $observer) |
|
| 98 | |||
| 99 | 4 | private function isSetToUseTwoFactor(User $user) |
|
| 103 | |||
| 104 | 2 | private function alreadyHasASecret(User $user) |
|
| 108 | |||
| 109 | 1 | private function addNewSecret(User $user) |
|
| 114 | |||
| 115 | 1 | private function markAsValidated() |
|
| 119 | } |
||
| 120 |
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.