yourls_add_query_arg()   F
last analyzed

Complexity

Conditions 15
Paths 384

Size

Total Lines 66
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 16.4478

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 51
nc 384
nop 0
dl 0
loc 66
ccs 35
cts 43
cp 0.8139
crap 16.4478
rs 2.7833
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
0 ignored issues
show
Coding Style introduced by
Empty line required before block comment
Loading history...
3
 * Functions relative to how YOURLS handle some links
4
 *
5
 */
6
7
/**
8
 * Add a query var to a URL and return URL. Completely stolen from WP.
9
 *
10
 * Works with one of these parameter patterns:
11
 *     array( 'var' => 'value' )
12
 *     array( 'var' => 'value' ), $url
13
 *     'var', 'value'
14
 *     'var', 'value', $url
15
 * If $url omitted, uses $_SERVER['REQUEST_URI']
16
 *
17
 * The result of this function call is a URL : it should be escaped before being printed as HTML
18
 *
19
 * @since 1.5
20
 * @param string|array $param1 Either newkey or an associative_array.
21
 * @param string       $param2 Either newvalue or oldquery or URI.
22
 * @param string       $param3 Optional. Old query or URI.
23
 * @return string New URL query string.
24
 */
25
function yourls_add_query_arg() {
26 2
    $ret = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
27 2
    if ( is_array( func_get_arg(0) ) ) {
28 1
        if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
29
            $uri = $_SERVER['REQUEST_URI'];
30
        else
31 1
            $uri = @func_get_arg( 1 );
32
    } else {
33 2
        if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
34
            $uri = $_SERVER['REQUEST_URI'];
35
        else
36 2
            $uri = @func_get_arg( 2 );
37
    }
38
39 2
    $uri = str_replace( '&amp;', '&', $uri );
40
41
42 2
    if ( $frag = strstr( $uri, '#' ) )
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
43
        $uri = substr( $uri, 0, -strlen( $frag ) );
44
    else
45 2
        $frag = '';
46
47 2
    if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
48 1
        $protocol = $matches[0];
49 1
        $uri = substr( $uri, strlen( $protocol ) );
50
    } else {
51 1
        $protocol = '';
52
    }
53
54 2
    if ( strpos( $uri, '?' ) !== false ) {
55 1
        $parts = explode( '?', $uri, 2 );
56 1
        if ( 1 == count( $parts ) ) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
57
            $base = '?';
58
            $query = $parts[0];
59
        } else {
60 1
            $base = $parts[0] . '?';
61 1
            $query = $parts[1];
62
        }
63 2
    } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
64 2
        $base = $uri . '?';
65 2
        $query = '';
66
    } else {
67
        $base = '';
68
        $query = $uri;
69
    }
70
71 2
    parse_str( $query, $qs );
72 2
    $qs = yourls_urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 107 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
73 2
    if ( is_array( func_get_arg( 0 ) ) ) {
74 1
        $kayvees = func_get_arg( 0 );
75 1
        $qs = array_merge( $qs, $kayvees );
0 ignored issues
show
Bug introduced by
It seems like $qs can also be of type string; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

75
        $qs = array_merge( /** @scrutinizer ignore-type */ $qs, $kayvees );
Loading history...
76
    } else {
77 2
        $qs[func_get_arg( 0 )] = func_get_arg( 1 );
78
    }
79
80 2
    foreach ( (array) $qs as $k => $v ) {
81 2
        if ( $v === false )
82
            unset( $qs[$k] );
83
    }
84
85 2
    $ret = http_build_query( $qs );
86 2
    $ret = trim( $ret, '?' );
87 2
    $ret = preg_replace( '#=(&|$)#', '$1', $ret );
88 2
    $ret = $protocol . $base . $ret . $frag;
89 2
    $ret = rtrim( $ret, '?' );
90 2
    return $ret;
91
}
92
93
/**
94
 * Navigates through an array and encodes the values to be used in a URL. Stolen from WP, used in yourls_add_query_arg()
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 120 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
95
 *
96
 */
97
function yourls_urlencode_deep( $value ) {
98 2
    $value = is_array( $value ) ? array_map( 'yourls_urlencode_deep', $value ) : urlencode( $value );
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
99 2
    return $value;
100
}
101
102
/**
103
 * Remove arg from query. Opposite of yourls_add_query_arg. Stolen from WP.
104
 *
105
 * The result of this function call is a URL : it should be escaped before being printed as HTML
106
 *
107
 * @since 1.5
108
 * @param string|array $key   Query key or keys to remove.
109
 * @param bool|string  $query Optional. When false uses the $_SERVER value. Default false.
110
 * @return string New URL query string.
111
 */
112
function yourls_remove_query_arg( $key, $query = false ) {
113
    if ( is_array( $key ) ) { // removing multiple keys
114
        foreach ( $key as $k )
115
            $query = yourls_add_query_arg( $k, false, $query );
116
        return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query also could return the type boolean which is incompatible with the documented return type string.
Loading history...
117
    }
118
    return yourls_add_query_arg( $key, false, $query );
119
}
120
121
/**
122
 * Converts keyword into short link (prepend with YOURLS base URL) or stat link (sho.rt/abc+)
123
 *
124
 * This function does not check for a valid keyword.
125
 * The resulting link is normalized to allow for IDN translation to UTF8
126
 *
127
 * @param  string $keyword  Short URL keyword
128
 * @param  bool   $stats    Optional, true to return a stat link (eg sho.rt/abc+)
129
 * @return string           Short URL, or keyword stat URL
130
 */
131
function yourls_link( $keyword = '', $stats = false ) {
132 9
    $keyword = yourls_sanitize_keyword($keyword);
133 9
    if( $stats  === true ) {
134 3
        $keyword = $keyword . '+';
135
    }
136 9
    $link    = yourls_normalize_uri( yourls_get_yourls_site() . '/' . $keyword );
137
138 9
    if( yourls_is_ssl() ) {
139
        $link = yourls_set_url_scheme( $link, 'https' );
140
    }
141
142 9
    return yourls_apply_filter( 'yourls_link', $link, $keyword );
143
}
144
145
/**
146
 * Converts keyword into stat link (prepend with YOURLS base URL, append +)
147
 *
148
 * This function does not make sure the keyword matches an actual short URL
149
 *
150
 */
151
function yourls_statlink( $keyword = '' ) {
152 3
    $link = yourls_link( $keyword, true );
153 3
    return yourls_apply_filter( 'yourls_statlink', $link, $keyword );
154
}
155
156
/**
157
 * Return admin link, with SSL preference if applicable.
158
 *
159
 */
160
function yourls_admin_url( $page = '' ) {
161 3
    $admin = yourls_get_yourls_site() . '/admin/' . $page;
162 3
    if( yourls_is_ssl() or yourls_needs_ssl() ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
163
        $admin = yourls_set_url_scheme( $admin, 'https' );
164
    }
165 3
    return yourls_apply_filter( 'admin_url', $admin, $page );
166
}
167
168
/**
169
 * Return YOURLS_SITE or URL under YOURLS setup, with SSL preference
170
 *
171
 */
172
function yourls_site_url( $echo = true, $url = '' ) {
173 1
    $url = yourls_get_relative_url( $url );
174 1
    $url = trim( yourls_get_yourls_site() . '/' . $url, '/' );
175
176
    // Do not enforce (checking yourls_need_ssl() ) but check current usage so it won't force SSL on non-admin pages
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 116 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
177 1
    if( yourls_is_ssl() ) {
178
        $url = yourls_set_url_scheme( $url, 'https' );
179
    }
180 1
    $url = yourls_apply_filter( 'site_url', $url );
181 1
    if( $echo ) {
182 1
        echo $url;
183
    }
184 1
    return $url;
185
}
186
187
/**
188
 *  Get YOURLS_SITE value, trimmed and filtered
189
 *
190
 *  In addition of being filtered for plugins to hack this, this function is mostly here
191
 *  to help people entering "sho.rt/" instead of "sho.rt" in their config
192
 *
193
 *  @since 1.7.7
194
 *  @return string  YOURLS_SITE, trimmed and filtered
195
 */
196
function yourls_get_yourls_site() {
197 66
    return yourls_apply_filter('get_yourls_site', trim(YOURLS_SITE, '/'));
198
}
199
200
/**
201
 * Change protocol of a URL to HTTPS if we are currently on HTTPS
202
 *
203
 * This function is used to avoid insert 'http://' images or scripts in a page when it's served through HTTPS,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 110 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
204
 * to avoid "mixed content" errors.
205
 * So:
206
 *   - if you are on http://sho.rt/, 'http://something' and 'https://something' are left untouched.
207
 *   - if you are on https:/sho.rt/, 'http://something' is changed to 'https://something'
208
 *
209
 * So, arguably, this function is poorly named. It should be something like yourls_match_current_protocol_if_we_re_on_https
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
210
 *
211
 * @since 1.5.1
212
 * @param string $url        a URL
213
 * @param string $normal     Optional, the standard scheme (defaults to 'http://')
214
 * @param string $ssl        Optional, the SSL scheme (defaults to 'https://')
215
 * @return string            the modified URL, if applicable
216
 */
217
function yourls_match_current_protocol( $url, $normal = 'http://', $ssl = 'https://' ) {
218
    // we're only doing something if we're currently serving through SSL and the input URL begins with 'http://' or 'https://'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
219 21
    if( yourls_is_ssl() && in_array( yourls_get_protocol($url), array('http://', 'https://') ) ) {
220 6
        $url = str_replace( $normal, $ssl, $url );
221
    }
222
223 21
    return yourls_apply_filter( 'match_current_protocol', $url );
224
}
225
226
/**
227
 * Auto detect custom favicon in /user directory, fallback to YOURLS favicon, and echo/return its URL
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
228
 *
229
 * This function supersedes function yourls_favicon(), deprecated in 1.7.10, with a better naming.
230
 *
231
 * @since 1.7.10
232
 * @param  bool $echo   true to echo, false to silently return
233
 * @return string       favicon URL
234
 *
235
 */
236
function yourls_get_yourls_favicon_url( $echo = true ) {
237 1
    static $favicon = null;
238
239 1
    if( $favicon !== null ) {
240
        if( $echo ) {
241
            echo $favicon;
242
        }
243
        return $favicon;
244
    }
245
246 1
    $custom = null;
247
    // search for favicon.(gif|ico|png|jpg|svg)
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
248 1
    foreach( array( 'gif', 'ico', 'png', 'jpg', 'svg' ) as $ext ) {
249 1
        if( file_exists( YOURLS_USERDIR. '/favicon.' . $ext ) ) {
250
            $custom = 'favicon.' . $ext;
251
            break;
252
        }
253
    }
254
255 1
    if( $custom ) {
256
        $favicon = yourls_site_url( false, YOURLS_USERURL . '/' . $custom );
257
    } else {
258 1
        $favicon = yourls_site_url( false ) . '/images/favicon.gif';
259
    }
260
261 1
    $favicon = yourls_apply_filter('get_favicon_url', $favicon);
262
263 1
    if( $echo ) {
264 1
        echo $favicon;
265
    }
266 1
    return $favicon;
267
}
268