Passed
Push — master ( d0beff...f2ea3d )
by Atanas
03:34
created

Mixed::normalizePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace WPEmerge\Helpers;
4
5
class Mixed {
6
	/**
7
	 * Converts a value to an array containing this value unless it is an array
8
	 *
9
	 * @param  mixed   $argument
10
	 * @return array
11
	 */
12 2
	public static function toArray( $argument ) {
13 2
		if ( ! is_array( $argument ) ) {
14 1
			$argument = [$argument];
15
		}
16
17 2
		return $argument;
18
	}
19
20
	/**
21
	 * Executes a value depending on what type it is and returns the result
22
	 * Callable: call; return result
23
	 * Instance: call method; return result
24
	 * Class:    instantiate; call method; return result
25
	 * Other:    return value without taking any action
26
	 *
27
	 * @param  mixed  $entity
28
	 * @param  array  $arguments
29
	 * @param  string $method
30
	 * @return mixed
31
	 */
32 5
	public static function value( $entity, $arguments = [], $method = '__invoke' ) {
33 5
		if ( is_callable( $entity ) ) {
34 2
			return call_user_func_array( $entity, $arguments );
35
		}
36
37 3
		if ( is_object( $entity ) ) {
38 1
			return call_user_func_array( [$entity, $method], $arguments );
39
		}
40
41 2
		if ( static::isClass( $entity ) ) {
42 1
			return call_user_func_array( [new $entity(), $method], $arguments );
43
		}
44
45 1
		return $entity;
46
	}
47
48
	/**
49
	 * Check if a value is a valid class name
50
	 *
51
	 * @param  mixed   $class_name
52
	 * @return boolean
53
	 */
54 1
	public static function isClass( $class_name ) {
55 1
		return ( is_string( $class_name ) && class_exists( $class_name ) );
56
	}
57
58
	/**
59
	 * Normalize a path's slashes according to the current OS.
60
	 * Solves mixed slashes that are sometimes returned by WordPress core functions.
61
	 *
62
	 * @param  string $path
63
	 * @param  string $replace_with
64
	 * @return string
65
	 */
66
	public static function normalizePath( $path, $replace_with = DIRECTORY_SEPARATOR ) {
67
		return preg_replace( '~[/' . preg_quote( '\\', '~' ) . ']~', $replace_with, $path );
68
	}
69
}
70