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

Mixed   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 12
cp 0
rs 10
wmc 6
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 7 2
A value() 0 12 4
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