Passed
Push — 0.6.x ( 9a1948...a98caa )
by Shinji
03:23 queued 01:32
created

DaemonSettingsFromConsoleInput   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 46
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 14 1
A createSettings() 0 22 5
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof 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 Reli\Inspector\Settings\DaemonSettings;
15
16
use PhpCast\NullableCast;
0 ignored issues
show
Bug introduced by
The type PhpCast\NullableCast 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...
17
use Reli\Inspector\Settings\InspectorSettingsException;
18
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...
19
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...
20
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...
21
22
use function filter_var;
23
use function is_null;
24
25
use const FILTER_VALIDATE_INT;
26
27
final class DaemonSettingsFromConsoleInput
28
{
29
    /** @codeCoverageIgnore */
30
    public function setOptions(Command $command): void
31
    {
32
        $command
33
            ->addOption(
34
                'target-regex',
35
                'P',
36
                InputOption::VALUE_REQUIRED,
37
                'regex to find target processes which have matching command-line (required)'
38
            )
39
            ->addOption(
40
                'threads',
41
                'T',
42
                InputOption::VALUE_OPTIONAL,
43
                'number of workers (default: 8)'
44
            )
45
        ;
46
    }
47
48
    /**
49
     * @throws InspectorSettingsException
50
     */
51
    public function createSettings(InputInterface $input): DaemonSettings
52
    {
53
        $threads = NullableCast::toString($input->getOption('threads'));
54
        if (is_null($threads)) {
55
            $threads = 8;
56
        }
57
        $threads = filter_var($threads, FILTER_VALIDATE_INT);
58
        if ($threads === false) {
59
            throw DaemonSettingsException::create(DaemonSettingsException::THREADS_IS_NOT_INTEGER);
60
        }
61
62
        $target_regex = $input->getOption('target-regex');
63
        if (is_null($target_regex)) {
64
            throw DaemonSettingsException::create(DaemonSettingsException::TARGET_REGEX_IS_NOT_SPECIFIED);
65
        }
66
        if (!is_string($target_regex)) {
67
            throw DaemonSettingsException::create(
68
                DaemonSettingsException::TARGET_REGEX_IS_NOT_STRING
69
            );
70
        }
71
72
        return new DaemonSettings('{' . $target_regex . '}', $threads);
73
    }
74
}
75