Passed
Push — master ( 29ee19...e5d2cb )
by Atanas
02:10
created

Mixed::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
1
<?php
2
3
namespace Obsidian\Helpers;
4
5
class Mixed {
6
	/**
7
	 * Converts a value to an array containing this value unless it's an array
8
	 *
9
	 * @param  mixed   $argument
10
	 * @return array
11
	 */
12
	public static function toArray( $argument ) {
13
		if ( ! is_array( $argument ) ) {
14
			$argument = [$argument];
15
		}
16
17
		return $argument;
18
	}
19
20
	/**
21
	 * Executes a value depending on what type it is and returns the result
22
	 * Callable: call
23
	 * Instance: call method
24
	 * Class:    instantiate and call method
25
	 * Other:    return it
26
	 *
27
	 * @param  mixed  $entity
28
	 * @param  array  $arguments
29
	 * @param  string $method
30
	 * @return mixed
31
	 */
32
	public static function value( $entity, $arguments = [], $method = '__invoke' ) {
33
		if ( is_callable( $entity ) ) {
34
			return call_user_func_array( $entity, $arguments );
35
		}
36
37
		if ( is_string( $entity ) && class_exists( $entity ) ) {
38
			$instance = new $entity();
39
			return call_user_func_array( [$instance, $method], $arguments );
40
		}
41
42
		return call_user_func_array( [$entity, $method], $arguments );
43
	}
44
}
45