Passed
Push — master ( fa63c5...708113 )
by
unknown
01:57
created

Handler::executeHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 8
cp 0
crap 6
1
<?php
2
3
namespace CarbonFramework\Routing;
4
5
use Closure;
6
use Exception;
7
use CarbonFramework\Framework;
8
9
/**
10
 * Represent a callable or a controller method to be executed in response to a request
11
 */
12
class Handler {
13
	/**
14
	 * Actual handler
15
	 *
16
	 * @var callable|array|null
17
	 */
18
	protected $handler = null;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param string|callable $handler
24
	 */
25
	public function __construct( $handler ) {
26
		$this->set( $handler );
27
	}
28
29
	/**
30
	 * Parse a handler to a callable or a [class, method] array
31
	 *
32
	 * @param  string|callable     $handler
33
	 * @return callable|array|null
34
	 */
35
	protected function parse( $handler ) {
36
		if ( $handler instanceof Closure ) {
37
			return $handler;
38
		}
39
40
		if ( is_string( $handler ) )  {
41
			$handlerPieces = preg_split( '/@|::/', $handler, 2 );
42
			if ( count( $handlerPieces ) === 1 ) {
43
				if ( is_callable( $handlerPieces[0] ) ) {
44
					return $handlerPieces[0];
45
				}
46
				return null;
47
			}
48
			return array(
49
				'class' => $handlerPieces[0],
50
				'method' => $handlerPieces[1],
51
			);
52
		}
53
54
		return null;
55
	}
56
57
	/**
58
	 * Execute the handler returning raw result
59
	 *
60
	 * @return string|array|\Psr\Http\Message\ResponseInterface
61
	 */
62
	protected function executeHandler() {
63
		$arguments = func_get_args();
64
		if ( is_callable( $this->handler ) ) {
65
			return call_user_func_array( $this->handler, $arguments );
66
		}
67
68
		$class = $this->handler['class'];
69
		$method = $this->handler['method'];
70
71
		$controller = Framework::instantiate( $class );
72
		return call_user_func_array( [$controller, $method], $arguments );
73
	}
74
75
	/**
76
	 * Set the handler
77
	 *
78
	 * @param  string|callable $new_handler
79
	 * @return null
80
	 */
81
	public function set( $new_handler ) {
82
		$handler = $this->parse( $new_handler );
83
84
		if ( $handler === null ) {
85
			throw new Exception( 'No or invalid handler provided.' );
86
		}
87
88
		$this->handler = $handler;
89
	}
90
91
	/**
92
	 * Execute the handler
93
	 *
94
	 * @return \Psr\Http\Message\ResponseInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be \Psr\Http\Message\ResponseInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
95
	 */
96
	public function execute() {
97
		$response = call_user_func_array( [$this, 'executeHandler'], func_get_args() );
98
99
		if ( is_string( $response ) ) {
100
			return cf_output( $response );
101
		}
102
103
		if ( is_array( $response ) ) {
104
			return cf_json( $response );
105
		}
106
107
		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 CarbonFramework\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...
108
	}
109
}
110