Test Failed
Push — master ( 0107e6...66b8c9 )
by Bence
05:42
created

TempFile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CSFCloud\TempFiles;
4
5
class TempFile {
6
7
    private $id;
8
    private $full_path;
9
10
    public function __construct(string $id, string $path) {
11
        $this->id = $id;
12
        $this->full_path = $path;
13
    }
14
15
    public function getId() : string {
16
        return $this->id;
17
    }
18
19
    public function getPath() : string {
20
        return $this->full_path;
21
    }
22
23
    public function __toString() : string {
24
        return $this->full_path;
25
    }
26
27
    public function getText() : string {
28
        return file_get_contents($this->full_path);
29
    }
30
31
    public function getLastLines(int $lines = 10, $buffer = 4096) : string {
32
        $f = fopen($this->full_path, "rb");
33
        fseek($f, -1, SEEK_END);
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        fseek(/** @scrutinizer ignore-type */ $f, -1, SEEK_END);
Loading history...
34
        if(fread($f, 1) != "\n") {
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        if(fread(/** @scrutinizer ignore-type */ $f, 1) != "\n") {
Loading history...
35
            $lines -= 1;
36
        }
37
        $output = '';
38
        $chunk = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $chunk is dead and can be removed.
Loading history...
39
40
        while(ftell($f) > 0 && $lines >= 0) {
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of ftell() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        while(ftell(/** @scrutinizer ignore-type */ $f) > 0 && $lines >= 0) {
Loading history...
41
            $seek = min(ftell($f), $buffer);
42
            fseek($f, -$seek, SEEK_CUR);
43
            $output = ($chunk = fread($f, $seek)).$output;
44
            fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
45
            $lines -= substr_count($chunk, "\n");
46
        }
47
48
        while($lines++ < 0) {
49
            $output = substr($output, strpos($output, "\n") + 1);
50
        }
51
    
52
        fclose($f);
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        fclose(/** @scrutinizer ignore-type */ $f);
Loading history...
53
        return $output;
54
    }
55
56
    public function delete() {
57
        unlink($this->full_path);
58
    }
59
60
}