Passed
Push — master ( 517cb3...2c2725 )
by Atanas
02:15
created

Handler::executeHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Routing;
4
5
use Closure;
6
use Exception;
7
use WPEmerge;
8
9
/**
10
 * Represent a Closure or a controller method to be executed in response to a request
11
 */
12
class Handler {
13
	/**
14
	 * Actual handler
15
	 *
16
	 * @var array|Closure|null
17
	 */
18
	protected $handler = null;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param string|Closure $handler
24
	 */
25 1
	public function __construct( $handler ) {
26 1
		$this->set( $handler );
27 1
	}
28
29
	/**
30
	 * Parse a handler to a Closure or a [class, method] array
31
	 *
32
	 * @param  string|Closure     $handler
33
	 * @return array|Closure|null
34
	 */
35 5
	protected function parse( $handler ) {
36 5
		if ( $handler instanceof Closure ) {
37 5
			return $handler;
38
		}
39
40 4
		if ( is_string( $handler ) )  {
41 3
			return $this->parseFromString( $handler );
42
		}
43
44 1
		return null;
45
	}
46
47
	/**
48
	 * Parse a string handler to a [class, method] array
49
	 *
50
	 * @param  string     $handler
51
	 * @return array|null
52
	 */
53 3
	protected function parseFromString( $handler ) {
54 3
		$handlerPieces = preg_split( '/@|::/', $handler, 2 );
55
56 3
		if ( count( $handlerPieces ) === 2 ) {
57
			return array(
58 2
				'class' => $handlerPieces[0],
59 2
				'method' => $handlerPieces[1],
60 2
			);
61
		}
62
63 1
		return null;
64
	}
65
66
	/**
67
	 * Get the handler
68
	 *
69
	 * @return array|Closure|null
70
	 */
71 1
	public function get() {
72 1
		return $this->handler;
73
	}
74
75
	/**
76
	 * Set the handler
77
	 *
78
	 * @throws Exception
79
	 * @param  string|Closure $new_handler
80
	 * @return void
81
	 */
82 6
	public function set( $new_handler ) {
83 6
		$handler = $this->parse( $new_handler );
84
85 6
		if ( $handler === null ) {
86 2
			throw new Exception( 'No or invalid handler provided.' );
87
		}
88
89 6
		$this->handler = $handler;
90 6
	}
91
92
	/**
93
	 * Execute the handler returning raw result
94
	 *
95
	 * @return string|array|\Psr\Http\Message\ResponseInterface
96
	 */
97 2
	protected function executeHandler() {
98 2
		$arguments = func_get_args();
99 2
		if ( is_a( $this->handler, Closure::class ) ) {
100 1
			return call_user_func_array( $this->handler, $arguments );
101
		}
102
103 1
		$class = $this->handler['class'];
104 1
		$method = $this->handler['method'];
105
106 1
		$controller = WPEmerge::instantiate( $class );
107 1
		return call_user_func_array( [$controller, $method], $arguments );
108
	}
109
110
	/**
111
	 * Execute the handler
112
	 *
113
	 * @return \Psr\Http\Message\ResponseInterface
114
	 */
115 5
	public function execute() {
116 5
		$response = call_user_func_array( [$this, 'executeHandler'], func_get_args() );
117
118 5
		if ( is_string( $response ) ) {
119 1
			return wpm_output( $response );
120
		}
121
122 4
		if ( is_array( $response ) ) {
123 1
			return wpm_json( $response );
124
		}
125
126 3
		return $response;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $response; (object|integer|double|null|boolean) is incompatible with the return type documented by WPEmerge\Routing\Handler::execute of type Psr\Http\Message\ResponseInterface|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
127
	}
128
}
129