TraceLoopSettingsFromConsoleInput::setOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 20
rs 9.7333
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\TraceLoopSettings;
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 PhpProfiler\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_NULL_ON_FAILURE;
26
use const FILTER_VALIDATE_BOOLEAN;
27
use const FILTER_VALIDATE_INT;
28
29
final class TraceLoopSettingsFromConsoleInput
30
{
31
    /** @codeCoverageIgnore */
32
    public function setOptions(Command $command): void
33
    {
34
        $command
35
            ->addOption(
36
                'sleep-ns',
37
                's',
38
                InputOption::VALUE_OPTIONAL,
39
                'nanoseconds between traces (default: 1000 * 1000 * 10)'
40
            )
41
            ->addOption(
42
                'max-retries',
43
                'r',
44
                InputOption::VALUE_OPTIONAL,
45
                'max retries on contiguous errors of read (default: 10)'
46
            )
47
            ->addOption(
48
                'stop-process',
49
                'S',
50
                InputOption::VALUE_OPTIONAL,
51
                'stop the target process while reading its trace (default: off)'
52
            )
53
        ;
54
    }
55
56
    /**
57
     * @throws InspectorSettingsException
58
     */
59
    public function createSettings(InputInterface $input): TraceLoopSettings
60
    {
61
        $sleep_nano_seconds = NullableCast::toString($input->getOption('sleep-ns'));
62
        if (is_null($sleep_nano_seconds)) {
63
            $sleep_nano_seconds = TraceLoopSettings::SLEEP_NANO_SECONDS_DEFAULT;
64
        }
65
        $sleep_nano_seconds = filter_var($sleep_nano_seconds, FILTER_VALIDATE_INT);
66
        if ($sleep_nano_seconds === false) {
67
            throw TraceLoopSettingsException::create(
68
                TraceLoopSettingsException::SLEEP_NS_IS_NOT_INTEGER
69
            );
70
        }
71
72
        $max_retries = NullableCast::toString($input->getOption('max-retries'));
73
        if (is_null($max_retries)) {
74
            $max_retries = TraceLoopSettings::MAX_RETRY_DEFAULT;
75
        }
76
        $max_retries = filter_var($max_retries, FILTER_VALIDATE_INT);
77
        if ($max_retries === false) {
78
            throw TraceLoopSettingsException::create(
79
                TraceLoopSettingsException::MAX_RETRY_IS_NOT_INTEGER
80
            );
81
        }
82
83
        $stop_process = NullableCast::toString($input->getOption('stop-process'));
84
        if (is_null($stop_process)) {
85
            $stop_process = false;
86
        }
87
        $stop_process = filter_var($stop_process, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
88
        if ($stop_process === null) {
89
            throw TraceLoopSettingsException::create(
90
                TraceLoopSettingsException::STOP_PROCESS_IS_NOT_BOOLEAN
91
            );
92
        }
93
94
        return new TraceLoopSettings(
95
            $sleep_nano_seconds,
96
            TraceLoopSettings::CANCEL_KEY_DEFAULT,
97
            $max_retries,
98
            $stop_process,
99
        );
100
    }
101
}
102