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 |
||
27 | class AddPrefixCommandHandler |
||
28 | { |
||
29 | /** |
||
30 | * @var Filesystem |
||
31 | */ |
||
32 | private $filesystem; |
||
33 | |||
34 | /** |
||
35 | * @var Finder |
||
36 | */ |
||
37 | private $finder; |
||
38 | |||
39 | /** |
||
40 | * @var Scoper |
||
41 | */ |
||
42 | private $scoper; |
||
43 | |||
44 | public function __construct(array $declaredClasses = []) |
||
52 | |||
53 | /** |
||
54 | * Handles the "add-prefix" command. |
||
55 | * |
||
56 | * @param string $prefix |
||
57 | * @param string[] $paths |
||
58 | * @param BasicFormatter $formatter |
||
59 | * |
||
60 | * @return int Returns 0 on success and a positive integer on error. |
||
61 | */ |
||
62 | public function handle($prefix, array $paths, BasicFormatter $formatter) |
||
99 | |||
100 | /** |
||
101 | * Scopes all files attached to Finder instance. |
||
102 | * |
||
103 | * @param string $prefix |
||
104 | * @param BasicFormatter $formatter |
||
105 | */ |
||
106 | private function scopeFiles($prefix, BasicFormatter $formatter) |
||
115 | |||
116 | /** |
||
117 | * Scopes a given file. |
||
118 | * |
||
119 | * @param string $path |
||
120 | * @param string $prefix |
||
121 | * @param BasicFormatter $formatter |
||
122 | */ |
||
123 | View Code Duplication | private function scopeFile($path, $prefix, BasicFormatter $formatter) |
|
134 | } |
||
135 |
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.