Conditions | 8 |
Paths | 14 |
Total Lines | 53 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
150 | public function store(array $todelete, string $outputfile): void |
||
151 | { |
||
152 | echo "Preparing to delete [" . count($todelete) . "] trackids\n"; |
||
153 | |||
154 | if (!is_dir($this->statdir)) { |
||
155 | throw new Exception('Statistics module: output dir do not exists [' . $this->statdir . ']'); |
||
156 | } |
||
157 | |||
158 | if (!file_exists($this->inputfile)) { |
||
159 | throw new Exception('Statistics module: input file do not exists [' . $this->inputfile . ']'); |
||
160 | } |
||
161 | |||
162 | $file = fopen($this->inputfile, 'r'); |
||
163 | |||
164 | // Open the output file in a way that guarantees that we will not overwrite a random file. |
||
165 | if (file_exists($outputfile)) { |
||
166 | // Delete existing output file. |
||
167 | unlink($outputfile); |
||
168 | } |
||
169 | $outfile = fopen($outputfile, 'x'); // Create the output file |
||
170 | |||
171 | $logparser = new LogParser( |
||
172 | $this->statconfig->getOptionalValue('datestart', 0), |
||
173 | $this->statconfig->getOptionalValue('datelength', 15), |
||
174 | $this->statconfig->getOptionalValue('offsetspan', 44), |
||
175 | ); |
||
176 | |||
177 | $i = 0; |
||
178 | // Parse through log file, line by line |
||
179 | while (!feof($file)) { |
||
180 | $logline = fgets($file, 4096); |
||
181 | |||
182 | // Continue if STAT is not found on line. |
||
183 | if (!preg_match('/STAT/', $logline)) { |
||
184 | continue; |
||
185 | } |
||
186 | $i++; |
||
187 | |||
188 | $content = $logparser->parseContent($logline); |
||
189 | |||
190 | if (($i % 10000) == 0) { |
||
191 | echo "Read line " . $i . "\n"; |
||
192 | } |
||
193 | |||
194 | $trackid = $content[4]; |
||
195 | if (in_array($trackid, $todelete, true)) { |
||
196 | continue; |
||
197 | } |
||
198 | |||
199 | fputs($outfile, $logline); |
||
200 | } |
||
201 | fclose($file); |
||
202 | fclose($outfile); |
||
203 | } |
||
205 |