Passed
Push — master ( 3a0782...491d5e )
by Atanas
02:17
created

MixedType::removeTrailingSlash()   A

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 2018 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
	 *
16
	 * @param  mixed $argument
17
	 * @return array
18
	 */
19 2
	public static function toArray( $argument ) {
20 2
		if ( ! is_array( $argument ) ) {
21 1
			$argument = [$argument];
22 1
		}
23
24 2
		return $argument;
25
	}
26
27
	/**
28
	 * Executes a value depending on what type it is and returns the result
29
	 * Callable: call; return result
30
	 * Instance: call method; return result
31
	 * Class:    instantiate; call method; return result
32
	 * Other:    return value without taking any action
33
	 *
34
	 * @param  mixed  $entity
35
	 * @param  array  $arguments
36
	 * @param  string $method
37
	 *
38
	 * @return mixed
39
	 */
40 5
	public static function value( $entity, $arguments = [], $method = '__invoke' ) {
41 5
		if ( is_callable( $entity ) ) {
42 2
			return call_user_func_array( $entity, $arguments );
43
		}
44
45 3
		if ( is_object( $entity ) ) {
46 1
			return call_user_func_array( [$entity, $method], $arguments );
47
		}
48
49 2
		if ( static::isClass( $entity ) ) {
50 1
			return call_user_func_array( [new $entity(), $method], $arguments );
51
		}
52
53 1
		return $entity;
54
	}
55
56
	/**
57
	 * Check if a value is a valid class name
58
	 *
59
	 * @param  mixed   $class_name
60
	 *
61
	 * @return boolean
62
	 */
63 1
	public static function isClass( $class_name ) {
64 1
		return ( is_string( $class_name ) && class_exists( $class_name ) );
65
	}
66
67
	/**
68
	 * Normalize a path's slashes according to the current OS.
69
	 * Solves mixed slashes that are sometimes returned by WordPress core functions.
70
	 *
71
	 * @param  string $path
72
	 * @param  string $slash
73
	 * @return string
74
	 */
75 1
	public static function normalizePath( $path, $slash = DIRECTORY_SEPARATOR ) {
76 1
		return preg_replace( '~[' . preg_quote( '/\\', '~' ) . ']+~', $slash, $path );
77
	}
78
79
	/**
80
	 * Ensure path has a trailing slash.
81
	 *
82
	 * @param  string $path
83
	 * @param  string $slash
84
	 * @return string
85
	 */
86 1
	public static function addTrailingSlash( $path, $slash = DIRECTORY_SEPARATOR ) {
87 1
		$path = static::normalizePath( $path, $slash );
88 1
		$path = preg_replace( '~' . preg_quote( $slash, '~' ) . '*$~', $slash, $path );
89 1
		return $path;
90
	}
91
92
	/**
93
	 * Ensure path does not have a trailing slash.
94
	 *
95
	 * @param  string $path
96
	 * @param  string $slash
97
	 * @return string
98
	 */
99 1
	public static function removeTrailingSlash( $path, $slash = DIRECTORY_SEPARATOR ) {
100 1
		$path = static::normalizePath( $path, $slash );
101 1
		$path = preg_replace( '~' . preg_quote( $slash, '~' ) . '+$~', '', $path );
102 1
		return $path;
103
	}
104
}
105