Completed
Push — master ( 612ab5...954270 )
by Auke
02:57 queued 01:02
created

route   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 89.47%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 0
cbo 2
dl 0
loc 35
ccs 17
cts 19
cp 0.8947
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B match() 0 25 4
1
<?php
2
3
namespace arc;
4
/**
5
 * Class route
6
 * A simple router.
7
 * @package arc
8
 */
9
class route {
10
11
    /**
12
     * Matches a path or url to a list of routes and calls the best matching route handler.
13
     * @param string $path
14
     * @param mixed $routes A tree of routes with handlers as nodeValue.
15
     * @return array|bool
16
     */
17 2
    public static function match($path, $routes) {
18 2
        $routes     = \arc\tree::expand($routes);
19 2
        $controller = \arc\tree::dive(
20 2
            $routes->cd($path),
21 2
            function($node) {
22 2
                if ( isset($node->nodeValue) ) {
23 2
                    return $node;
24
                }
25 2
            }
26 2
        );
27 2
        if ( $controller ) {
28 2
            $remainder = substr( $path, strlen($controller->getPath()) );
29 2
            if ( is_callable($controller->nodeValue) ) {
30 2
                $result = call_user_func($controller->nodeValue, $remainder);
31 2
            } else {
32
                $result = $controller->nodeValue;
33
            }
34
            return [
35 2
                'path' => $controller->getPath(),
36 2
                'remainder' => $remainder,
37
                'result' => $result
38 2
            ];
39
        }
40
        return false;
41
    }
42
43
}