Passed
Push — master ( cfe0c1...d1cf98 )
by Divine Niiquaye
02:27
created

ValidationTrait::resolveController()   B

Complexity

Conditions 11
Paths 7

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 9
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 11
rs 7.3166

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Traits;
19
20
use Closure;
21
use Flight\Routing\Interfaces\RouteInterface;
22
use Flight\Routing\Router;
23
24
trait ValidationTrait
25
{
26
    /**
27
     * Check if given request method matches given route method.
28
     *
29
     * @param string[] $routeMethod
30
     * @param string   $requestMethod
31
     *
32
     * @return bool
33
     */
34 28
    private function compareMethod(array $routeMethod, string $requestMethod): bool
35
    {
36 28
        return \in_array($requestMethod, $routeMethod, true);
37
    }
38
39
    /**
40
     * Check if given request domain matches given route domain.
41
     *
42
     * @param null|string              $routeDomain
43
     * @param string                   $requestDomain
44
     * @param array<int|string,string> $parameters
45
     *
46
     * @return bool
47
     */
48 26
    private function compareDomain(?string $routeDomain, string $requestDomain, array &$parameters): bool
49
    {
50 26
        return ($routeDomain === null || empty($routeDomain)) ||
51 26
            (bool) \preg_match($routeDomain, $requestDomain, $parameters);
52
    }
53
54
    /**
55
     * Check if given request uri matches given uri method.
56
     *
57
     * @param string                   $routeUri
58
     * @param string                   $requestUri
59
     * @param array<int|string,string> $parameters
60
     *
61
     * @return bool
62
     */
63 30
    private function compareUri(string $routeUri, string $requestUri, array &$parameters): bool
64
    {
65 30
        return (bool) \preg_match($routeUri, $requestUri, $parameters);
66
    }
67
68
    /**
69
     * Check if given request uri scheme matches given route scheme.
70
     *
71
     * @param string[] $routeScheme
72
     * @param string   $requestScheme
73
     *
74
     * @return bool
75
     */
76 25
    private function compareScheme(array $routeScheme, string $requestScheme): bool
77
    {
78 25
        return empty($routeScheme) || \in_array($requestScheme, $routeScheme, true);
79
    }
80
81
    /**
82
     * Get merged default parameters.
83
     *
84
     * @param array<int|string,mixed> $params
85
     * @param array<string,string>    $defaults
86
     *
87
     * @return array<string,string> Merged default parameters
88
     */
89 24
    private function mergeDefaults(array $params, array $defaults): array
90
    {
91 24
        foreach ($params as $key => $value) {
92 24
            if (!\is_int($key) && (!isset($defaults[$key]) || null !== $value)) {
93 4
                $defaults[$key] = $value;
94
            }
95
        }
96
97 24
        return $defaults;
98
    }
99
100
    /**
101
     * Merge Router attributes in route default and patterns.
102
     *
103
     * @param RouteInterface $route
104
     *
105
     * @return RouteInterface
106
     */
107 30
    private function mergeAttributes(RouteInterface $route): RouteInterface
108
    {
109 30
        foreach ($this->attributes as $type => $attributes) {
110 1
            if (Router::TYPE_DEFAULT === $type) {
111 1
                $route->setDefaults($attributes);
112
113 1
                continue;
114
            }
115
116 1
            $route->setPatterns($attributes);
117
        }
118
119 30
        return $route;
120
    }
121
122
    /**
123
     * @param callable|object|string|string[] $controller
124
     *
125
     * @return callable|object|string|string[]
126
     */
127 21
    private function resolveController($controller)
128
    {
129 21
        if (null !== $this->namespace && (\is_string($controller) || !$controller instanceof Closure)) {
130
            if (
131 4
                \is_string($controller) &&
132 4
                !\class_exists($controller) &&
133 4
                false === \stripos($controller, $this->namespace)
134
            ) {
135 3
                $controller = \is_callable($controller) ? $controller : $this->namespace . $controller;
136
            }
137
138 4
            if (\is_array($controller) && (!\is_object($controller[0]) && !\class_exists($controller[0]))) {
139 1
                $controller[0] = $this->namespace . $controller[0];
140
            }
141
        }
142
143 21
        return $controller;
144
    }
145
}
146