Passed
Push — master ( 45b34a...9eb8da )
by
unknown
01:50
created

Url   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 50
ccs 12
cts 12
cp 1
rs 10
wmc 5

5 Methods

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