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 |
||
21 | class ImportService |
||
22 | { |
||
23 | /** |
||
24 | * Imports Elasticsearch index data. |
||
25 | * |
||
26 | * @param Manager $manager |
||
27 | * @param string $filename |
||
28 | * @param OutputInterface $output |
||
29 | * @param int $bulkSize |
||
30 | */ |
||
31 | public function importIndex(Manager $manager, $filename, OutputInterface $output, $bulkSize) |
||
61 | |||
62 | /** |
||
63 | * Returns a real file path. |
||
64 | * |
||
65 | * @param string $filename |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | View Code Duplication | protected function getFilePath($filename) |
|
77 | |||
78 | /** |
||
79 | * Prepares JSON reader. |
||
80 | * |
||
81 | * @param Manager $manager |
||
82 | * @param string $filename |
||
83 | * @param bool $convertDocuments |
||
84 | * |
||
85 | * @return JsonReader |
||
86 | */ |
||
87 | protected function getReader($manager, $filename, $convertDocuments) |
||
91 | } |
||
92 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.