Completed
Push — develop ( 8109ce...b15e90 )
by Alejandro
17s queued 12s
created

AbstractWithDateRangeCommand::getDateOption()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 21
ccs 11
cts 12
cp 0.9167
rs 9.8333
c 1
b 0
f 0
cc 4
nc 4
nop 3
crap 4.0092
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\CLI\Command\Util;
6
7
use Cake\Chronos\Chronos;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Throwable;
13
14
use function sprintf;
15
16
abstract class AbstractWithDateRangeCommand extends Command
17
{
18 20
    final protected function configure(): void
19
    {
20 20
        $this->doConfigure();
21
        $this
22 20
            ->addOption('startDate', 's', InputOption::VALUE_REQUIRED, $this->getStartDateDesc())
23 20
            ->addOption('endDate', 'e', InputOption::VALUE_REQUIRED, $this->getEndDateDesc());
24 20
    }
25
26 20
    protected function getDateOption(InputInterface $input, OutputInterface $output, string $key): ?Chronos
27
    {
28 20
        $value = $input->getOption($key);
29 20
        if (empty($value)) {
30 18
            return null;
31
        }
32
33
        try {
34 5
            return Chronos::parse($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string[]; however, parameter $time of Cake\Chronos\Chronos::parse() does only seem to accept DateTimeInterface|string, maybe add an additional type check? ( Ignorable by Annotation )

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

34
            return Chronos::parse(/** @scrutinizer ignore-type */ $value);
Loading history...
35 1
        } catch (Throwable $e) {
36 1
            $output->writeln(sprintf(
37
                '<comment>> Ignored provided "%s" since its value "%s" is not a valid date. <</comment>',
38 1
                $key,
39 1
                $value,
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

39
                /** @scrutinizer ignore-type */ $value,
Loading history...
40
            ));
41
42 1
            if ($output->isVeryVerbose()) {
43
                $this->getApplication()->renderThrowable($e, $output);
44
            }
45
46 1
            return null;
47
        }
48
    }
49
50
    abstract protected function doConfigure(): void;
51
52
    abstract protected function getStartDateDesc(): string;
53
    abstract protected function getEndDateDesc(): string;
54
}
55