Test Failed
Branch master (effa58)
by Divine Niiquaye
02:13
created

Route::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 18
ccs 1
cts 1
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.4 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\Annotation;
19
20
use Flight\Routing\Routes\{DomainRoute, Route as BaseRoute};
21
use Flight\Routing\Handlers\ResourceHandler;
22
23
/**
24
 * Annotation class for @Route().
25
 *
26
 * @Annotation
27
 * @NamedArgumentConstructor
28
 *
29
 * On PHP 7.2+ Attributes are supported except you want to use Doctrine annotations:
30
 * ```php
31
 *     #[Route('/blog/{_locale}', name: 'blog', defaults: ['_locale' => 'en'])]
32
 *     class Blog
33
 *     {
34
 *         #[Route('/', name: '_index', methods: ['GET', 'HEAD'] schemes: 'https')]
35
 *         public function index()
36
 *         {
37
 *         }
38
 *         #[Route('/{id}', name: '_post', methods: 'POST' where: ["id" => '\d+'])]
39
 *         public function show()
40
 *         {
41
 *         }
42
 *     }
43
 * ```
44
 *
45
 * @Target({"CLASS", "METHOD", "FUNCTION"})
46
 */
47
#[\Spiral\Attributes\NamedArgumentConstructor]
48
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
49
final class Route
50
{
51
    /** @var string|null @Required */
52
    public ?string $path;
53
54
    /** @var string|null @Required */
55
    public ?string $name;
56
57
    /** @var string[] @Required */
58
    public array $methods;
59
60
    /** @var string[] */
61
    public array $hosts;
62
63
    /** @var string[] */
64
    public array $schemes;
65
66
    /** @var array<string,string> */
67
    public array $patterns;
68
69
    /** @var array<string,mixed> */
70
    public array $defaults;
71
72
    /** @var string|null */
73
    public ?string $resource;
74
75
    /**
76
     * @param string|string[] $methods
77
     * @param string|string[] $schemes
78
     * @param string|string[] $hosts
79
     * @param string[]        $where
80
     * @param string[]        $defaults
81
     */
82
    public function __construct(
83
        string $path = null,
84
        string $name = null,
85
        $methods = [],
86
        $schemes = [],
87
        $hosts = [],
88
        array $where = [],
89
        array $defaults = [],
90
        string $resource = null
91
    ) {
92
        $this->path = $path;
93
        $this->name = $name;
94
        $this->resource = $resource;
95
        $this->methods = (array) $methods;
96 97
        $this->schemes = (array) $schemes;
97
        $this->hosts = (array) $hosts;
98
        $this->patterns = $where;
99
        $this->defaults = $defaults;
100
    }
101
102
    /**
103
     * @param mixed $handler
104
     */
105
    public function getRoute($handler): DomainRoute
106
    {
107
        $routeData = [
108 97
            'handler' => !empty($this->resource) ? new ResourceHandler($handler, $this->resource) : $handler,
109 5
            'name' => $this->name,
110 5
            'path' => $this->path,
111 96
            'methods' => $this->methods,
112 1
            'schemes' => $this->schemes,
113
            'patterns' => $this->patterns,
114
            'defaults' => $this->defaults,
115 97
        ];
116 97
117 97
        if (null !== $this->path && 1 === \preg_match('/\*\<[\w@]+\>/', $this->path)) {
118 97
            $route = BaseRoute::__set_state($routeData);
119 97
        } else {
120 97
            $route = DomainRoute::__set_state($routeData);
121 97
        }
122 97
123 97
        if (!empty($this->hosts)) {
124 97
            $route->domain(...$this->hosts);
125
        }
126 97
127
        return $route;
128 97
    }
129
}
130