Functions::collection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace MinasRouter\Helpers;
4
5
use MinasRouter\Router\Route;
6
7
class Functions
8
{
9
    /**
10
     * Method responsible for returning the
11
     * active RouteCollection instance.
12
     * 
13
     * @return null|\MinasRouter\Router\RouteCollection
14
     */
15
    protected function collection()
16
    {
17
        return Route::$collection;
18
    }
19
20
    /**
21
     * Method responsible for returning a route.
22
     * 
23
     * @param string $name
24
     * 
25
     * @return null|\MinasRouter\Router\RouteManager
26
     */
27
    public function get(String $routerName)
28
    {
29
        return $this->collection()->getByName($routerName);
30
    }
31
32
    public function getStructuredRoute(String $routerName, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
    public function getStructuredRoute(String $routerName, /** @scrutinizer ignore-unused */ $params)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        $params = (array) func_get_arg(1);
35
36
        if(!$router = $this->get($routerName)) {
37
            return null;
38
        }
39
40
        $originalRoute = $router->getOriginalRoute();
41
42
        preg_match_all("/{\w+\??}/", $originalRoute, $matches);
43
  
44
        if(empty($matches[0])) {
45
            return $originalRoute;
46
        }
47
48
        if(count($matches[0]) != count($params)) {
49
            return null;
50
        }
51
52
        foreach($matches[0] as $index => $match) {
53
            $paramForReplace = isset($params[$index]) ? $params[$index] : 'undefined';
54
55
            $originalRoute = str_replace($match, $paramForReplace, $originalRoute);
56
        }
57
58
        return $originalRoute;
59
    }
60
61
    /**
62
     * Method responsible for returning the
63
     * current route.
64
     * 
65
     * @return null|\MinasRouter\Router\RouteManager
66
     */
67
    public function current()
68
    {
69
        return $this->collection()->getCurrentRoute();
70
    }
71
}