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 |
||
10 | class SelfUpdateCommand extends Command implements PharContext |
||
11 | { |
||
12 | /** |
||
13 | * @var Updater |
||
14 | */ |
||
15 | private $updater; |
||
16 | |||
17 | /** |
||
18 | * SelfUpdateCommand constructor. |
||
19 | * |
||
20 | * @param null $name |
||
21 | * @param Updater $updater |
||
22 | */ |
||
23 | public function __construct($name, Updater $updater) |
||
29 | |||
30 | /** |
||
31 | * Configure the command. |
||
32 | */ |
||
33 | protected function configure() |
||
44 | |||
45 | /** |
||
46 | * Execute the command. |
||
47 | * |
||
48 | * @param InputInterface $input |
||
49 | * @param OutputInterface $output |
||
50 | * |
||
51 | * @return int |
||
52 | */ |
||
53 | protected function execute(InputInterface $input, OutputInterface $output) |
||
61 | |||
62 | /** |
||
63 | * Validate user input. |
||
64 | * |
||
65 | * @param InputInterface $input |
||
66 | */ |
||
67 | protected function validateInput(InputInterface $input) |
||
71 | |||
72 | /** |
||
73 | * @param $stability |
||
74 | */ |
||
75 | private function validateStability($stability) |
||
85 | |||
86 | /** |
||
87 | * @param InputInterface $input |
||
88 | * @param OutputInterface $output |
||
89 | * @return int |
||
90 | */ |
||
91 | private function update(InputInterface $input, OutputInterface $output) |
||
108 | |||
109 | /** |
||
110 | * @param InputInterface $input |
||
111 | * @param OutputInterface $output |
||
112 | * @return int |
||
113 | */ |
||
114 | private function rollback(InputInterface $input, OutputInterface $output) |
||
126 | } |
||
127 |
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.