Passed
Push — master ( 2cccd3...fd1ca6 )
by Atanas
02:44
created

MixedType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 6 2
A normalizePath() 0 2 1
A isClass() 0 2 2
A value() 0 14 4
1
<?php
2
3
namespace WPEmerge\Helpers;
4
5
class MixedType {
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 1
		}
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
	 *
31
	 * @return mixed
32
	 */
33 5
	public static function value( $entity, $arguments = [], $method = '__invoke' ) {
34 5
		if ( is_callable( $entity ) ) {
35 2
			return call_user_func_array( $entity, $arguments );
36
		}
37
38 3
		if ( is_object( $entity ) ) {
39 1
			return call_user_func_array( [$entity, $method], $arguments );
40
		}
41
42 2
		if ( static::isClass( $entity ) ) {
43 1
			return call_user_func_array( [new $entity(), $method], $arguments );
44
		}
45
46 1
		return $entity;
47
	}
48
49
	/**
50
	 * Check if a value is a valid class name
51
	 *
52
	 * @param  mixed   $class_name
53
	 *
54
	 * @return boolean
55
	 */
56 1
	public static function isClass( $class_name ) {
57 1
		return ( is_string( $class_name ) && class_exists( $class_name ) );
58
	}
59
60
	/**
61
	 * Normalize a path's slashes according to the current OS.
62
	 * Solves mixed slashes that are sometimes returned by WordPress core functions.
63
	 *
64
	 * @param  string $path
65
	 * @param  string $replace_with
66
	 * @return string
67
	 */
68 1
	public static function normalizePath( $path, $replace_with = DIRECTORY_SEPARATOR ) {
69 1
		return preg_replace( '~[/' . preg_quote( '\\', '~' ) . ']~', $replace_with, $path );
70
	}
71
}
72