Completed
Push — prototype ( 3bfdd7...aec713 )
by Peter
07:47
created

AbstractRoute::hasPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
20
    RouteInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $name;
26
27
    /**
28
     * @var string
29
     */
30
    private $path;
31
32
    /**
33
     * @return bool
34
     */
35
    public function hasName()
36
    {
37
        return !empty($this->name);
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getName()
44
    {
45
        return $this->name;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function setName($name)
52
    {
53
        $this->name = (string) $name;
54
        return $this;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    protected function hasPath()
61
    {
62
        return !is_null($this->path);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    protected function getPath()
69
    {
70
        return $this->path;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function setPath($path)
77
    {
78
        $this->path = (string) $path;
79
        $path and $this->setRouteOption($path);
0 ignored issues
show
Bug introduced by
It seems like $path defined by parameter $path on line 76 can also be of type array; however, WebinoConfigLib\Router\A...Route::setRouteOption() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
80
        return $this;
81
    }
82
83
    /**
84
     * @param string $route
85
     * @return $this
86
     */
87
    protected function setRouteOption($route)
88
    {
89
        $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...
90
        return $this;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getType()
97
    {
98
        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...
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function setType($type = self::LITERAL)
105
    {
106
        $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...
107
        return $this;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function setMayTerminate($mayTerminate = true)
114
    {
115
        $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...
116
        return $this;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    protected function getDefaults()
123
    {
124
        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...
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 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...
131
    {
132
        $data = $this->getData();
133
        foreach ($defaults as $key => $value) {
134
            $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...
135
        }
136
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function setChild(array $routes)
143
    {
144
        foreach ($routes as $route) {
145
            $this->appendChildRoute($route);
146
        }
147
        return $this;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function chain(array $routes)
154
    {
155
        foreach ($routes as $route) {
156
            $this->appendRoute('chain_routes', $route);
157
        }
158
        return $this;
159
    }
160
161
    /**
162
     * @param array $handlers
163
     * @return $this
164
     */
165 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...
166
    {
167
        $data = $this->getData();
168
        foreach ($handlers as $key => $value) {
169
            $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...
170
        }
171
        return $this;
172
    }
173
174
    /**
175
     * @param string $section
176
     * @param self $route
177
     * @return $this
178
     */
179
    protected function appendRoute($section, self $route)
180
    {
181
        if ($route->hasName()) {
182
            $this->mergeArray([$section => [$route->getName() => $route->toArray()]]);
183
            return $this;
184
        }
185
186
        if ($this->getData()->offsetExists($section)) {
187
            $this->getData()->{$section}[] = $route->toArray();
188
            return $this;
189
        }
190
191
        $this->mergeArray([$section => [$route->toArray()]]);
192
        return $this;
193
    }
194
195
    /**
196
     * @param RouteInterface $route
197
     * @return $this
198
     */
199
    protected function appendChildRoute(RouteInterface $route)
200
    {
201
        if (!($route instanceof self)) {
202
            throw (new InvalidArgumentException('Expected route of type %s but got %s'))
203
                ->format(self::class, get_class($route));
204
        }
205
        $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...
206
        return $this;
207
    }
208
}
209