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

TraceLoopSettingsFromConsoleInput::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 14
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\TraceLoopSettings;
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 TraceLoopSettingsFromConsoleInput
22
{
23
    public function setOptions(Command $command): void
24
    {
25
        $command
26
            ->addOption(
27
                'sleep-ns',
28
                's',
29
                InputOption::VALUE_OPTIONAL,
30
                'nanoseconds between traces (default: 1000 * 1000 * 10)'
31
            )
32
            ->addOption(
33
                'max-retries',
34
                'r',
35
                InputOption::VALUE_OPTIONAL,
36
                'max retries on contiguous errors of read (default: 10)'
37
            )
38
        ;
39
    }
40
41
    /**
42
     * @param InputInterface $input
43
     * @return TraceLoopSettings
44
     * @throws InspectorSettingsException
45
     */
46
    public function fromConsoleInput(InputInterface $input): TraceLoopSettings
47
    {
48
        $sleep_nano_seconds = $input->getOption('sleep-ns');
49
        if (is_null($sleep_nano_seconds)) {
50
            $sleep_nano_seconds = TraceLoopSettings::SLEEP_NANO_SECONDS_DEFAULT;
51
        }
52
        $sleep_nano_seconds = filter_var($sleep_nano_seconds, FILTER_VALIDATE_INT);
53
        if ($sleep_nano_seconds === false) {
54
            throw TraceLoopSettingsException::create(
55
                TraceLoopSettingsException::SLEEP_NS_IS_NOT_INTEGER
56
            );
57
        }
58
59
        $max_retries = $input->getOption('max-retries');
60
        if (is_null($max_retries)) {
61
            $max_retries = TraceLoopSettings::MAX_RETRY_DEFAULT;
62
        }
63
        $max_retries = filter_var($max_retries, FILTER_VALIDATE_INT);
64
        if ($max_retries === false) {
65
            throw TraceLoopSettingsException::create(
66
                TraceLoopSettingsException::MAX_RETRY_IS_NOT_INTEGER
67
            );
68
        }
69
70
        return new TraceLoopSettings($sleep_nano_seconds, TraceLoopSettings::CANCEL_KEY_DEFAULT, $max_retries);
71
    }
72
}
73