DirectivesAwareConfigTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 84
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A buildDirectives() 0 11 3
A addDirectives() 0 13 3
A addDirective() 0 9 2
A getDirective() 0 4 2
A hasDirective() 0 4 1
A hasDirectives() 0 4 1
A getDirectives() 0 4 1
A removeDirective() 0 8 2
1
<?php
2
/**
3
 * Date: 03/17/2017
4
 *
5
 * @author Volodymyr Rashchepkin <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Config\Traits;
9
10
11
use Youshido\GraphQL\Directive\Directive;
12
use Youshido\GraphQL\Field\InputField;
13
14
trait DirectivesAwareConfigTrait
15
{
16
    protected $directives = [];
17
    protected $_isDirectivesBuilt;
18
19
    public function buildDirectives()
20
    {
21
        if ($this->_isDirectivesBuilt) {
22
            return;
23
        }
24
25
        if (!empty($this->data['directives'])) {
26
            $this->addDirectives($this->data['directives']);
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        }
28
        $this->_isDirectivesBuilt = true;
29
    }
30
31
    public function addDirectives($directiveList)
32
    {
33
        foreach ($directiveList as $directiveName => $directiveInfo) {
34
            if ($directiveInfo instanceof Directive) {
35
                $this->directives[$directiveInfo->getName()] = $directiveInfo;
36
                continue;
37
            } else {
38
                $this->addDirective($directiveName, $this->buildConfig($directiveName, $directiveInfo));
0 ignored issues
show
Bug introduced by
It seems like buildConfig() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
            }
40
        }
41
42
        return $this;
43
    }
44
45
    public function addDirective($directive, $directiveInfo = null)
46
    {
47
        if (!($directive instanceof Directive)) {
48
            $directive = new Directive($this->buildConfig($directive, $directiveInfo));
0 ignored issues
show
Bug introduced by
It seems like buildConfig() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
        }
50
        $this->directives[$directive->getName()] = $directive;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param $name
57
     *
58
     * @return InputField
59
     */
60
    public function getDirective($name)
61
    {
62
        return $this->hasDirective($name) ? $this->directives[$name] : null;
63
    }
64
65
    /**
66
     * @param $name
67
     *
68
     * @return bool
69
     */
70
    public function hasDirective($name)
71
    {
72
        return array_key_exists($name, $this->directives);
73
    }
74
75
    public function hasDirectives()
76
    {
77
        return !empty($this->directives);
78
    }
79
80
    /**
81
     * @return InputField[]
82
     */
83
    public function getDirectives()
84
    {
85
        return $this->directives;
86
    }
87
88
    public function removeDirective($name)
89
    {
90
        if ($this->hasDirective($name)) {
91
            unset($this->directives[$name]);
92
        }
93
94
        return $this;
95
    }
96
97
}
98