Completed
Push — master ( dacaa0...701f1f )
by Vítor
02:08
created

RouteCollectionBuilder::processRoutes()   D

Complexity

Conditions 9
Paths 49

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 4.909
cc 9
eloc 19
nc 49
nop 3
1
<?php
2
/**
3
 * ZfDebugModule. Console commands and other utilities for debugging ZF2 apps.
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
6
 * @copyright 2016 Vítor Brandão <[email protected]>
7
 */
8
9
namespace Noiselabs\ZfDebugModule\Util\Routing;
10
11
use Zend\Mvc\Router\RouteInterface;
12
13
class RouteCollectionBuilder
14
{
15
    /**
16
     * @var RouteInterface
17
     */
18
    private $routes;
19
20
    /**
21
     * RoutesInspector constructor.
22
     *
23
     * @param array $routes
24
     */
25
    public function __construct(array $routes = [])
26
    {
27
        $this->routes = $routes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $routes of type array is incompatible with the declared type object<Zend\Mvc\Router\RouteInterface> of property $routes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
    }
29
30
    /**
31
     * @return RouteCollection
32
     */
33
    public function build()
34
    {
35
        $routeCollection = new FlatRouteCollection();
36
        $this->processRoutes($routeCollection, $this->routes);
0 ignored issues
show
Documentation introduced by
$this->routes is of type object<Zend\Mvc\Router\RouteInterface>, but the function expects a array.

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...
37
38
        return $routeCollection;
39
    }
40
41
    /**
42
     * @param FlatRouteCollection $routeCollection
43
     * @param array               $routesConfig
44
     * @param array               $parentRoutesConfig
45
     */
46
    private function processRoutes(FlatRouteCollection $routeCollection, array $routesConfig, array $parentRoutesConfig = null)
47
    {
48
        foreach (array_keys($routesConfig) as $k) {
49
            $routeName = $k;
50
            $routeUrl = isset($routesConfig[$k]['options']['route']) ? $routesConfig[$k]['options']['route'] : '';
51
52
            if (!empty($parentRoutesConfig)) {
53
                $pk = key($parentRoutesConfig);
54
55
                $routeName = $pk . '/' . $routeName;
56
                if (isset($parentRoutesConfig[$pk]['options']['route'])) {
57
                    $routeUrl = $parentRoutesConfig[$pk]['options']['route'] . $routeUrl;
58
                }
59
            }
60
61
            $controller = isset($routesConfig[$k]['options']['defaults']['controller'])
62
                ? $routesConfig[$k]['options']['defaults']['controller'] : null;
63
            $action = isset($routesConfig[$k]['options']['defaults']['action'])
64
                ? $routesConfig[$k]['options']['defaults']['action'] : null;
65
            $route = new Route($routeName, $routeUrl, $controller, $action);
66
            $routeCollection->addRoute($route);
67
68
            if (isset($routesConfig[$k]['child_routes']) && !empty($routesConfig[$k]['child_routes'])) {
69
                $childRoutes = $routesConfig[$k]['child_routes'];
70
                unset($routesConfig[$k]['child_routes']);
71
                $this->processRoutes($routeCollection, $childRoutes, [$k => $routesConfig[$k]]);
72
            }
73
        }
74
    }
75
}
76