Passed
Push — master ( 4bc1b6...29ee19 )
by Atanas
01:56
created

Mixed::toArray()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 30
rs 8.8571
1
<?php
2
3
namespace Obsidian\Helpers;
4
5
class Mixed {
6
	/**
7
	 * Converts a value to an array containing this value if needed
8
	 * The $check_for_callable flag is there to prevent coincidences that
9
	 * can happen with [$object, 'stringThatMatchesAMethodOf$object']
10
	 *
11
	 * @param  mixed   $argument
12
	 * @param  boolean $check_for_callable
13
	 * @return array
14
	 */
15
	public static function toArray( $argument, $check_for_callable = false ) {
16
		if ( $check_for_callable && is_array( $argument ) && is_callable( $argument ) ) {
17
			$argument = [$argument];
18
		}
19
20
		if ( ! is_array( $argument ) ) {
21
			$argument = [$argument];
22
		}
23
24
		return $argument;
25
	}
26
27
	/**
28
	 * Executes a value depending on what type it is and returns the result
29
	 * Callable: call
30
	 * Instance: call method
31
	 * Class:    instantiate and call method
32
	 * Other:    return it
33
	 *
34
	 * @param  mixed  $entity
35
	 * @param  array  $arguments
36
	 * @param  string $method
0 ignored issues
show
Documentation introduced by
Should the type for parameter $method not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
	 * @return mixed
38
	 */
39
	public static function value( $entity, $arguments = [], $method = null ) {
40
		if ( is_callable( $entity ) ) {
41
			return call_user_func_array( $entity, $arguments );
42
		}
43
44
		if ( is_string( $entity ) && class_exists( $entity ) ) {
45
			$instance = new $entity();
46
			return call_user_func_array( [$instance, $method], $arguments );
47
		}
48
49
		return call_user_func_array( [$entity, $method], $arguments );
50
	}
51
}
52