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 |
||
| 20 | class OneParameterCommand extends AbstractConnectionCommand |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Description of the parameter |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $parameterDescription; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Description of the command. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $description; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Message to display in chat. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $chatMessage; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Name of the dedicated function to call. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $functionName; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function getParameterDescription() |
||
| 54 | { |
||
| 55 | return $this->parameterDescription; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function getChatMessage() |
||
| 62 | { |
||
| 63 | return $this->chatMessage; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function getFunctionName() |
||
| 70 | { |
||
| 71 | return $this->functionName; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritdoc |
||
| 76 | */ |
||
| 77 | 2 | protected function configure() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritdoc |
||
| 88 | */ |
||
| 89 | 1 | public function getDescription() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @inheritdoc |
||
| 96 | */ |
||
| 97 | 1 | View Code Duplication | public function execute($login, InputInterface $input) |
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $parameterDescription |
||
| 114 | */ |
||
| 115 | 2 | public function setParameterDescription($parameterDescription) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $description |
||
| 122 | */ |
||
| 123 | 2 | public function setDescription($description) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $chatMessage |
||
| 130 | */ |
||
| 131 | 2 | public function setChatMessage($chatMessage) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $functionName |
||
| 138 | */ |
||
| 139 | 2 | public function setFunctionName($functionName) |
|
| 143 | } |
||
| 144 |
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.