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

DaemonSettingsFromConsoleInput::fromConsoleInput()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 6
nop 1
dl 0
loc 19
rs 9.9
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\DaemonSettings;
15
16
use PhpProfiler\Inspector\Settings\InspectorSettingsException;
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 DaemonSettingsFromConsoleInput
22
{
23
    public function setOptions(Command $command): void
24
    {
25
        $command
26
            ->addOption(
27
                'threads',
28
                'T',
29
                InputOption::VALUE_OPTIONAL,
30
                'number of workers (default: 8)'
31
            )
32
            ->addOption(
33
                'target-regex',
34
                'P',
35
                InputOption::VALUE_OPTIONAL,
36
                'regex to find the php binary loaded in the target process'
37
            )
38
        ;
39
    }
40
41
    /**
42
     * @param InputInterface $input
43
     * @return DaemonSettings
44
     * @throws InspectorSettingsException
45
     */
46
    public function fromConsoleInput(InputInterface $input): DaemonSettings
47
    {
48
        $threads = $input->getOption('threads');
49
        if (is_null($threads)) {
50
            $threads = 8;
51
        }
52
        $threads = filter_var($threads, FILTER_VALIDATE_INT);
53
        if ($threads === false) {
54
            throw DaemonSettingsException::create(DaemonSettingsException::THREADS_IS_NOT_INTEGER);
55
        }
56
57
        $target_regex = $input->getOption('target-regex') ?? DaemonSettings::TARGET_REGEX_DEFAULT;
58
        if (!is_string($target_regex)) {
59
            throw DaemonSettingsException::create(
60
                DaemonSettingsException::TARGET_REGEX_IS_NOT_STRING
61
            );
62
        }
63
64
        return new DaemonSettings('{' . $target_regex . '}', $threads);
65
    }
66
}
67