AbstractRoute   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 188
Duplicated Lines 8.51 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
dl 16
loc 188
rs 10
c 0
b 0
f 0
wmc 24
lcom 3
cbo 2

17 Methods

Rating   Name   Duplication   Size   Complexity  
A hasName() 0 4 1
A getName() 0 4 1
A setName() 0 5 1
A hasPath() 0 4 1
A getPath() 0 4 1
A setPath() 0 5 1
A setRouteOption() 0 5 1
A getType() 0 4 1
A setType() 0 5 1
A setMayTerminate() 0 5 1
A getDefaults() 0 4 1
A setDefaults() 8 8 2
A setChild() 0 7 2
A chain() 0 7 2
A setHandlers() 8 8 2
A appendRoute() 0 15 3
A appendChildRoute() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoConfigLib\Router;
12
13
use WebinoConfigLib\AbstractConfig;
14
use WebinoConfigLib\Exception\InvalidArgumentException;
15
16
/**
17
 * Class AbstractRoute
18
 */
19
abstract class AbstractRoute extends AbstractConfig implements RouteInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $name;
25
26
    /**
27
     * @var string
28
     */
29
    private $path;
30
31
    /**
32
     * @return bool
33
     */
34
    public function hasName()
35
    {
36
        return !empty($this->name);
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getName()
43
    {
44
        return $this->name;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function setName($name)
51
    {
52
        $this->name = (string) $name;
53
        return $this;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    protected function hasPath()
60
    {
61
        return !is_null($this->path);
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    protected function getPath()
68
    {
69
        return $this->path;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function setPath($path)
76
    {
77
        $this->path = (string) $path;
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $route
83
     * @return $this
84
     */
85
    protected function setRouteOption($route)
86
    {
87
        $this->getData()->options['route'] = (string) $route;
0 ignored issues
show
Bug introduced by
The property options does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
88
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getType()
95
    {
96
        return $this->getData()->type;
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function setType($type = self::LITERAL)
103
    {
104
        $this->getData()->type = (string) $type;
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
        return $this;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function setMayTerminate($mayTerminate = true)
112
    {
113
        $this->getData()->may_terminate = $mayTerminate;
0 ignored issues
show
Bug introduced by
The property may_terminate does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
114
        return $this;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    protected function getDefaults()
121
    {
122
        return $this->getData()->options['defaults'];
0 ignored issues
show
Bug introduced by
The property options does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 View Code Duplication
    public function setDefaults(array $defaults)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $data = $this->getData();
131
        foreach ($defaults as $key => $value) {
132
            $data->options['defaults'][$key] = $value;
0 ignored issues
show
Bug introduced by
The property options does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
        }
134
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function setChild(array $routes)
141
    {
142
        foreach ($routes as $route) {
143
            $this->appendChildRoute($route);
144
        }
145
        return $this;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function chain(array $routes)
152
    {
153
        foreach ($routes as $route) {
154
            $this->appendRoute('chain_routes', $route);
155
        }
156
        return $this;
157
    }
158
159
    /**
160
     * @param array $handlers
161
     * @return $this
162
     */
163 View Code Duplication
    protected function setHandlers(array $handlers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165
        $data = $this->getData();
166
        foreach ($handlers as $key => $value) {
167
            $data->options['defaults']['handlers'][$key] = $value;
0 ignored issues
show
Bug introduced by
The property options does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
168
        }
169
        return $this;
170
    }
171
172
    /**
173
     * @param string $section
174
     * @param self $route
175
     * @return $this
176
     */
177
    protected function appendRoute($section, self $route)
178
    {
179
        if ($route->hasName()) {
180
            $this->mergeArray([$section => [$route->getName() => $route->toArray()]]);
181
            return $this;
182
        }
183
184
        if ($this->getData()->offsetExists($section)) {
185
            $this->getData()->{$section}[] = $route->toArray();
186
            return $this;
187
        }
188
189
        $this->mergeArray([$section => [$route->toArray()]]);
190
        return $this;
191
    }
192
193
    /**
194
     * @param RouteInterface $route
195
     * @return $this
196
     */
197
    protected function appendChildRoute(RouteInterface $route)
198
    {
199
        if (!($route instanceof self)) {
200
            throw (new InvalidArgumentException('Expected route of type %s but got %s'))
201
                ->format(self::class, get_class($route));
202
        }
203
        $this->appendRoute('child_routes', $route);
0 ignored issues
show
Documentation introduced by
$route is of type object<WebinoConfigLib\Router\AbstractRoute>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
        return $this;
205
    }
206
}
207