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 |
||
24 | class Indexer { |
||
25 | /** |
||
26 | * @var Log |
||
27 | */ |
||
28 | protected $log; |
||
29 | |||
30 | /** |
||
31 | * @var Insert |
||
32 | */ |
||
33 | protected $insert; |
||
34 | |||
35 | /** |
||
36 | * @var \PhpParser\Parser |
||
37 | */ |
||
38 | protected $parser; |
||
39 | |||
40 | /** |
||
41 | * @var ASTVisitor[] |
||
42 | */ |
||
43 | protected $ast_visitors; |
||
44 | |||
45 | 53 | View Code Duplication | public function __construct(Log $log, \PhpParser\Parser $parser, Insert $insert, array $ast_visitors) { |
53 | |||
54 | /** |
||
55 | * Index a directory. |
||
56 | * |
||
57 | * @param string $path |
||
58 | * @param array $ignore_paths |
||
59 | * @return null |
||
60 | */ |
||
61 | 7 | public function index_directory($path, array $ignore_paths) { |
|
85 | |||
86 | /** |
||
87 | * Initialize the filesystem abstraction. |
||
88 | * |
||
89 | * @return Flightcontrol |
||
90 | */ |
||
91 | 7 | public function init_flightcontrol($path) { |
|
96 | |||
97 | /** |
||
98 | * @param string $base_dir |
||
99 | * @param string $path |
||
100 | * @return null |
||
101 | */ |
||
102 | public function index_file($base_dir, $path) { |
||
113 | |||
114 | /** |
||
115 | * @param string $path |
||
116 | * @param string $content |
||
117 | * @return null |
||
118 | */ |
||
119 | 53 | public function index_content($path, $content) { |
|
137 | } |
||
138 |
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.