Args::prepare()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Quma;
6
7
use Conia\Quma\Util;
8
9
/**
10
 * @psalm-type ArgsArray = list<mixed>|array<non-empty-string, mixed>
11
 */
12
class Args
13
{
14
    protected ArgType $type;
15
    protected int $count;
16
17
    /** @psalm-var ArgsArray */
18
    protected readonly array $args;
19
20 60
    public function __construct(array $args)
21
    {
22 60
        $this->args = $this->prepare($args);
23
    }
24
25
    /** @psalm-return ArgsArray */
26 25
    public function get(): array
27
    {
28 25
        return $this->args;
29
    }
30
31 59
    public function count(): int
32
    {
33 59
        return $this->count;
34
    }
35
36 26
    public function type(): ArgType
37
    {
38 26
        return $this->type;
39
    }
40
41 60
    protected function prepare(array $args): array
42
    {
43 60
        $this->count = count($args);
44
45 60
        if ($this->count === 1 && is_array($args[0])) {
46 23
            if (Util::isAssoc($args[0])) {
47 22
                $this->type = ArgType::Named;
48
            } else {
49 3
                $this->type = ArgType::Positional;
50
            }
51
52 23
            return $args[0];
53
        }
54
55 48
        $this->type = ArgType::Positional;
56
57 48
        return $args;
58
    }
59
}
60