Passed
Branch master (120d90)
by
unknown
02:31
created

logs.php ➔ tailCustom()   D

Complexity

Conditions 9
Paths 41

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 41
nop 3
dl 0
loc 41
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * Slightly modified version of http://www.geekality.net/2011/05/28/php-tail-tackling-large-files/
4
 * @author Torleif Berger, Lorenzo Stanco
5
 * @link http://stackoverflow.com/a/15025877/995958
6
 * @license http://creativecommons.org/licenses/by/3.0/
7
 */
8
function tailCustom($filepath, $lines = 1, $adaptive = true)
9
{
10
    // Open file
11
    $f = @fopen($filepath, "rb");
12
    if ($f === false) return false;
13
    // Sets buffer size, according to the number of lines to retrieve.
14
    // This gives a performance boost when reading a few lines from the file.
15
    if (!$adaptive) $buffer = 4096;
16
    else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
17
    // Jump to last character
18
    fseek($f, -1, SEEK_END);
19
    // Read it and adjust line number if necessary
20
    // (Otherwise the result would be wrong if file doesn't end with a blank line)
21
    if (fread($f, 1) != "\n") $lines -= 1;
22
23
    // Start reading
24
    $output = '';
25
    $chunk = '';
0 ignored issues
show
Unused Code introduced by
$chunk is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
    // While we would like more
27
    while (ftell($f) > 0 && $lines >= 0) {
28
        // Figure out how far back we should jump
29
        $seek = min(ftell($f), $buffer);
30
        // Do the jump (backwards, relative to where we are)
31
        fseek($f, -$seek, SEEK_CUR);
32
        // Read a chunk and prepend it to our output
33
        $output = ($chunk = fread($f, $seek)) . $output;
34
        // Jump back to where we started reading
35
        fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
36
        // Decrease our line counter
37
        $lines -= substr_count($chunk, "\n");
38
    }
39
    // While we have too many lines
40
    // (Because of buffer size we might have read too many)
41
    while ($lines++ < 0) {
42
        // Find first newline and remove all text before that
43
        $output = substr($output, strpos($output, "\n") + 1);
44
    }
45
    // Close file and return
46
    fclose($f);
47
    return trim($output);
48
}
49
50
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...