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

CastingTrait::castPrefix()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 4
rs 10
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
     * {@inheritdoc}
26
     *
27
     * @internal
28
     */
29 1
    final public function serialize(): string
30
    {
31 1
        return \serialize($this->__serialize());
0 ignored issues
show
Bug introduced by
The method __serialize() does not exist on Flight\Routing\Traits\CastingTrait. Did you maybe mean serialize()? ( Ignorable by Annotation )

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

31
        return \serialize($this->/** @scrutinizer ignore-call */ __serialize());

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...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * @internal
38
     */
39 1
    final public function unserialize($serialized): void
40
    {
41 1
        $this->__unserialize(\unserialize($serialized));
0 ignored issues
show
Bug introduced by
The method __unserialize() does not exist on Flight\Routing\Traits\CastingTrait. Did you maybe mean unserialize()? ( Ignorable by Annotation )

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

41
        $this->/** @scrutinizer ignore-call */ 
42
               __unserialize(\unserialize($serialized));

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...
42 1
    }
43
44
    /**
45
     * Locates appropriate route by name. Support dynamic route allocation using following pattern:
46
     * Pattern route:   `pattern/*<controller@action>`
47
     * Default route: `*<controller@action>`
48
     * Only action:   `pattern/*<action>`.
49
     *
50
     * @param string $route
51
     *
52
     * @throws InvalidControllerException
53
     *
54
     * @return string
55
     */
56 128
    private function castRoute(string $route): string
57
    {
58
        // Match domain + scheme from pattern...
59 128
        if (false !== \preg_match($regex = '@^(?:(https?):)?(//[^/]+)@i', $route)) {
60 128
            $route = $this->castDomain($route, $regex);
61
        }
62
63 128
        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...
64 2
            if (!isset($matches['route']) || empty($matches['route'])) {
65 1
                throw new InvalidControllerException("Unable to locate route candidate on `{$route}`");
66
            }
67
68 1
            if (isset($matches['controller'], $matches['action'])) {
69 1
                $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...
70
            }
71
72 1
            $route = $matches['route'];
73
        }
74
75 127
        return (empty($route) || '/' === $route) ? '/' : $route;
76
    }
77
78
    /**
79
     * Match scheme and domain from route patterned path
80
     *
81
     * @param string $route
82
     * @param string $regex
83
     *
84
     * @return string
85
     */
86 128
    private function castDomain(string $route, string $regex): string
87
    {
88 128
        return (string) \preg_replace_callback($regex, function (array $matches): string {
89 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

89
            $this->/** @scrutinizer ignore-call */ 
90
                   setDomain(isset($matches[1]) ? $matches[0] : $matches[2]);
Loading history...
90
91 9
            return '';
92 128
        }, $route);
93
    }
94
95
    /**
96
     * Ensures that the right-most slash is trimmed for prefixes of more than
97
     * one character, and that the prefix begins with a slash.
98
     *
99
     * @param string $uri
100
     * @param string $prefix
101
     *
102
     * @return string
103
     */
104 5
    private function castPrefix(string $uri, string $prefix): string
105
    {
106
        // Allow homepage uri on prefix just like python django url style.
107 5
        if (\in_array($uri, ['', '/'], true)) {
108 1
            return \rtrim($prefix, '/') . $uri;
109
        }
110
111 5
        if (1 === \preg_match('/^([^\|\/|&|-|_|~|@]+)(&|-|_|~|@)/i', $prefix, $matches)) {
112 1
            $newPattern = \rtrim($prefix, $matches[2]) . $matches[2] . $uri;
113
        }
114
115 5
        return !empty($newPattern) ? $newPattern : \rtrim($prefix, '/') . '/' . \ltrim($uri, '/');
116
    }
117
}
118