Conditions | 6 |
Paths | 9 |
Total Lines | 26 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 42 |
Changes | 0 |
1 | <?php |
||
33 | public function getLastLines(int $lines = 10, $buffer = 4096) : string { |
||
34 | $f = fopen($this->full_path, "rb"); |
||
35 | if ($f === false) { |
||
36 | throw new Exception("Cant open file"); |
||
37 | } |
||
38 | |||
39 | fseek($f, -1, SEEK_END); |
||
40 | if (fread($f, 1) != "\n") { |
||
41 | $lines -= 1; |
||
42 | } |
||
43 | $output = ''; |
||
44 | |||
45 | while (ftell($f) > 0 && $lines >= 0) { |
||
46 | $seek = min(ftell($f), $buffer); |
||
47 | fseek($f, -$seek, SEEK_CUR); |
||
48 | $output = ($chunk = fread($f, $seek)) . $output; |
||
49 | fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); |
||
50 | $lines -= substr_count($chunk, "\n"); |
||
51 | } |
||
52 | |||
53 | while ($lines++ < 0) { |
||
54 | $output = substr($output, strpos($output, "\n") + 1); |
||
55 | } |
||
56 | |||
57 | fclose($f); |
||
58 | return $output; |
||
59 | } |
||
65 | } |