Passed
Pull Request — master (#96)
by Shinji
01:40
created

Execvp   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 36
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execvp() 0 22 2
A __construct() 0 6 1
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\Libc\Unistd;
15
16
use FFI\CInteger;
17
18
class Execvp
19
{
20
    /** @var \FFI\Libc\execvp_ffi */
21
    private \FFI $ffi;
22
23
    public function __construct()
24
    {
25
        /** @var \FFI\Libc\execvp_ffi */
26
        $this->ffi = \FFI::cdef('
0 ignored issues
show
Bug introduced by
The property ffi does not seem to exist on FFI\Libc\execvp_ffi.
Loading history...
27
            int execvp(const char *file, char *const argv[]);
28
       ', 'libc.so.6');
29
    }
30
31
    /** @param list<string> $argv */
0 ignored issues
show
Bug introduced by
The type PhpProfiler\Lib\Libc\Unistd\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
    public function execvp(string $file, array $argv): int
33
    {
34
        /** @var CInteger $zero */
35
        $zero = \FFI::new('long', false, true);
36
        $zero->cdata = 0;
37
        $null = \FFI::cast('void *', $zero);
38
39
        $args = [$file, ...$argv];
40
        $size = \count($args) + 1;
41
        /** @var \FFI\CArray $argv_real */
42
        $argv_real = \FFI::new('char *[' . $size . ']', false, true);
43
        foreach ($args as $key => $item) {
44
            $item_len = strlen($item);
0 ignored issues
show
Bug introduced by
It seems like $item can also be of type array; however, parameter $string of strlen() does only seem to accept string, 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

44
            $item_len = strlen(/** @scrutinizer ignore-type */ $item);
Loading history...
45
            $item_len_nul = $item_len + 1;
46
            /** @var \FFI\CArray $argv_item */
47
            $argv_item = \FFI::new("char[{$item_len_nul}]", false, true);
48
            \FFI::memcpy($argv_item, $item, $item_len);
49
            $argv_item[$item_len] = "\0";
50
            $argv_real[$key] = $argv_item;
51
        }
52
        $argv_real[$key + 1] = $null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key seems to be defined by a foreach iteration on line 43. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
53
        return $this->ffi->execvp($file, $argv_real);
0 ignored issues
show
Bug introduced by
The method execvp() does not exist on FFI. It seems like you code against a sub-type of FFI such as FFI\Libc\execvp_ffi. ( Ignorable by Annotation )

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

53
        return $this->ffi->/** @scrutinizer ignore-call */ execvp($file, $argv_real);
Loading history...
54
    }
55
}
56