Passed
Push — master ( 74a67e...bf1276 )
by Atanas
01:58
created

Url   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
ccs 15
cts 15
cp 1
wmc 5
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 8 1
A addLeadingSlash() 0 3 1
A removeLeadingSlash() 0 3 1
A addTrailingSlash() 0 3 1
A removeTrailingSlash() 0 3 1
1
<?php
2
3
namespace WPEmerge\Helpers;
4
5
use WPEmerge\Requests\Request;
6
7
/**
8
 * A collection of tools dealing with urls
9
 */
10
class Url {
11
	/**
12
	 * Get the path for the request relative to the home url
13
	 *
14
	 * @return string
15
	 */
16 3
	public static function getPath( Request $request ) {
17 3
		$url = $request->getUrl();
18 3
		$relative_url = substr( $url, strlen( home_url( '/' ) ) );
19 3
		$relative_url = static::addLeadingSlash( $relative_url );
20 3
		$relative_url = preg_replace( '~\?.*~', '', $relative_url );
21 3
		$relative_url = static::addTrailingSlash( $relative_url );
22 3
		return $relative_url;
23
	}
24
25
	/**
26
	 * Ensure url has a leading slash
27
	 *
28
	 * @param  string $url
29
	 * @return string
30
	 */
31 4
	public static function addLeadingSlash( $url ) {
32 4
		return '/' . static::removeLeadingSlash( $url );
33
	}
34
35
	/**
36
	 * Ensure url does not have a leading slash
37
	 *
38
	 * @param  string $url
39
	 * @return string
40
	 */
41 5
	public static function removeLeadingSlash( $url ) {
42 5
		return preg_replace( '/^\/+/', '', $url );
43
	}
44
45
	/**
46
	 * Ensure url has a trailing slash
47
	 *
48
	 * @param  string $url
49
	 * @return string
50
	 */
51 4
	public static function addTrailingSlash( $url ) {
52 4
		return trailingslashit( $url );
53
	}
54
55
	/**
56
	 * Ensure url does not have a trailing slash
57
	 *
58
	 * @param  string $url
59
	 * @return string
60
	 */
61 1
	public static function removeTrailingSlash( $url ) {
62 1
		return untrailingslashit( $url );
63
	}
64
}
65