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 |
||
28 | class AnnotationScanner implements ScannerInterface |
||
29 | { |
||
30 | /** @type array $result result of scanning task */ |
||
31 | public $result = []; |
||
32 | /** @type array $ignoreList annotations to be ignored */ |
||
33 | public $ignoreList = [ |
||
34 | "link", |
||
35 | "copyright", |
||
36 | "license", |
||
37 | "package", |
||
38 | "author", |
||
39 | "since", |
||
40 | "type", |
||
41 | "param", |
||
42 | "return", |
||
43 | "throws", |
||
44 | "todo", |
||
45 | "see", |
||
46 | "ignore" |
||
47 | ]; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Scans a file |
||
52 | * |
||
53 | * @param string $uFile file path |
||
54 | * @param string $uFileContents contents of file |
||
55 | * |
||
56 | * @return void |
||
57 | */ |
||
58 | public function processFile($uFile, $uFileContents) |
||
61 | |||
62 | /** |
||
63 | * Scans a token stream |
||
64 | * |
||
65 | * @param TokenStream $uTokenStream extracted tokens wrapped with tokenstream |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | public function processTokenStream(TokenStream $uTokenStream) |
||
72 | |||
73 | /** |
||
74 | * Processes classes using reflection |
||
75 | * |
||
76 | * @param string $uClass class name |
||
77 | * @param ReflectionClass $uReflection reflection information for the class |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function processClass($uClass, ReflectionClass $uReflection) |
||
157 | |||
158 | /** |
||
159 | * Finalizes the task |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | public function finalize() |
||
166 | |||
167 | /** |
||
168 | * Parses the docblock and returns annotations in an array |
||
169 | * |
||
170 | * @param string $uDocComment docblock which contains annotations |
||
171 | * |
||
172 | * @return array set of annotations |
||
173 | */ |
||
174 | protected function parseAnnotations($uDocComment) |
||
201 | } |
||
202 |
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.