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 array $options |
||
| 30 | */ |
||
| 31 | public function importIndex( |
||
| 32 | Manager $manager, |
||
| 33 | $filename, |
||
| 34 | OutputInterface $output, |
||
| 35 | $options |
||
| 36 | ) { |
||
| 37 | $reader = $this->getReader($manager, $this->getFilePath($filename), $options); |
||
| 38 | |||
| 39 | $progress = new ProgressBar($output, $reader->count()); |
||
| 40 | $progress->setRedrawFrequency(100); |
||
| 41 | $progress->start(); |
||
| 42 | |||
| 43 | $bulkSize = $options['bulk-size']; |
||
| 44 | foreach ($reader as $key => $document) { |
||
| 45 | $data = $document['_source']; |
||
| 46 | $data['_id'] = $document['_id']; |
||
| 47 | |||
| 48 | if (array_key_exists('fields', $document)) { |
||
| 49 | $data = array_merge($document['fields'], $data); |
||
| 50 | } |
||
| 51 | |||
| 52 | $manager->bulk('index', $document['_type'], $data); |
||
|
|
|||
| 53 | |||
| 54 | if (($key + 1) % $bulkSize == 0) { |
||
| 55 | $manager->commit(); |
||
| 56 | } |
||
| 57 | |||
| 58 | $progress->advance(); |
||
| 59 | } |
||
| 60 | |||
| 61 | $manager->commit(); |
||
| 62 | |||
| 63 | $progress->finish(); |
||
| 64 | $output->writeln(''); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns a real file path. |
||
| 69 | * |
||
| 70 | * @param string $filename |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | View Code Duplication | protected function getFilePath($filename) |
|
| 75 | { |
||
| 76 | if ($filename{0} == '/' || strstr($filename, ':') !== false) { |
||
| 77 | return $filename; |
||
| 78 | } |
||
| 79 | |||
| 80 | return realpath(getcwd() . '/' . $filename); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Prepares JSON reader. |
||
| 85 | * |
||
| 86 | * @param Manager $manager |
||
| 87 | * @param string $filename |
||
| 88 | * @param array $options |
||
| 89 | * |
||
| 90 | * @return JsonReader |
||
| 91 | */ |
||
| 92 | protected function getReader($manager, $filename, $options) |
||
| 96 | } |
||
| 97 |
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.