Passed
Push — master ( 50166b...e300d2 )
by Shinji
01:23
created

TargetProcessSettings::fromConsoleInput()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 25
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 44
rs 8.0555
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\Command\Inspector\Settings;
15
16
use PhpProfiler\Command\CommandSettingsException;
17
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface 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...
18
19
class TargetProcessSettings
20
{
21
    private const PHP_REGEX_DEFAULT = '.*/(php(74|7.4|80|8.0)?|php-fpm|libphp[78].*\.so)$';
22
    private const LIBPTHREAD_REGEX_DEFAULT = '.*/libpthread.*\.so$';
23
24
    public int $pid;
25
    public string $php_regex;
26
    public string $libpthread_regex;
27
    public ?string $php_path;
28
    public ?string $libpthread_path;
29
30
    /**
31
     * GetTraceSettings constructor.
32
     * @param int $pid
33
     * @param string $php_regex
34
     * @param string $libpthread_regex
35
     * @param string|null $php_path
36
     * @param string|null $libpthread_path
37
     */
38
    public function __construct(
39
        int $pid,
40
        string $php_regex = self::PHP_REGEX_DEFAULT,
41
        string $libpthread_regex = self::LIBPTHREAD_REGEX_DEFAULT,
42
        ?string $php_path = null,
43
        ?string $libpthread_path = null
44
    ) {
45
        $this->pid = $pid;
46
        $this->php_regex = '{' . $php_regex . '}';
47
        $this->libpthread_regex = '{' . $libpthread_regex . '}';
48
        $this->php_path = $php_path;
49
        $this->libpthread_path = $libpthread_path;
50
    }
51
52
    /**
53
     * @param InputInterface $input
54
     * @return self
55
     * @throws CommandSettingsException
56
     */
57
    public static function fromConsoleInput(InputInterface $input): self
58
    {
59
        $pid = $input->getOption('pid');
60
        if (is_null($pid)) {
61
            throw TargetProcessSettingsException::create(
62
                TargetProcessSettingsException::PID_NOT_SPECIFIED
63
            );
64
        }
65
        $pid = filter_var($pid, FILTER_VALIDATE_INT);
66
        if ($pid === false) {
67
            throw TargetProcessSettingsException::create(
68
                TargetProcessSettingsException::PID_NOT_SPECIFIED
69
            );
70
        }
71
72
        $php_regex = $input->getOption('php-regex') ?? self::PHP_REGEX_DEFAULT;
73
        if (!is_string($php_regex)) {
74
            throw TargetProcessSettingsException::create(
75
                TargetProcessSettingsException::PHP_REGEX_IS_NOT_STRING
76
            );
77
        }
78
79
        $libpthread_regex = $input->getOption('libpthread-regex') ?? self::LIBPTHREAD_REGEX_DEFAULT;
80
        if (!is_string($libpthread_regex)) {
81
            throw TargetProcessSettingsException::create(
82
                TargetProcessSettingsException::LIBPTHREAD_REGEX_IS_NOT_STRING
83
            );
84
        }
85
86
        $php_path = $input->getOption('php-path');
87
        if (!is_null($php_path) and !is_string($php_path)) {
88
            throw TargetProcessSettingsException::create(
89
                TargetProcessSettingsException::PHP_PATH_IS_NOT_STRING
90
            );
91
        }
92
93
        $libpthread_path = $input->getOption('libpthread-path');
94
        if (!is_null($libpthread_path) and !is_string($libpthread_path)) {
95
            throw TargetProcessSettingsException::create(
96
                TargetProcessSettingsException::LIBPTHREAD_PATH_IS_NOT_STRING
97
            );
98
        }
99
100
        return new self($pid, $php_regex, $libpthread_regex, $php_path, $libpthread_path);
101
    }
102
}
103