Issues (200)

src/Lib/File/NativeFileReader.php (4 issues)

Labels
Severity
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
use PhpProfiler\Lib\FFI\CannotAllocateBufferException;
18
19
class NativeFileReader implements FileReaderInterface
20
{
21
    /** @var FFI\Libc\libc_file_ffi  */
22
    private FFI $ffi;
23
24
    public function __construct()
25
    {
26
        /** @var FFI\Libc\libc_file_ffi */
27
        $this->ffi = FFI::cdef('
0 ignored issues
show
The property ffi does not seem to exist on FFI\Libc\libc_file_ffi.
Loading history...
28
            int open(const char *pathname, int flags);
29
            ssize_t read(int fd, void *buf, size_t count);
30
            int close(int fd);
31
        ');
32
    }
33
34
    public function readAll(string $path): string
35
    {
36
        $buffer = $this->ffi->new("unsigned char[4096]")
37
            ?? throw new CannotAllocateBufferException('cannot allocate buffer');
38
39
        $fd = $this->ffi->open($path, 0);
0 ignored issues
show
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

39
        /** @scrutinizer ignore-call */ 
40
        $fd = $this->ffi->open($path, 0);
Loading history...
40
        $result = "";
41
        $done = false;
42
        do {
43
            $read_len = $this->ffi->read($fd, $buffer, 4096);
0 ignored issues
show
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

43
            /** @scrutinizer ignore-call */ 
44
            $read_len = $this->ffi->read($fd, $buffer, 4096);
Loading history...
44
            if ($read_len > 0) {
45
                $result .= FFI::string($buffer, min($read_len, 4096));
46
            } else {
47
                $done = true;
48
            }
49
        } while (!$done);
50
        $this->ffi->close($fd);
0 ignored issues
show
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

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