RouteCollection::getBaseCodeRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Route;
15
16
use Symfony\Component\Routing\Route;
17
18
/**
19
 * @author Thomas Rabaix <[email protected]>
20
 */
21
final class RouteCollection implements RouteCollectionInterface
22
{
23
    /**
24
     * @var Route[]
25
     */
26
    private $elements = [];
27
28
    /**
29
     * @var string
30
     */
31
    private $baseCodeRoute;
32
33
    /**
34
     * @var string
35
     */
36
    private $baseRouteName;
37
38
    /**
39
     * @var string
40
     */
41
    private $baseControllerName;
42
43
    /**
44
     * @var string
45
     */
46
    private $baseRoutePattern;
47
48
    /**
49
     * @var Route[]
50
     */
51
    private $cachedElements = [];
52
53
    public function __construct(
54
        string $baseCodeRoute,
55
        string $baseRouteName,
56
        string $baseRoutePattern,
57
        string $baseControllerName
58
    ) {
59
        $this->baseCodeRoute = $baseCodeRoute;
60
        $this->baseRouteName = $baseRouteName;
61
        $this->baseRoutePattern = $baseRoutePattern;
62
        $this->baseControllerName = $baseControllerName;
63
    }
64
65
    public function add(
66
        string $name,
67
        ?string $pattern = null,
68
        array $defaults = [],
69
        array $requirements = [],
70
        array $options = [],
71
        string $host = '',
72
        array $schemes = [],
73
        array $methods = [],
74
        string $condition = ''
75
    ): RouteCollectionInterface {
76
        $pattern = sprintf('%s/%s', $this->baseRoutePattern, $pattern ?: $name);
77
        $code = $this->getCode($name);
78
        $routeName = sprintf('%s_%s', $this->baseRouteName, $name);
79
80
        if (!isset($defaults['_controller'])) {
81
            $actionJoiner = false === strpos($this->baseControllerName, '\\') ? ':' : '::';
82
            if (':' !== $actionJoiner && false !== strpos($this->baseControllerName, ':')) {
83
                $actionJoiner = ':';
84
            }
85
86
            $defaults['_controller'] = $this->baseControllerName.$actionJoiner.$this->actionify($code);
87
        }
88
89
        if (!isset($defaults['_sonata_admin'])) {
90
            $defaults['_sonata_admin'] = $this->baseCodeRoute;
91
        }
92
93
        $defaults['_sonata_name'] = $routeName;
94
95
        $element = static function () use ($pattern, $defaults, $requirements, $options, $host, $schemes, $methods, $condition) {
96
            return new Route($pattern, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
97
        };
98
        $this->addElement($code, $element);
99
100
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sonata\AdminBundle\Route\RouteCollection) is incompatible with the return type declared by the interface Sonata\AdminBundle\Route...ollectionInterface::add of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
101
    }
102
103
    public function getCode(string $name): string
104
    {
105
        if (false !== strrpos($name, '.')) {
106
            return $name;
107
        }
108
109
        return sprintf('%s.%s', $this->baseCodeRoute, $name);
110
    }
111
112
    public function addCollection(RouteCollectionInterface $collection): RouteCollectionInterface
113
    {
114
        foreach ($collection->getElements() as $code => $element) {
115
            $this->addElement($code, $element);
0 ignored issues
show
Documentation introduced by
$element is of type object<Symfony\Component\Routing\Route>, but the function expects a callable.

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...
116
        }
117
118
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sonata\AdminBundle\Route\RouteCollection) is incompatible with the return type declared by the interface Sonata\AdminBundle\Route...nterface::addCollection of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
119
    }
120
121
    public function getElements(): array
122
    {
123
        foreach ($this->elements as $code => $element) {
124
            $this->resolveElement($code);
125
        }
126
127
        return $this->elements;
128
    }
129
130
    public function has(string $name): bool
131
    {
132
        return \array_key_exists($this->getCode($name), $this->elements);
133
    }
134
135
    public function hasCached(string $name): bool
136
    {
137
        return \array_key_exists($this->getCode($name), $this->cachedElements);
138
    }
139
140
    public function get(string $name): Route
141
    {
142
        if ($this->has($name)) {
143
            $code = $this->getCode($name);
144
            $this->resolveElement($code);
145
146
            return $this->elements[$code];
147
        }
148
149
        throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
150
    }
151
152
    public function remove(string $name): RouteCollectionInterface
153
    {
154
        unset($this->elements[$this->getCode($name)]);
155
156
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sonata\AdminBundle\Route\RouteCollection) is incompatible with the return type declared by the interface Sonata\AdminBundle\Route...ectionInterface::remove of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
157
    }
158
159
    public function restore(string $name): RouteCollectionInterface
160
    {
161
        if ($this->hasCached($name)) {
162
            $code = $this->getCode($name);
163
            $this->addElement($code, $this->cachedElements[$code]);
0 ignored issues
show
Documentation introduced by
$this->cachedElements[$code] is of type object<Symfony\Component\Routing\Route>, but the function expects a callable.

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...
164
165
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sonata\AdminBundle\Route\RouteCollection) is incompatible with the return type declared by the interface Sonata\AdminBundle\Route...ctionInterface::restore of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
166
        }
167
168
        throw new \InvalidArgumentException(sprintf('Element "%s" does not exist in cache.', $name));
169
    }
170
171
    public function clearExcept($routeList): RouteCollectionInterface
172
    {
173
        if (!\is_array($routeList)) {
174
            $routeList = [$routeList];
175
        }
176
177
        $routeCodeList = [];
178
        foreach ($routeList as $name) {
179
            $routeCodeList[] = $this->getCode($name);
180
        }
181
182
        $elements = $this->elements;
183
        foreach ($elements as $code => $element) {
184
            if (!\in_array($code, $routeCodeList, true)) {
185
                unset($this->elements[$code]);
186
            }
187
        }
188
189
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sonata\AdminBundle\Route\RouteCollection) is incompatible with the return type declared by the interface Sonata\AdminBundle\Route...nInterface::clearExcept of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
190
    }
191
192
    public function clear(): RouteCollectionInterface
193
    {
194
        $this->elements = [];
195
196
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sonata\AdminBundle\Route\RouteCollection) is incompatible with the return type declared by the interface Sonata\AdminBundle\Route...lectionInterface::clear of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
197
    }
198
199
    public function actionify(string $action): string
200
    {
201
        if (false !== ($pos = strrpos($action, '.'))) {
202
            $action = substr($action, $pos + 1);
203
        }
204
205
        // if this is a service rather than just a controller name, the suffix
206
        // Action is not automatically appended to the method name
207
        if (false === strpos($this->baseControllerName, ':')) {
208
            $action .= 'Action';
209
        }
210
211
        return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', '  '))));
212
    }
213
214
    public function getBaseCodeRoute(): string
215
    {
216
        return $this->baseCodeRoute;
217
    }
218
219
    public function getBaseControllerName(): string
220
    {
221
        return $this->baseControllerName;
222
    }
223
224
    public function getBaseRouteName(): string
225
    {
226
        return $this->baseRouteName;
227
    }
228
229
    public function getBaseRoutePattern(): string
230
    {
231
        return $this->baseRoutePattern;
232
    }
233
234
    /**
235
     * @param Route|callable $element
236
     */
237
    private function addElement(string $code, $element): void
238
    {
239
        $this->elements[$code] = $element;
240
        $this->updateCachedElement($code);
241
    }
242
243
    private function updateCachedElement(string $code): void
244
    {
245
        $this->cachedElements[$code] = $this->elements[$code];
246
    }
247
248
    private function resolveElement(string $code): void
249
    {
250
        $element = $this->elements[$code];
251
252
        if (\is_callable($element)) {
253
            $this->elements[$code] = $element();
254
            $this->updateCachedElement($code);
255
        }
256
    }
257
}
258