Test Failed
Push — master ( d3660e...c7a4a9 )
by Divine Niiquaye
10:08
created

RouterTrait::mergeAttributes()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 9.2222
ccs 7
cts 7
cp 1
cc 6
nc 5
nop 1
crap 6
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
            $routes = $this->getCollection()->getRoutes();
0 ignored issues
show
Bug introduced by
It seems like getCollection() 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

65
            $routes = $this->/** @scrutinizer ignore-call */ getCollection()->getRoutes();
Loading history...
66 1
67 1
            foreach ($routes as $route) {
68 1
                $this->debug->addProfile(new DebugRoute($route->getName(), $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

68
                $this->debug->/** @scrutinizer ignore-call */ 
69
                              addProfile(new DebugRoute($route->getName(), $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...
69
            }
70
71
            return $this->debug;
72 1
        }
73
74
        return null;
75
    }
76
77
    /**
78
     * Load routes from annotation.
79
     *
80
     * @param LoaderInterface $loader
81
     */
82
    public function loadAnnotation(LoaderInterface $loader): void
83
    {
84 6
        // Guess you off debug mode and would not need to add new routes.
85
        if ($this->isFrozen()) {
0 ignored issues
show
Bug introduced by
It seems like isFrozen() 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

85
        if ($this->/** @scrutinizer ignore-call */ isFrozen()) {
Loading history...
86 6
            return;
87 2
        }
88
89
        $annotations = $loader->load();
90 4
91
        foreach ($annotations as $annotation) {
92
            if ($annotation instanceof RouteCollection) {
93
                $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

93
                $this->/** @scrutinizer ignore-call */ 
94
                       addRoute(...$annotation->getRoutes());
Loading history...
94
            }
95
        }
96
    }
97
98 1
    /**
99
     * Get merged default parameters.
100 1
     *
101
     * @param Route $route
102
     */
103
    private function mergeDefaults(Route $route): void
104
    {
105
        $defaults = $route->getDefaults();
106 1
        $param    = $route->getArguments();
107
        $excludes = ['_arguments' => true, '_domain' => true];
108 1
109 1
        foreach ($defaults as $key => $value) {
110
            if (isset($excludes[$key])) {
111
                continue;
112
            }
113
114
            if (!isset($param[$key]) || (!\is_int($key) && null !== $value)) {
115
                $route->argument($key, $value);
116 4
            }
117
        }
118 4
    }
119
}
120