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 |
||
26 | class ExportService |
||
27 | { |
||
28 | /** |
||
29 | * Exports es index to provided file. |
||
30 | * |
||
31 | * @param Manager $manager |
||
32 | * @param string $filename |
||
33 | * @param array $types |
||
34 | * @param int $chunkSize |
||
35 | * @param int $maxLinesInFile |
||
36 | * @param OutputInterface $output |
||
37 | */ |
||
38 | public function exportIndex( |
||
39 | Manager $manager, |
||
40 | $filename, |
||
41 | $types, |
||
42 | $chunkSize, |
||
43 | OutputInterface $output, |
||
44 | $maxLinesInFile = 300000 |
||
45 | ) { |
||
46 | |||
47 | $search = new Search(); |
||
48 | $search->addQuery(new MatchAllQuery()); |
||
49 | $search->setSize($chunkSize); |
||
50 | |||
51 | $queryParameters = [ |
||
52 | '_source' => true, |
||
53 | 'scroll' => '10m', |
||
54 | ]; |
||
55 | |||
56 | $searchResults = $manager->search($types, $search->toArray(), $queryParameters); |
||
57 | |||
58 | $results = new RawIterator( |
||
59 | $searchResults, |
||
|
|||
60 | $manager, |
||
61 | [ |
||
62 | 'duration' => $queryParameters['scroll'], |
||
63 | '_scroll_id' => $searchResults['_scroll_id'], |
||
64 | ] |
||
65 | ); |
||
66 | |||
67 | $progress = new ProgressBar($output, $results->count()); |
||
68 | $progress->setRedrawFrequency(100); |
||
69 | $progress->start(); |
||
70 | |||
71 | $counter = $fileCounter = 0; |
||
72 | $count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
||
73 | |||
74 | $date = date(\DateTime::ISO8601); |
||
75 | $metadata = [ |
||
76 | 'count' => $count, |
||
77 | 'date' => $date, |
||
78 | ]; |
||
79 | |||
80 | $filename = str_replace('.json', '', $filename); |
||
81 | $writer = $this->getWriter($this->getFilePath($filename.'.json'), $metadata); |
||
82 | |||
83 | $file = []; |
||
84 | foreach ($results as $data) { |
||
85 | if ($counter >= $maxLinesInFile) { |
||
86 | $writer->finalize(); |
||
87 | $writer = null; |
||
88 | $fileCounter++; |
||
89 | $count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter); |
||
90 | $metadata = [ |
||
91 | 'count' => $count, |
||
92 | 'date' => $date, |
||
93 | ]; |
||
94 | $writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata); |
||
95 | $counter = 0; |
||
96 | } |
||
97 | |||
98 | $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source'])); |
||
99 | $writer->push($doc); |
||
100 | $file[] = $doc; |
||
101 | $progress->advance(); |
||
102 | $counter++; |
||
103 | } |
||
104 | |||
105 | $writer->finalize(); |
||
106 | $progress->finish(); |
||
107 | $output->writeln(''); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns real file path. |
||
112 | * |
||
113 | * @param string $filename |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | View Code Duplication | protected function getFilePath($filename) |
|
125 | |||
126 | /** |
||
127 | * Prepares JSON writer. |
||
128 | * |
||
129 | * @param string $filename |
||
130 | * @param array $metadata |
||
131 | * |
||
132 | * @return JsonWriter |
||
133 | */ |
||
134 | protected function getWriter($filename, $metadata) |
||
138 | |||
139 | /** |
||
140 | * @param int $resultsCount |
||
141 | * @param int $maxLinesInFile |
||
142 | * @param int $fileCounter |
||
143 | * |
||
144 | * @return int |
||
145 | */ |
||
146 | protected function getFileCount($resultsCount, $maxLinesInFile, $fileCounter) |
||
157 | } |
||
158 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: