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.

Url::to()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 28
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 36
ccs 0
cts 36
cp 0
crap 90
rs 8.0555
1
<?php
2
/**
3
 * Class Url
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
10
namespace icy2003\php\ihelpers;
11
12
use icy2003\php\I;
13
14
/**
15
 * Url 相关
16
 */
17
class Url
18
{
19
    /**
20
     * URL 编码
21
     *
22
     * 推荐使用 rawurlencode
23
     *
24
     * @param string $url
25
     *
26
     * @return string
27
     */
28
    public static function encode($url)
29
    {
30
        return rawurlencode($url);
31
    }
32
33
    /**
34
     * URL 解码
35
     *
36
     * 推荐使用 rawurldecode
37
     *
38
     * @param string $url
39
     *
40
     * @return string
41
     */
42
    public static function decode($url)
43
    {
44
        return rawurldecode($url);
45
    }
46
47
    /**
48
     * 创建 URL 的 query 字符串
49
     *
50
     * @param array $queryArray
51
     *
52
     * @return string
53
     */
54
    public static function buildQuery($queryArray)
55
    {
56
        return http_build_query($queryArray, null, '&', PHP_QUERY_RFC3986);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $numeric_prefix of http_build_query(). ( Ignorable by Annotation )

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

56
        return http_build_query($queryArray, /** @scrutinizer ignore-type */ null, '&', PHP_QUERY_RFC3986);
Loading history...
57
    }
58
59
    /**
60
     * 拼装 URL 链接
61
     *
62
     *
63
     * @param array|string $url URL 信息数组
64
     * - 数组:第一个项为路由,其他项示键值对的参数
65
     * - 字符串:即为路由
66
     * @param false|string $scheme 协议名,默认 false,表示使用相对路径
67
     *
68
     * @return string
69
     */
70
    public static function to($url, $scheme = false)
71
    {
72
        if (I::isYii2()) {
73
            return (string)I::call(['\yii\helpers\Url', 'to'], [$url, $scheme]);
74
        }
75
        $request = new Request();
76
        if (is_array($url)) {
77
            $params = $url;
78
            $anchor = (string)I::get($url, '#', '');
79
            unset($params['#']);
80
            $route = trim($params[0], '/');
81
            unset($params[0]);
82
            $baseUrl = $request->getBaseUrl();
83
            $url = $baseUrl . '/' . $route . '?' . self::buildQuery($params) . '#' . $anchor;
84
            if (false !== $scheme) {
85
                if (strpos($url, '://') === false) {
86
                    $hostInfo = $request->getHostInfo();
87
                    if (strncmp($url, '//', 2) === 0) {
88
                        $url = substr($hostInfo, 0, strpos($hostInfo, '://')) . ':' . $url;
89
                    } else {
90
                        $url = $hostInfo . $url;
91
                    }
92
                }
93
                return self::scheme($url, $scheme);
94
            }
95
            return $url;
96
        } else {
97
            $url = (string)I::getAlias($url);
98
            if (false === $scheme) {
99
                return $url;
100
            }
101
            if (self::isRelative($url)) {
102
                $url = $request->getHostInfo() . '/' . ltrim($url);
103
                return '' === $scheme ? $url : $scheme . '://' . ltrim($url, '/');
104
            }
105
            return self::scheme($url, $scheme);
106
        }
107
    }
108
109
    /**
110
     * 判断 url 是否是相对地址
111
     *
112
     * @param string $url
113
     *
114
     * @return boolean
115
     */
116
    public static function isRelative($url)
117
    {
118
        return strncmp($url, '//', 2) && strpos($url, '://') === false;
119
    }
120
121
    /**
122
     * 获取加协议的 url
123
     *
124
     * @param string $url
125
     * @param string|false $scheme
126
     *
127
     * @return string
128
     */
129
    public static function scheme($url, $scheme)
130
    {
131
        if (self::isRelative($url) || !is_string($scheme)) {
132
            return $url;
133
        }
134
135
        if (Strings::isStartsWith($url, '//')) {
136
            return $scheme === '' ? $url : "$scheme:$url";
137
        }
138
139
        if (Strings::isContains($url, '://', $pos)) {
140
            if ($scheme === '') {
141
                $url = substr($url, $pos + 1);
142
            } else {
143
                $url = $scheme . substr($url, $pos);
144
            }
145
        }
146
147
        return $url;
148
    }
149
}
150