Completed
Pull Request — master (#20)
by Christoffer
02:40 queued 53s
created

ArgumentsTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getArgs() 0 3 1
A hasArgs() 0 3 1
A addArguments() 0 7 2
A setArgs() 0 12 2
A addArgument() 0 5 1
1
<?php
2
3
namespace Digia\GraphQL\Type\Definition;
4
5
use function Digia\GraphQL\Type\isAssocArray;
6
use function Digia\GraphQL\Util\invariant;
7
8
trait ArgumentsTrait
9
{
10
11
    /**
12
     * @var Argument[]
13
     */
14
    private $args = [];
15
16
    /**
17
     * @return bool
18
     */
19
    public function hasArgs(): bool
20
    {
21
        return !empty($this->args);
22
    }
23
24
    /**
25
     * @return Argument[]
26
     */
27
    public function getArgs(): array
28
    {
29
        return $this->args;
30
    }
31
32
    /**
33
     * @param Argument $arg
34
     * @return $this
35
     */
36
    protected function addArgument(Argument $arg)
37
    {
38
        $this->args[] = $arg;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param array $args
45
     * @return $this
46
     */
47
    protected function addArguments(array $args)
48
    {
49
        foreach ($args as $argument) {
50
            $this->addArgument($argument);
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param Argument[] $args
58
     * @return $this
59
     * @throws \Exception
60
     */
61
    protected function setArgs(array $args)
62
    {
63
        invariant(
64
            isAssocArray($args),
65
            'Args must be an associative array with argument names as keys.'
66
        );
67
68
        foreach ($args as $argName => $argConfig) {
69
            $this->addArgument(new Argument(array_merge($argConfig, ['name' => $argName])));
0 ignored issues
show
Bug introduced by
$argConfig of type Digia\GraphQL\Type\Definition\Argument is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

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

69
            $this->addArgument(new Argument(array_merge(/** @scrutinizer ignore-type */ $argConfig, ['name' => $argName])));
Loading history...
70
        }
71
72
        return $this;
73
    }
74
}
75