Passed
Push — master ( 8fb003...dae8ba )
by Divine Niiquaye
11:39
created

RouterTrait::getProfile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
cc 3
nc 3
nop 0
crap 3.0416
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 Biurad\Annotations\LoaderInterface;
21
use Flight\Routing\DebugRoute;
22
use Flight\Routing\Interfaces\RouteMatcherInterface;
23
use Flight\Routing\Route;
24
use Flight\Routing\RouteCollection;
25
use Flight\Routing\RouteResolver;
26
use Psr\Http\Message\ResponseFactoryInterface;
27
use Psr\Http\Message\UriFactoryInterface;
28
29
trait RouterTrait
30
{
31
    use MiddlewareTrait;
32
33
    /** @var null|RouteMatcherInterface */
34
    private $matcher;
35
36
    /** @var ResponseFactoryInterface */
37
    private $responseFactory;
38
39
    /** @var UriFactoryInterface */
40
    private $uriFactory;
41
42
    /** @var null|DebugRoute */
43
    private $debug;
44
45
    /** @var RouteCollection */
46
    private $routes;
47
48
    /** @var null|Route */
49
    private $route;
50
51
    /** @var RouteResolver */
52
    private $resolver;
53
54
    /** @var mixed[] */
55
    private $options = [];
56
57
    /**
58
     * Get the profiled routes
59
     *
60
     * @return null|DebugRoute
61
     */
62 1
    public function getProfile(): ?DebugRoute
63
    {
64 1
        if ($this->options['debug']) {
65 1
            foreach ($this->routes as $route) {
66 1
                $this->debug->addProfile(new DebugRoute($route->get('name'), $route));
0 ignored issues
show
Bug introduced by
The method addProfile() does not exist on null. ( Ignorable by Annotation )

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

66
                $this->debug->/** @scrutinizer ignore-call */ 
67
                              addProfile(new DebugRoute($route->get('name'), $route));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            }
68
69 1
            return $this->debug;
70
        }
71
72
        return null;
73
    }
74
75
    /**
76
     * Load routes from annotation.
77
     *
78
     * @param LoaderInterface $loader
79
     */
80 15
    public function loadAnnotation(LoaderInterface $loader): void
81
    {
82 15
        $annotations = $loader->load();
83
84 15
        foreach ($annotations as $annotation) {
85 4
            if ($annotation instanceof RouteCollection) {
86 4
                $this->addRoute(...$annotation->getRoutes());
0 ignored issues
show
Bug introduced by
It seems like addRoute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

86
                $this->/** @scrutinizer ignore-call */ 
87
                       addRoute(...$annotation->getRoutes());
Loading history...
87
            }
88
        }
89 4
    }
90
91
    /**
92
     * Get merged default parameters.
93
     *
94
     * @param Route $route
95
     */
96 46
    private function mergeDefaults(Route $route): void
97
    {
98 46
        $defaults = $route->get('defaults');
99 46
        $param    = $route->get('arguments');
100 46
        $excludes = ['_arguments' => true, '_domain' => true];
101
102 46
        foreach ($defaults as $key => $value) {
103 20
            if (isset($excludes[$key])) {
104 10
                continue;
105
            }
106
107 15
            if (!isset($param[$key]) || (!\is_int($key) && null !== $value)) {
108 15
                $route->argument($key, $value);
109
            }
110
        }
111 46
    }
112
}
113