MixedType::removeTrailingSlash()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2017-2019 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Helpers;
11
12
class MixedType {
13
	/**
14
	 * Converts a value to an array containing this value unless it is an array.
15
	 * This will not convert objects like (array) casting does.
16
	 *
17
	 * @param  mixed $argument
18
	 * @return array
19
	 */
20 2
	public static function toArray( $argument ) {
21 2
		if ( ! is_array( $argument ) ) {
22 1
			$argument = [$argument];
23
		}
24
25 2
		return $argument;
26
	}
27
28
	/**
29
	 * Executes a value depending on what type it is and returns the result
30
	 * Callable: call; return result
31
	 * Instance: call method; return result
32
	 * Class:    instantiate; call method; return result
33
	 * Other:    return value without taking any action
34
	 *
35
	 * @noinspection PhpDocSignatureInspection
36
	 * @param  mixed    $entity
37
	 * @param  array    $arguments
38
	 * @param  string   $method
39
	 * @param  callable $instantiator
40
	 * @return mixed
41
	 */
42 6
	public static function value(
43
		$entity,
44
		$arguments = [],
45
		$method = '__invoke',
46
		$instantiator = 'static::instantiate'
47
	) {
48 6
		if ( is_callable( $entity ) ) {
49 2
			return call_user_func_array( $entity, $arguments );
50
		}
51
52 4
		if ( is_object( $entity ) ) {
53 1
			return call_user_func_array( [$entity, $method], $arguments );
54
		}
55
56 3
		if ( static::isClass( $entity ) ) {
57 2
			return call_user_func_array( [call_user_func( $instantiator, $entity ), $method], $arguments );
58
		}
59
60 1
		return $entity;
61
	}
62
63
	/**
64
	 * Check if a value is a valid class name
65
	 *
66
	 * @param  mixed   $class_name
67
	 * @return boolean
68
	 */
69 1
	public static function isClass( $class_name ) {
70 1
		return ( is_string( $class_name ) && class_exists( $class_name ) );
71
	}
72
73
	/**
74
	 * Create a new instance of the given class.
75
	 *
76
	 * @param  string $class_name
77
	 * @return object
78
	 */
79 1
	public static function instantiate( $class_name ) {
80 1
		return new $class_name();
81
	}
82
83
	/**
84
	 * Normalize a path's slashes according to the current OS.
85
	 * Solves mixed slashes that are sometimes returned by WordPress core functions.
86
	 *
87
	 * @param  string $path
88
	 * @param  string $slash
89
	 * @return string
90
	 */
91 1
	public static function normalizePath( $path, $slash = DIRECTORY_SEPARATOR ) {
92 1
		return preg_replace( '~[' . preg_quote( '/\\', '~' ) . ']+~', $slash, $path );
93
	}
94
95
	/**
96
	 * Ensure path has a trailing slash.
97
	 *
98
	 * @param  string $path
99
	 * @param  string $slash
100
	 * @return string
101
	 */
102 1
	public static function addTrailingSlash( $path, $slash = DIRECTORY_SEPARATOR ) {
103 1
		$path = static::normalizePath( $path, $slash );
104 1
		$path = preg_replace( '~' . preg_quote( $slash, '~' ) . '*$~', $slash, $path );
105 1
		return $path;
106
	}
107
108
	/**
109
	 * Ensure path does not have a trailing slash.
110
	 *
111
	 * @param  string $path
112
	 * @param  string $slash
113
	 * @return string
114
	 */
115 1
	public static function removeTrailingSlash( $path, $slash = DIRECTORY_SEPARATOR ) {
116 1
		$path = static::normalizePath( $path, $slash );
117 1
		$path = preg_replace( '~' . preg_quote( $slash, '~' ) . '+$~', '', $path );
118 1
		return $path;
119
	}
120
}
121