RouteFactory::preprocessPathString()   C
last analyzed

Complexity

Conditions 14
Paths 20

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 23
nc 20
nop 1
dl 0
loc 41
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Klein (klein.php) - A fast & flexible router for PHP
4
 *
5
 * @author      Chris O'Hara <[email protected]>
6
 * @author      Trevor Suarez (Rican7) (contributor and v2 refactorer)
7
 * @copyright   (c) Chris O'Hara
8
 * @link        https://github.com/klein/klein.php
9
 * @license     MIT
10
 */
11
12
namespace app\framework\Component\Routing;
13
14
/**
15
 * RouteFactory
16
 *
17
 * The default implementation of the AbstractRouteFactory
18
 * @package app\framework\Component\Routing
19
 */
20
class RouteFactory extends AbstractRouteFactory
21
{
22
23
    /**
24
     * Constants
25
     */
26
27
    /**
28
     * The value given to path's when they are entered as null values
29
     *
30
     * @type string
31
     */
32
    const NULL_PATH_VALUE = '*';
33
34
35
    /**
36
     * Methods
37
     */
38
39
    /**
40
     * Check if the path is null or equal to our match-all, null-like value
41
     *
42
     * @param mixed $path
43
     * @return boolean
44
     */
45
    protected function pathIsNull($path)
46
    {
47
        return (static::NULL_PATH_VALUE === $path || null === $path);
48
    }
49
50
    /**
51
     * Quick check to see whether or not to count the route
52
     * as a match when counting total matches
53
     *
54
     * @param string $path
55
     * @return boolean
56
     */
57
    protected function shouldPathStringCauseRouteMatch($path)
58
    {
59
        // Only consider a request to be matched when not using 'matchall'
60
        return !$this->pathIsNull($path);
61
    }
62
63
    /**
64
     * Pre-process a path string
65
     *
66
     * This method wraps the path string in a regular expression syntax baesd
67
     * on whether the string is a catch-all or custom regular expression.
68
     * It also adds the namespace in a specific part, based on the style of expression
69
     *
70
     * @param string $path
71
     * @return string
72
     */
73
    protected function preprocessPathString($path)
74
    {
75
        // If the path is null, make sure to give it our match-all value
76
        $path = (null === $path) ? static::NULL_PATH_VALUE : (string) $path;
0 ignored issues
show
introduced by
The condition null === $path is always false.
Loading history...
77
78
        // If a custom regular expression (or negated custom regex)
79
        if ($this->namespace &&
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($this->namespace && Iss...ode && $path[1] === '@', Probably Intended Meaning: $this->namespace && (Iss...de && $path[1] === '@')
Loading history...
80
            (isset($path[0]) && $path[0] === '@') ||
81
            (isset($path[0]) && $path[0] === '!' && isset($path[1]) && $path[1] === '@')
82
        ) {
83
            // Is it negated?
84
            if ($path[0] === '!') {
85
                $negate = true;
86
                $path = substr($path, 2);
87
            } else {
88
                $negate = false;
89
                $path = substr($path, 1);
90
            }
91
92
            // Regex anchored to front of string
93
            if ($path[0] === '^') {
94
                $path = substr($path, 1);
95
            } else {
96
                $path = '.*' . $path;
97
            }
98
99
            if ($negate) {
100
                $path = '@^' . $this->namespace . '(?!' . $path . ')';
101
            } else {
102
                $path = '@^' . $this->namespace . $path;
103
            }
104
105
        } elseif ($this->namespace && $this->pathIsNull($path)) {
106
            // Empty route with namespace is a match-all
107
            $path = '@^' . $this->namespace . '(/|$)';
108
        } else {
109
            // Just prepend our namespace
110
            $path = $this->namespace . $path;
111
        }
112
113
        return $path;
114
    }
115
116
    /**
117
     * Build a Route instance
118
     *
119
     * @param callable $callback    Callable callback method to execute on route match
120
     * @param string $path          Route URI path to match
121
     * @param string|array $method  HTTP Method to match
122
     * @param boolean $count_match  Whether or not to count the route as a match when counting total matches
123
     * @param string $name          The name of the route
124
     * @return Route
125
     */
126
    public function build($callback, $path = null, $method = null, $count_match = true, $name = null)
127
    {
128
        return new Route(
129
            $callback,
130
            $this->preprocessPathString($path),
131
            $method,
132
            $this->shouldPathStringCauseRouteMatch($path) // Ignore the $count_match boolean that they passed
133
        );
134
    }
135
}
136