Completed
Push — master ( e47ebd...1dc9c8 )
by LA
01:51
created

compability.php ➔ http_build_url()   C

Complexity

Conditions 11
Paths 1024

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 11
nc 1024
nop 1
dl 0
loc 13
rs 5.5714
c 0
b 0
f 0

How to fix   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
/**
3
 * Adds compability for missing methods
4
 *
5
 * @package imgix
6
 */
7
8
if ( ! function_exists( 'wp_parse_url' ) ) {
9
	/**
10
	 * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7
11
	 *
12
	 * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
13
	 * schemeless and relative url's with :// in the path, this works around those
14
	 * limitations providing a standard output on PHP 5.2~5.4+.
15
	 *
16
	 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
17
	 * when URL parsing failed.
18
	 *
19
	 * @since 4.4.0
20
	 *
21
	 * @param string $url The URL to parse.
22
	 *
23
	 * @return bool|array False on failure; Array of URL components on success;
24
	 *                    See parse_url()'s return values.
25
	 */
26
	function wp_parse_url( $url ) {
27
		$parts = @parse_url( $url );
28
		if ( ! $parts ) {
29
			// < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
30
			if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
31
				// Since we know it's a relative path, prefix with a scheme/host placeholder and try again
32
				if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
33
					return $parts;
34
				}
35
				// Remove the placeholder values
36
				unset( $parts['scheme'], $parts['host'] );
37
			} else {
38
				return $parts;
39
			}
40
		}
41
42
		// < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
43
		if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
44
			$path_parts    = explode( '/', substr( $parts['path'], 2 ), 2 );
45
			$parts['host'] = $path_parts[0];
46
			if ( isset( $path_parts[1] ) ) {
47
				$parts['path'] = '/' . $path_parts[1];
48
			} else {
49
				unset( $parts['path'] );
50
			}
51
		}
52
53
		return $parts;
54
	}
55
}
56
57
if ( ! function_exists( 'http_build_url' ) ) {
58
	/**
59
	 * This is a simplified version of http_build_url if pecl_http is missing
60
	 *
61
	 * @param array $parsed_url
62
	 *
63
	 * @return string
64
	 */
65
	function http_build_url( $parsed_url ) {
66
		$scheme   = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '';
67
		$host     = isset( $parsed_url['host'] ) ? $parsed_url['host'] : '';
68
		$port     = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : '';
69
		$user     = isset( $parsed_url['user'] ) ? $parsed_url['user'] : '';
70
		$pass     = isset( $parsed_url['pass'] ) ? ':' . $parsed_url['pass'] : '';
71
		$pass     = ( $user || $pass ) ? "$pass@" : '';
72
		$path     = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '';
73
		$query    = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '';
74
		$fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '';
75
76
		return "$scheme$user$pass$host$port$path$query$fragment";
77
	}
78
}