Completed
Push — master ( 5ac91e...39483e )
by Kirill
38:30
created

BaseArgumentsContainer::hasArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Base\Dependent\Argument;
11
12
use Railt\SDL\Contracts\Dependent\Argument\HasArguments;
13
use Railt\SDL\Contracts\Dependent\ArgumentDefinition;
14
15
/**
16
 * Trait BaseArgumentsContainer
17
 * @mixin HasArguments
18
 */
19
trait BaseArgumentsContainer
20
{
21
    /**
22
     * @var array|ArgumentDefinition[]
23
     */
24
    protected $arguments = [];
25
26
    /**
27
     * @return iterable|ArgumentDefinition[]
28
     */
29 1742
    public function getArguments(): iterable
30
    {
31 1742
        return \array_values($this->arguments);
32
    }
33
34
    /**
35
     * @param ArgumentDefinition $argument
36
     * @return void
37
     */
38
    public function addArgument(ArgumentDefinition $argument): void
39
    {
40
        $this->arguments[$argument->getName()] = $argument;
41
    }
42
43
    /**
44
     * @param string $name
45
     * @return bool
46
     */
47 680
    public function hasArgument(string $name): bool
48
    {
49 680
        return \array_key_exists($name, $this->arguments);
50
    }
51
52
    /**
53
     * @param string $name
54
     * @return null|ArgumentDefinition
55
     */
56 453
    public function getArgument(string $name): ?ArgumentDefinition
57
    {
58 453
        return $this->arguments[$name] ?? null;
59
    }
60
61
    /**
62
     * @return int
63
     */
64 42
    public function getNumberOfArguments(): int
65
    {
66 42
        return \count($this->arguments);
67
    }
68
69
    /**
70
     * @return int
71
     */
72 42
    public function getNumberOfRequiredArguments(): int
73
    {
74 42
        return (int)\array_reduce($this->arguments, [$this, 'requiredArgumentsCounter'], 0);
75
    }
76
77
    /**
78
     * @return int
79
     */
80 42
    public function getNumberOfOptionalArguments(): int
81
    {
82 42
        return (int)\array_reduce($this->arguments, [$this, 'optionalArgumentsCounter'], 0);
83
    }
84
85
    /**
86
     * @param int $carry
87
     * @param ArgumentDefinition $argument
88
     * @return int
89
     */
90 6
    private function optionalArgumentsCounter(int $carry, ArgumentDefinition $argument): int
91
    {
92 6
        return $carry + (int)$argument->hasDefaultValue();
93
    }
94
95
    /**
96
     * @param int $carry
97
     * @param ArgumentDefinition $argument
98
     * @return int
99
     */
100 6
    private function requiredArgumentsCounter(int $carry, ArgumentDefinition $argument): int
101
    {
102 6
        return $carry + (int)! $argument->hasDefaultValue();
103
    }
104
}
105