Passed
Branch master (739b13)
by
unknown
02:43
created

logs.php ➔ tailCustom()   C

Complexity

Conditions 9
Paths 41

Size

Total Lines 48
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 41
nop 3
dl 0
loc 48
rs 5.5102
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
 * @param string $filepath
8
 */
9
function tailCustom($filepath, $lines = 1, $adaptive = true)
10
{
11
    // Open file
12
    $f = @fopen($filepath, "rb");
13
    if ($f === false) {
14
        return false;
15
    }
16
    // Sets buffer size, according to the number of lines to retrieve.
17
    // This gives a performance boost when reading a few lines from the file.
18
    if (!$adaptive) {
19
        $buffer = 4096;
20
    } else {
21
        $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
22
    }
23
    // Jump to last character
24
    fseek($f, -1, SEEK_END);
25
    // Read it and adjust line number if necessary
26
    // (Otherwise the result would be wrong if file doesn't end with a blank line)
27
    if (fread($f, 1) != "\n") {
28
        $lines -= 1;
29
    }
30
31
    // Start reading
32
    $output = '';
33
    $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...
34
    // While we would like more
35
    while (ftell($f) > 0 && $lines >= 0) {
36
        // Figure out how far back we should jump
37
        $seek = min(ftell($f), $buffer);
38
        // Do the jump (backwards, relative to where we are)
39
        fseek($f, -$seek, SEEK_CUR);
40
        // Read a chunk and prepend it to our output
41
        $output = ($chunk = fread($f, $seek)) . $output;
42
        // Jump back to where we started reading
43
        fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
44
        // Decrease our line counter
45
        $lines -= substr_count($chunk, "\n");
46
    }
47
    // While we have too many lines
48
    // (Because of buffer size we might have read too many)
49
    while ($lines++ < 0) {
50
        // Find first newline and remove all text before that
51
        $output = substr($output, strpos($output, "\n") + 1);
52
    }
53
    // Close file and return
54
    fclose($f);
55
    return trim($output);
56
}
57
58
?>
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...