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

createSettings()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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