RouteCollectionMatcher::matchRouteCollection()   D
last analyzed

Complexity

Conditions 10
Paths 20

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 45
rs 4.8196
cc 10
eloc 27
nc 20
nop 4

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
 * Dash
4
 *
5
 * @link      http://github.com/DASPRiD/Dash For the canonical source repository
6
 * @copyright 2013-2015 Ben Scholzen 'DASPRiD'
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Dash\RouteCollection;
11
12
use Dash\Exception\UnexpectedValueException;
13
use Dash\MatchResult;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
/**
17
 * Utility methods for working with route collections.
18
 */
19
final class RouteCollectionMatcher
0 ignored issues
show
Coding Style introduced by
RouteCollectionMatcher does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
20
{
21
    /**
22
     * This is a purely static class, so disallow instantiating.
23
     *
24
     * @codeCoverageIgnore
25
     */
26
    private function __construct()
27
    {
28
    }
29
30
    /**
31
     * Matches a request against a route collection.
32
     *
33
     * @param  RouteCollectionInterface $routeCollection
34
     * @param  ServerRequestInterface   $request
35
     * @param  int                      $pathOffset
36
     * @param  array                    $parentParams
37
     * @return MatchResult|null
38
     * @throws UnexpectedValueException
39
     */
40
    public static function matchRouteCollection(
41
        RouteCollectionInterface $routeCollection,
42
        ServerRequestInterface $request,
43
        $pathOffset,
44
        array $parentParams
45
    ) {
46
        $methodFailureResult = null;
47
        $schemeFailureResult = null;
48
49
        foreach ($routeCollection as $name => $route) {
50
            if (null === ($matchResult = $route->match($request, $pathOffset))) {
51
                continue;
52
            }
53
54
            if ($matchResult->isSuccess()) {
55
                return MatchResult::fromChildMatch($matchResult, $parentParams, $name);
56
            }
57
58
            if ($matchResult->isMethodFailure()) {
59
                if (null === $methodFailureResult) {
60
                    $methodFailureResult = $matchResult;
61
                } else {
62
                    $methodFailureResult = MatchResult::mergeMethodFailures($methodFailureResult, $matchResult);
63
                }
64
                continue;
65
            }
66
67
            if ($matchResult->isSchemeFailure()) {
68
                $schemeFailureResult = $schemeFailureResult ?: $matchResult;
69
                continue;
70
            }
71
72
            return $matchResult;
73
        }
74
75
        if (null !== $schemeFailureResult) {
76
            return $schemeFailureResult;
77
        }
78
79
        if (null !== $methodFailureResult) {
80
            return $methodFailureResult;
81
        }
82
83
        return null;
84
    }
85
}
86