Passed
Pull Request — master (#9)
by Shinji
01:24
created

TraceLoopSettings::fromConsoleInput()   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;
15
16
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...
17
18
final class TraceLoopSettings
19
{
20
    private const SLEEP_NANO_SECONDS_DEFAULT = 1000 * 1000 * 10;
21
    private const CANCEL_KEY_DEFAULT = 'q';
22
    private const MAX_RETRY_DEFAULT = 10;
23
24
    public int $sleep_nano_seconds;
25
    public string $cancel_key;
26
    public int $max_retries;
27
28
    /**
29
     * TraceLoopSettings constructor.
30
     * @param int $sleep_nano_seconds
31
     * @param string $cancel_key
32
     * @param int $max_retries
33
     */
34
    public function __construct(int $sleep_nano_seconds, string $cancel_key, int $max_retries)
35
    {
36
        $this->sleep_nano_seconds = $sleep_nano_seconds;
37
        $this->cancel_key = $cancel_key;
38
        $this->max_retries = $max_retries;
39
    }
40
41
    /**
42
     * @param InputInterface $input
43
     * @return self
44
     * @throws InspectorSettingsException
45
     */
46
    public static function fromConsoleInput(InputInterface $input): self
47
    {
48
        $sleep_nano_seconds = $input->getOption('sleep-ns');
49
        if (is_null($sleep_nano_seconds)) {
50
            $sleep_nano_seconds = self::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 TraceLoopInspectorSettingsException::create(
55
                TraceLoopInspectorSettingsException::SLEEP_NS_IS_NOT_INTEGER
56
            );
57
        }
58
59
        $max_retries = $input->getOption('max-retries');
60
        if (is_null($max_retries)) {
61
            $max_retries = self::MAX_RETRY_DEFAULT;
62
        }
63
        $max_retries = filter_var($max_retries, FILTER_VALIDATE_INT);
64
        if ($max_retries === false) {
65
            throw TraceLoopInspectorSettingsException::create(
66
                TraceLoopInspectorSettingsException::MAX_RETRY_IS_NOT_INTEGER
67
            );
68
        }
69
70
        return new self($sleep_nano_seconds, self::CANCEL_KEY_DEFAULT, $max_retries);
71
    }
72
}
73