Completed
Pull Request — master (#104)
by
unknown
07:23
created

TRegistry   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 23
c 4
b 1
f 1
lcom 1
cbo 2
dl 0
loc 157
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultComponents() 0 4 1
A getComponents() 0 7 2
A getComponentByName() 0 8 3
B getComponentByNameRecursive() 0 15 6
A getTagged() 0 8 2
A addComponent() 0 6 1
A setComponents() 0 8 2
A addComponents() 0 7 1
A makeComponent() 0 6 1
A initializeComponents() 0 6 2
A prepareComponents() 0 6 2
1
<?php
2
namespace Nayjest\Grids\Components\Base;
3
4
use Illuminate\Support\Collection;
5
use Nayjest\Grids\Grid;
6
7
trait TRegistry
8
{
9
    protected $components;
10
11
    /**
12
     * Returns default child components.
13
     *
14
     * Override this method.
15
     *
16
     * @return \Illuminate\Support\Collection|ComponentInterface[]|array
17
     */
18
    protected function getDefaultComponents()
19
    {
20
        return [];
21
    }
22
23
    /**
24
     * Returns child components.
25
     *
26
     * @return Collection|ComponentInterface[]
27
     */
28
    final public function getComponents()
29
    {
30
        if ($this->components === null) {
31
            $this->setComponents($this->getDefaultComponents());
32
        }
33
        return $this->components;
34
    }
35
36
    /**
37
     * Finds child component by name.
38
     *
39
     * @param string $name
40
     * @return null|ComponentInterface
41
     */
42
    public function getComponentByName($name)
43
    {
44
        foreach ($this->getComponents() as $component) {
45
            if ($component->getName() === $name) {
46
                return $component;
47
            }
48
        }
49
    }
50
51
    /**
52
     * Finds child component by name recursively.
53
     *
54
     * @param string $name
55
     * @return null|ComponentInterface
56
     */
57
    public function getComponentByNameRecursive($name)
58
    {
59
        foreach ($this->getComponents() as $component) {
60
            if ($component->getName() === $name) {
61
                return $component;
62
            }
63
            if ($component instanceof TRegistry || $component instanceof RegistryInterface) {
64
                if ($res = $component->getComponentByNameRecursive($name)) {
0 ignored issues
show
Bug introduced by
The method getComponentByNameRecursive does only exist in Nayjest\Grids\Components\Base\TRegistry, but not in Nayjest\Grids\Components\Base\RegistryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65
                    return $res;
66
                }
67
            }
68
69
        }
70
        return null;
71
    }
72
73
    /**
74
     * @param string|string[] $tagNames
75
     * @return Collection|ComponentInterface[]
76
     */
77
    public function getTagged($tagNames)
78
    {
79
        return $this->getComponents()->filter(
80
            function (ComponentInterface $component) use ($tagNames) {
81
                return is_array($tagNames) ? $component->hasTags($tagNames) : $component->hasTag($tagNames);
82
            }
83
        );
84
    }
85
86
    /**
87
     * Adds component to the collection of child components.
88
     *
89
     * @param ComponentInterface $component
90
     * @return $this
91
     */
92
    public function addComponent(ComponentInterface $component)
93
    {
94
        $this->getComponents()->push($component);
95
        $component->attachTo($this);
96
        return $this;
97
    }
98
99
    /**
100
     * Allows to specify collection of child components.
101
     *
102
     * @param \Illuminate\Support\Collection|ComponentInterface[]|array $components
103
     * @return $this
104
     */
105
    public function setComponents($components)
106
    {
107
        $this->components = Collection::make($components);
108
        foreach ($components as $component) {
109
            $component->attachTo($this);
110
        }
111
        return $this;
112
    }
113
114
    /**
115
     * Adds set of components to the collection of child components.
116
     *
117
     * @param  Collection|\Illuminate\Support\Contracts\ArrayableInterface|array  $components
118
     * @return $this
119
     */
120
    public function addComponents($components)
121
    {
122
        $this->setComponents(
123
            $this->getComponents()->merge($components)
124
        );
125
        return $this;
126
    }
127
128
    /**
129
     * Creates component,
130
     * adds it to child components collection and returns it.
131
     *
132
     * @param string $class
133
     * @return ComponentInterface
134
     */
135
    public function makeComponent($class)
136
    {
137
        $component = new $class;
138
        $this->addComponent($component);
139
        return $component;
140
    }
141
142
    /**
143
     * Initializes child components.
144
     *
145
     * @param Grid $grid
146
     */
147
    public function initializeComponents(Grid $grid)
148
    {
149
        foreach ($this->getComponents() as $component) {
150
            $component->initialize($grid);
151
        }
152
    }
153
154
    /**
155
     * Prepares child components for rendering.
156
     */
157
    public function prepareComponents()
158
    {
159
        foreach ($this->getComponents() as $component) {
160
            $component->prepare();
161
        }
162
    }
163
}
164