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 |
||
14 | class FileParser |
||
15 | { |
||
16 | const TODO_FINDER = 'todo_finder'; |
||
17 | const TOTAL_ALLOWED = 'total_allowed'; |
||
18 | const EXPRESSIONS = 'expressions'; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $file; |
||
24 | |||
25 | /** |
||
26 | * @param string $file |
||
27 | */ |
||
28 | public function __construct($file) |
||
36 | |||
37 | /** |
||
38 | * @param array $file |
||
39 | * |
||
40 | * @throws FileParserException |
||
41 | */ |
||
42 | private function validateFileStructure(array &$file) |
||
57 | |||
58 | /** |
||
59 | * @param array $file |
||
60 | * @param string $key |
||
61 | * |
||
62 | * @throws FileParserException |
||
63 | */ |
||
64 | private function assertYamlKey(array &$file, $key) |
||
77 | |||
78 | /** |
||
79 | * @throws FileParserException |
||
80 | * @return int |
||
81 | */ |
||
82 | public function getTotalAllowed() |
||
97 | |||
98 | /** |
||
99 | * @throws FileParserException |
||
100 | * @return array |
||
101 | */ |
||
102 | public function getTodoExpressions() |
||
118 | } |
||
119 |
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.