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

TargetProcessSettingsFromConsoleInput   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 18 1
A createSettings() 0 21 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\TargetProcessSettings;
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\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument 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\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...
21
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...
22
23
use function filter_var;
24
use function is_null;
25
26
use const FILTER_VALIDATE_INT;
27
28
final class TargetProcessSettingsFromConsoleInput
29
{
30
    /** @codeCoverageIgnore */
31
    public function setOptions(Command $command): void
32
    {
33
        $command
34
            ->addOption(
35
                'pid',
36
                'p',
37
                InputOption::VALUE_REQUIRED,
38
                'process id'
39
            )
40
            ->addArgument(
41
                'cmd',
42
                InputArgument::OPTIONAL,
43
                'command to execute as a target: either pid (via -p/--pid) or cmd must be specified'
44
            )
45
            ->addArgument(
46
                'args',
47
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
48
                'command line arguments for cmd',
49
            )
50
        ;
51
    }
52
53
    /**
54
     * @throws InspectorSettingsException
55
     */
56
    public function createSettings(InputInterface $input): TargetProcessSettings
57
    {
58
        $pid = NullableCast::toString($input->getOption('pid'));
59
        $command = NullableCast::toString($input->getArgument('cmd'));
60
        if (is_null($pid) and is_null($command)) {
61
            throw TargetProcessSettingsException::create(
62
                TargetProcessSettingsException::TARGET_NOT_SPECIFIED
63
            );
64
        }
65
        if (!is_null($pid)) {
66
            $pid = filter_var($pid, FILTER_VALIDATE_INT);
67
            if ($pid === false) {
68
                throw TargetProcessSettingsException::create(
69
                    TargetProcessSettingsException::TARGET_NOT_SPECIFIED
70
                );
71
            }
72
            return new TargetProcessSettings($pid);
73
        }
74
        /** @var list<string> $args */
75
        $args = $input->getArgument('args');
76
        return new TargetProcessSettings(null, $command, $args);
0 ignored issues
show
Bug introduced by
$args of type Reli\Inspector\Settings\TargetProcessSettings\list is incompatible with the type array expected by parameter $arguments of Reli\Inspector\Settings\...Settings::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return new TargetProcessSettings(null, $command, /** @scrutinizer ignore-type */ $args);
Loading history...
77
    }
78
}
79