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 |
||
9 | class Configuration extends Config |
||
10 | { |
||
11 | /** |
||
12 | * @var int[] |
||
13 | */ |
||
14 | protected $mapping = []; |
||
15 | |||
16 | /** |
||
17 | * @param string|array $file |
||
18 | * @return \PHPSemVerChecker\Configuration\Configuration |
||
19 | */ |
||
20 | public static function fromFile($file) |
||
24 | |||
25 | /** |
||
26 | * @return \PHPSemVerChecker\Configuration\Configuration |
||
27 | */ |
||
28 | public static function defaults() |
||
32 | |||
33 | /** |
||
34 | * @param string|array $path |
||
35 | */ |
||
36 | 1 | public function __construct($path) |
|
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getLevelMapping() |
||
49 | |||
50 | /** |
||
51 | * Merges command line arguments and file configuration. |
||
52 | * |
||
53 | * This way Console\Command can validate the InputDefinition and we end up with a nice, valid Configuration before |
||
54 | * execute() is called. |
||
55 | * |
||
56 | * @param InputInterface $input |
||
57 | * @return $this |
||
58 | */ |
||
59 | 1 | public function mergeCommandInput(InputInterface $input) |
|
79 | |||
80 | /** |
||
81 | * @param array $mapping |
||
82 | */ |
||
83 | 1 | protected function extractMapping(array $mapping) |
|
90 | } |
||
91 |
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.