TargetPhpSettingsFromConsoleInput::setOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 1
dl 0
loc 32
rs 9.504
c 0
b 0
f 0
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\Inspector\Settings\TargetPhpSettings;
15
16
use PhpProfiler\Lib\PhpInternals\ZendTypeReader;
17
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command 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
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...
19
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption 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...
20
21
use function in_array;
22
use function is_null;
23
use function is_string;
24
25
final class TargetPhpSettingsFromConsoleInput
26
{
27
    /** @codeCoverageIgnore */
28
    public function setOptions(Command $command): void
29
    {
30
        $command
31
            ->addOption(
32
                'php-regex',
33
                null,
34
                InputOption::VALUE_OPTIONAL,
35
                'regex to find the php binary loaded in the target process'
36
            )
37
            ->addOption(
38
                'libpthread-regex',
39
                null,
40
                InputOption::VALUE_OPTIONAL,
41
                'regex to find the libpthread.so loaded in the target process'
42
            )
43
            ->addOption(
44
                'php-version',
45
                null,
46
                InputOption::VALUE_OPTIONAL,
47
                'php version (auto|v7[0-4]|v8[01]) of the target (default: auto)'
48
            )
49
            ->addOption(
50
                'php-path',
51
                null,
52
                InputOption::VALUE_OPTIONAL,
53
                'path to the php binary (only needed in tracing chrooted ZTS target)'
54
            )
55
            ->addOption(
56
                'libpthread-path',
57
                null,
58
                InputOption::VALUE_OPTIONAL,
59
                'path to the libpthread.so (only needed in tracing chrooted ZTS target)'
60
            )
61
        ;
62
    }
63
64
    public function createSettings(InputInterface $input): TargetPhpSettings
65
    {
66
        $php_regex = $input->getOption('php-regex') ?? TargetPhpSettings::PHP_REGEX_DEFAULT;
67
        if (!is_string($php_regex)) {
68
            throw TargetPhpSettingsException::create(
69
                TargetPhpSettingsException::PHP_REGEX_IS_NOT_STRING
70
            );
71
        }
72
73
        $libpthread_regex = $input->getOption('libpthread-regex') ?? TargetPhpSettings::LIBPTHREAD_REGEX_DEFAULT;
74
        if (!is_string($libpthread_regex)) {
75
            throw TargetPhpSettingsException::create(
76
                TargetPhpSettingsException::LIBPTHREAD_REGEX_IS_NOT_STRING
77
            );
78
        }
79
80
        $php_version = $input->getOption('php-version') ?? TargetPhpSettings::TARGET_PHP_VERSION_DEFAULT;
81
        if ($php_version !== 'auto' and !in_array($php_version, ZendTypeReader::ALL_SUPPORTED_VERSIONS, true)) {
82
            throw TargetPhpSettingsException::create(
83
                TargetPhpSettingsException::TARGET_PHP_VERSION_INVALID
84
            );
85
        }
86
87
        $php_path = $input->getOption('php-path');
88
        if (!is_null($php_path) and !is_string($php_path)) {
89
            throw TargetPhpSettingsException::create(
90
                TargetPhpSettingsException::PHP_PATH_IS_NOT_STRING
91
            );
92
        }
93
94
        $libpthread_path = $input->getOption('libpthread-path');
95
        if (!is_null($libpthread_path) and !is_string($libpthread_path)) {
96
            throw TargetPhpSettingsException::create(
97
                TargetPhpSettingsException::LIBPTHREAD_PATH_IS_NOT_STRING
98
            );
99
        }
100
101
        return new TargetPhpSettings($php_regex, $libpthread_regex, $php_version, $php_path, $libpthread_path);
102
    }
103
}
104