Passed
Push — master ( e847fb...cefdd7 )
by Shinji
03:11 queued 01:31
created

NativeFileReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Lib\File;
15
16
use FFI;
17
18
class NativeFileReader implements FileReaderInterface
19
{
20
    /** @var FFI\Libc\libc_file_ffi  */
21
    private FFI $ffi;
22
23
    public function __construct()
24
    {
25
        /** @var FFI\Libc\libc_file_ffi */
26
        $this->ffi = FFI::cdef('
0 ignored issues
show
Bug introduced by
The property ffi does not seem to exist on FFI\Libc\libc_file_ffi.
Loading history...
27
            int open(const char *pathname, int flags);
28
            ssize_t read(int fd, void *buf, size_t count);
29
            int close(int fd);
30
        ');
31
    }
32
33
    public function readAll(string $path): string
34
    {
35
        $buffer = $this->ffi->new("unsigned char[4096]");
36
37
        $fd = $this->ffi->open($path, 0);
0 ignored issues
show
Bug introduced by
The method open() does not exist on FFI. It seems like you code against a sub-type of FFI such as FFI\Libc\libc_file_ffi. ( Ignorable by Annotation )

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

37
        /** @scrutinizer ignore-call */ 
38
        $fd = $this->ffi->open($path, 0);
Loading history...
38
        $result = "";
39
        $done = false;
40
        do {
41
            $read_len = $this->ffi->read($fd, $buffer, 4096);
0 ignored issues
show
Bug introduced by
The method read() does not exist on FFI. It seems like you code against a sub-type of FFI such as FFI\Libc\libc_file_ffi. ( Ignorable by Annotation )

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

41
            /** @scrutinizer ignore-call */ 
42
            $read_len = $this->ffi->read($fd, $buffer, 4096);
Loading history...
42
            if ($read_len > 0) {
43
                $result .= FFI::string($buffer, min($read_len, 4096));
44
            } else {
45
                $done = true;
46
            }
47
        } while (!$done);
48
        $this->ffi->close($fd);
0 ignored issues
show
Bug introduced by
The method close() does not exist on FFI. It seems like you code against a sub-type of FFI such as FFI\Libc\libc_file_ffi. ( Ignorable by Annotation )

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

48
        $this->ffi->/** @scrutinizer ignore-call */ 
49
                    close($fd);
Loading history...
49
50
        return $result;
51
    }
52
}
53