Passed
Push — master ( d0f9e3...c3e906 )
by Divine Niiquaye
08:38
created

CastingTrait::__serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 2
rs 9.9
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 Flight\Routing\Exceptions\InvalidControllerException;
21
22
trait CastingTrait
23
{
24
    /**
25
     * Locates appropriate route by name. Support dynamic route allocation using following pattern:
26
     * Pattern route:   `pattern/*<controller@action>`
27
     * Default route: `*<controller@action>`
28
     * Only action:   `pattern/*<action>`.
29
     *
30
     * @param string $route
31
     *
32
     * @throws InvalidControllerException
33
     *
34
     * @return string
35
     */
36 150
    private function castRoute(string $route): string
37
    {
38
        // Match domain + scheme from pattern...
39 150
        if (false !== \preg_match($regex = '@^(?:(https?):)?(//[^/]+)@i', $route)) {
40 150
            $route = $this->castDomain($route, $regex);
41
        }
42
43 150
        if (false !== \strpbrk($route, '*') && false !== \preg_match(self::RCA_PATTERN, $route, $matches)) {
0 ignored issues
show
Bug introduced by
The constant Flight\Routing\Traits\CastingTrait::RCA_PATTERN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
44 5
            if (!isset($matches['route']) || empty($matches['route'])) {
45 1
                throw new InvalidControllerException("Unable to locate route candidate on `{$route}`");
46
            }
47
48 4
            if (isset($matches['controller'], $matches['action'])) {
49 4
                $this->controller = [$matches['controller'] ?: $this->controller, $matches['action']];
0 ignored issues
show
Bug Best Practice introduced by
The property controller does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
            }
51
52 4
            $route = $matches['route'];
53
        }
54
55 149
        return (empty($route) || '/' === $route) ? '/' : $route;
56
    }
57
58
    /**
59
     * Match scheme and domain from route patterned path
60
     *
61
     * @param string $route
62
     * @param string $regex
63
     *
64
     * @return string
65
     */
66 150
    private function castDomain(string $route, string $regex): string
67
    {
68 150
        return (string) \preg_replace_callback($regex, function (array $matches): string {
69 9
            $this->setDomain(isset($matches[1]) ? $matches[0] : $matches[2]);
0 ignored issues
show
Bug introduced by
It seems like setDomain() 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

69
            $this->/** @scrutinizer ignore-call */ 
70
                   setDomain(isset($matches[1]) ? $matches[0] : $matches[2]);
Loading history...
70
71 9
            return '';
72 150
        }, $route);
73
    }
74
75
    /**
76
     * Ensures that the right-most slash is trimmed for prefixes of more than
77
     * one character, and that the prefix begins with a slash.
78
     *
79
     * @param string $uri
80
     * @param string $prefix
81
     *
82
     * @return string
83
     */
84 15
    private function castPrefix(string $uri, string $prefix): string
85
    {
86
        // Allow homepage uri on prefix just like python django url style.
87 15
        if (\in_array($uri, ['', '/'], true)) {
88 7
            return \rtrim($prefix, '/') . $uri;
89
        }
90
91 15
        if (1 === \preg_match('/^([^\|\/|&|-|_|~|@]+)(&|-|_|~|@)/i', $prefix, $matches)) {
92 1
            $newPattern = \rtrim($prefix, $matches[2]) . $matches[2] . $uri;
93
        }
94
95 15
        return !empty($newPattern) ? $newPattern : \rtrim($prefix, '/') . '/' . \ltrim($uri, '/');
96
    }
97
}
98