GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cd2d3a...6886d6 )
by Robert
09:38
created

UrlNormalizer::normalizeRoute()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
cc 6
eloc 10
nc 5
nop 1
crap 6.0359
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\Object;
12
use yii\base\InvalidConfigException;
13
14
/**
15
 * UrlNormalizer normalizes URLs for [[UrlManager]] and [[UrlRule]].
16
 *
17
 * @author Robert Korulczyk <[email protected]>
18
 * @author Cronfy <[email protected]>
19
 * @since 2.0.10
20
 */
21
class UrlNormalizer extends Object
22
{
23
    /**
24
     * Represents permament redirection during route normalization.
25
     * @see https://en.wikipedia.org/wiki/HTTP_301
26
     */
27
    const ACTION_REDIRECT_PERMANENT = 301;
28
    /**
29
     * Represents temporary redirection during route normalization.
30
     * @see https://en.wikipedia.org/wiki/HTTP_302
31
     */
32
    const ACTION_REDIRECT_TEMPORARY = 302;
33
    /**
34
     * Represents showing 404 error page during route normalization.
35
     * @see https://en.wikipedia.org/wiki/HTTP_404
36
     */
37
    const ACTION_NOT_FOUND = 404;
38
39
    /**
40
     * @var boolean whether slashes should be collapsed, for example `site///index` will be
41
     * converted into `site/index`
42
     */
43
    public $collapseSlashes = true;
44
    /**
45
     * @var boolean whether trailing slash should be normalized according to the suffix settings
46
     * of the rule
47
     */
48
    public $normalizeTrailingSlash = true;
49
    /**
50
     * @var integer|callable|null action to perform during route normalization.
51
     * Available options are:
52
     * - `null` - no special action will be performed
53
     * - `301` - the request should be redirected to the normalized URL using
54
     * permanent redirection
55
     * - `302` - the request should be redirected to the normalized URL using
56
     * temporary redirection
57
     * - `404` - [[NotFoundHttpException]] will be thrown
58
     * - `callable` - custom user callback, for example:
59
     *
60
     *   ```php
61
     *   function ($route, $normalizer) {
62
     *       // use custom action for redirections
63
     *       $route[1]['oldRoute'] = $route[0];
64
     *       $route[0] = 'site/redirect';
65
     *       return $route;
66
     *   }
67
     *   ```
68
     */
69
    public $action = self::ACTION_REDIRECT_PERMANENT;
70
71
72
    /**
73
     * Performs normalization action for the specified $route.
74
     * @param array $route route for normalization
75
     * @return array normalized route
76
     * @throws InvalidConfigException if invalid normalization action is used.
77
     * @throws UrlNormalizerRedirectException if normalization requires redirection.
78
     * @throws NotFoundHttpException if normalization suggests action matching route does not exist.
79
     */
80 5
    public function normalizeRoute($route)
81
    {
82 5
        if ($this->action === null) {
83 4
            return $route;
84 3
        } elseif ($this->action === static::ACTION_REDIRECT_PERMANENT || $this->action === static::ACTION_REDIRECT_TEMPORARY) {
85 3
            throw new UrlNormalizerRedirectException([$route[0]] + $route[1], $this->action);
86 2
        } elseif ($this->action === static::ACTION_NOT_FOUND) {
87 2
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
88 2
        } elseif (is_callable($this->action)) {
89 2
            return call_user_func($this->action, $route, $this);
90
        }
91
92
        throw new InvalidConfigException('Invalid normalizer action.');
93
    }
94
95
    /**
96
     * Normalizes specified pathInfo.
97
     * @param string $pathInfo pathInfo for normalization
98
     * @param string $suffix current rule suffix
99
     * @param boolean $normalized if specified, this variable will be set to `true` if $pathInfo
100
     * was changed during normalization
101
     * @return string normalized pathInfo
102
     */
103 5
    public function normalizePathInfo($pathInfo, $suffix, &$normalized = false)
104
    {
105 5
        if (empty($pathInfo)) {
106 2
            return $pathInfo;
107
        }
108
109 5
        $sourcePathInfo = $pathInfo;
110 5
        if ($this->collapseSlashes) {
111 5
            $pathInfo = $this->collapseSlashes($pathInfo);
112 5
        }
113
114 5
        if ($this->normalizeTrailingSlash === true) {
115 5
            $pathInfo = $this->normalizeTrailingSlash($pathInfo, $suffix);
116 5
        }
117
118 5
        $normalized = $sourcePathInfo !== $pathInfo;
119
120 5
        return $pathInfo;
121
    }
122
123
    /**
124
     * Collapse consecutive slashes in $pathInfo, for example converts `site///index` into `site/index`.
125
     * @param string $pathInfo raw path info.
126
     * @return string normalized path info.
127
     */
128 5
    protected function collapseSlashes($pathInfo)
129
    {
130 5
        return ltrim(preg_replace('#/{2,}#', '/', $pathInfo), '/');
131
    }
132
133
    /**
134
     * Adds or removes trailing slashes from $pathInfo depending on whether the $suffix has a
135
     * trailing slash or not.
136
     * @param string $pathInfo raw path info.
137
     * @param string $suffix
138
     * @return string normalized path info.
139
     */
140 5
    protected function normalizeTrailingSlash($pathInfo, $suffix)
141
    {
142 5
        if (substr($suffix, -1) === '/' && substr($pathInfo, -1) !== '/') {
143 2
            $pathInfo .= '/';
144 5
        } elseif (substr($suffix, -1) !== '/' && substr($pathInfo, -1) === '/') {
145 3
            $pathInfo = rtrim($pathInfo, '/');
146 3
        }
147
148 5
        return $pathInfo;
149
    }
150
}