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

DaemonSettingsFromConsoleInput   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 23
c 1
b 0
f 0
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 14 1
A createSettings() 0 19 4
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
    /**
24
     * @codeCoverageIgnore
25
     */
26
    public function setOptions(Command $command): void
27
    {
28
        $command
29
            ->addOption(
30
                'threads',
31
                'T',
32
                InputOption::VALUE_OPTIONAL,
33
                'number of workers (default: 8)'
34
            )
35
            ->addOption(
36
                'target-regex',
37
                'P',
38
                InputOption::VALUE_OPTIONAL,
39
                'regex to find the php binary loaded in the target process'
40
            )
41
        ;
42
    }
43
44
    /**
45
     * @param InputInterface $input
46
     * @return DaemonSettings
47
     * @throws InspectorSettingsException
48
     */
49
    public function createSettings(InputInterface $input): DaemonSettings
50
    {
51
        $threads = $input->getOption('threads');
52
        if (is_null($threads)) {
53
            $threads = 8;
54
        }
55
        $threads = filter_var($threads, FILTER_VALIDATE_INT);
56
        if ($threads === false) {
57
            throw DaemonSettingsException::create(DaemonSettingsException::THREADS_IS_NOT_INTEGER);
58
        }
59
60
        $target_regex = $input->getOption('target-regex') ?? DaemonSettings::TARGET_REGEX_DEFAULT;
61
        if (!is_string($target_regex)) {
62
            throw DaemonSettingsException::create(
63
                DaemonSettingsException::TARGET_REGEX_IS_NOT_STRING
64
            );
65
        }
66
67
        return new DaemonSettings('{' . $target_regex . '}', $threads);
68
    }
69
}
70