Passed
Push — master ( db8d74...f13993 )
by Atanas
01:54
created

Handler::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Helpers;
4
5
use Closure;
6
use Exception;
7
use WPEmerge;
8
9
/**
10
 * Represent a generic handler - a Closure or a class method to be resolved from the service container
11
 */
12
class Handler {
13
	/**
14
	 * Parsed handler
15
	 *
16
	 * @var array|Closure|null
17
	 */
18
	protected $handler = null;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @throws Exception
24
	 * @param  string|Closure $raw_handler
25
	 */
26 4
	public function __construct( $raw_handler ) {
27 4
		$handler = $this->parse( $raw_handler );
28
29 4
		if ( $handler === null ) {
30 1
			throw new Exception( 'No or invalid handler provided.' );
31
		}
32
33 3
		$this->handler = $handler;
34 3
	}
35
36
	/**
37
	 * Parse a raw handler to a Closure or a [class, method] array
38
	 *
39
	 * @param  string|Closure     $handler
0 ignored issues
show
Documentation introduced by
There is no parameter named $handler. Did you maybe mean $raw_handler?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
40
	 * @return array|Closure|null
41
	 */
42 3
	protected function parse( $raw_handler ) {
43 3
		if ( $raw_handler instanceof Closure ) {
44
			return $raw_handler;
45
		}
46
47 3
		if ( is_string( $raw_handler ) )  {
48 3
			return $this->parseFromString( $raw_handler );
49
		}
50
51
		return null;
52
	}
53
54
	/**
55
	 * Parse a raw string handler to a [class, method] array
56
	 *
57
	 * @param  string     $handler
0 ignored issues
show
Documentation introduced by
There is no parameter named $handler. Did you maybe mean $raw_handler?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
58
	 * @return array|null
59
	 */
60 3
	protected function parseFromString( $raw_handler ) {
61 3
		$handlerPieces = preg_split( '/@|::/', $raw_handler, 2 );
62
63 3
		if ( count( $handlerPieces ) === 2 ) {
64
			return array(
65 2
				'class' => $handlerPieces[0],
66 2
				'method' => $handlerPieces[1],
67 2
			);
68
		}
69
70 1
		return null;
71
	}
72
73
	/**
74
	 * Get the parsed handler
75
	 *
76
	 * @return array|Closure|null
77
	 */
78 1
	public function get() {
79 1
		return $this->handler;
80
	}
81
82
	/**
83
	 * Execute the parsed handler with any provided arguments and return the result
84
	 *
85
	 * @param  mixed $arguments,...
0 ignored issues
show
Bug introduced by
There is no parameter named $arguments,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
86
	 * @return mixed
87
	 */
88 3
	public function execute() {
89 3
		$arguments = func_get_args();
90 3
		if ( is_a( $this->handler, Closure::class ) ) {
91 2
			return call_user_func_array( $this->handler, $arguments );
92
		}
93
94 1
		$class = $this->handler['class'];
95 1
		$method = $this->handler['method'];
96
97 1
		$instance = WPEmerge::instantiate( $class );
98 1
		return call_user_func_array( [$instance, $method], $arguments );
99
	}
100
}
101