Passed
Push — main ( 950f85...4bfa5d )
by Greg
07:33
created

AbstractCommand::boolOption()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Cli\Commands;
21
22
use InvalidArgumentException;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputInterface;
25
26
use function is_bool;
27
use function is_string;
28
29
abstract class AbstractCommand extends Command
30
{
31
    protected function boolOption(InputInterface $input, string $name): bool
32
    {
33
        $value = $input->getOption(name: $name);
34
35
        if ($value === null || is_bool($value)) {
36
            return (bool) $value;
37
        }
38
39
        throw new InvalidArgumentException(message: 'Argument must be bool : ' . $name);
40
    }
41
42
    protected function stringArgument(InputInterface $input, string $name): string
43
    {
44
        $value = $input->getArgument(name: $name);
45
46
        if ($value === null || is_string($value)) {
47
            return (string) $value;
48
        }
49
50
        throw new InvalidArgumentException(message: 'Argument must be string: ' . $name);
51
    }
52
53
    protected function stringOption(InputInterface $input, string $name): string
54
    {
55
        $value = $input->getOption(name: $name);
56
57
        if ($value === null || is_string($value)) {
58
            return (string) $value;
59
        }
60
61
        throw new InvalidArgumentException(message: 'Argument must be string : ' . $name);
62
    }
63
}
64