Passed
Pull Request — master (#13)
by Shinji
01:23
created

fromConsoleInput()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 21
nc 6
nop 1
dl 0
loc 39
rs 8.4444
c 1
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
final class TargetPhpSettingsFromConsoleInput
22
{
23
    public function setOptions(Command $command): void
24
    {
25
        $command
26
            ->addOption(
27
                'php-regex',
28
                null,
29
                InputOption::VALUE_OPTIONAL,
30
                'regex to find the php binary loaded in the target process'
31
            )
32
            ->addOption(
33
                'libpthread-regex',
34
                null,
35
                InputOption::VALUE_OPTIONAL,
36
                'regex to find the libpthread.so loaded in the target process'
37
            )
38
            ->addOption(
39
                'php-version',
40
                null,
41
                InputOption::VALUE_OPTIONAL,
42
                'php version of the target'
43
            )
44
            ->addOption(
45
                'php-path',
46
                null,
47
                InputOption::VALUE_OPTIONAL,
48
                'path to the php binary (only needed in tracing chrooted ZTS target)'
49
            )
50
            ->addOption(
51
                'libpthread-path',
52
                null,
53
                InputOption::VALUE_OPTIONAL,
54
                'path to the libpthread.so (only needed in tracing chrooted ZTS target)'
55
            )
56
        ;
57
    }
58
59
    public function fromConsoleInput(InputInterface $input): TargetPhpSettings
60
    {
61
        $php_regex = $input->getOption('php-regex') ?? TargetPhpSettings::PHP_REGEX_DEFAULT;
62
        if (!is_string($php_regex)) {
63
            throw TargetPhpSettingsException::create(
64
                TargetPhpSettingsException::PHP_REGEX_IS_NOT_STRING
65
            );
66
        }
67
68
        $libpthread_regex = $input->getOption('libpthread-regex') ?? TargetPhpSettings::LIBPTHREAD_REGEX_DEFAULT;
69
        if (!is_string($libpthread_regex)) {
70
            throw TargetPhpSettingsException::create(
71
                TargetPhpSettingsException::LIBPTHREAD_REGEX_IS_NOT_STRING
72
            );
73
        }
74
75
        $php_version = $input->getOption('php-version') ?? TargetPhpSettings::TARGET_PHP_VERSION_DEFAULT;
76
        if (!in_array($php_version, ZendTypeReader::ALL_SUPPORTED_VERSIONS, true)) {
77
            throw TargetPhpSettingsException::create(
78
                TargetPhpSettingsException::TARGET_PHP_VERSION_INVALID
79
            );
80
        }
81
        /** @psalm-var value-of<ZendTypeReader::ALL_SUPPORTED_VERSIONS> $php_version */
82
83
        $php_path = $input->getOption('php-path');
84
        if (!is_null($php_path) and !is_string($php_path)) {
85
            throw TargetPhpSettingsException::create(
86
                TargetPhpSettingsException::PHP_PATH_IS_NOT_STRING
87
            );
88
        }
89
90
        $libpthread_path = $input->getOption('libpthread-path');
91
        if (!is_null($libpthread_path) and !is_string($libpthread_path)) {
92
            throw TargetPhpSettingsException::create(
93
                TargetPhpSettingsException::LIBPTHREAD_PATH_IS_NOT_STRING
94
            );
95
        }
96
97
        return new TargetPhpSettings($php_regex, $libpthread_regex, $php_version, $php_path, $libpthread_path);
98
    }
99
}
100