Passed
Pull Request — master (#13)
by Shinji
02:00
created

TargetPhpSettingsFromConsoleInput::setOptions()   A

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