Passed
Push — master ( de569e...cba919 )
by Jip
03:18
created

Stencil_Handler_Factory::execute_handler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4286
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
/**
3
 * Factory for PageType handlers
4
 *
5
 * @package Stencil
6
 */
7
8
/**
9
 * Class HandlerFactory
10
 */
11
class Stencil_Handler_Factory implements Stencil_Handler_Factory_Interface {
12
	/**
13
	 * List of registered Handlers
14
	 *
15
	 * @var array
16
	 */
17
	private static $handlers = array();
18
19
	/**
20
	 * Set a hierarchy handler for a page
21
	 *
22
	 * @param string                 $page Page to set.
23
	 * @param array|Traversable|null $handler Optional. Handler to use.
24
	 */
25
	public static function set_hierarchy_handler( $page, $handler = null ) {
26
		self::set_settable_handler( 'Hierarchy', $page, $handler = null );
27
	}
28
29
	/**
30
	 * Set a page type handler
31
	 *
32
	 * @param string   $page Page to set.
33
	 * @param callable $handler Handler to use.
34
	 */
35
	public static function set_page_type_handler( $page, $handler ) {
36
		self::set_settable_handler( 'PageType', $page, $handler );
37
	}
38
39
	/**
40
	 * Remove handler for a page.
41
	 *
42
	 * @param string $page Page to remove from.
43
	 */
44
	public static function remove_page_type_handler( $page ) {
45
		self::set_settable_handler( 'PageType', $page );
46
	}
47
48
	/**
49
	 * Set page type hooking function
50
	 *
51
	 * @param string   $page Page to set to.
52
	 * @param callable $handler Handler to use.
53
	 */
54
	public static function set_page_type_hooker( $page, $handler ) {
55
		self::set_settable_handler( 'PageHook', $page, $handler );
56
	}
57
58
	/**
59
	 * Remove page hooker from page
60
	 *
61
	 * @param string $page Page to remove from.
62
	 */
63
	public static function remove_page_type_hooker( $page ) {
64
		self::set_settable_handler( 'PageHook', $page );
65
	}
66
67
	/**
68
	 * Get current hierarchy handler for page
69
	 *
70
	 * @param string $page Page to get from.
71
	 *
72
	 * @return array|Traversable
73
	 */
74
	public static function get_hierarchy_handler( $page ) {
75
		return self::get_settable_handler( $page, 'Hierarchy' );
76
	}
77
78
	/**
79
	 * Get the current page type handler
80
	 *
81
	 * @param string $page Page to get from.
82
	 *
83
	 * @return callable
84
	 */
85
	public static function get_page_type_handler( $page ) {
86
		return self::get_settable_handler( $page, 'Page_Type' );
87
	}
88
89
	/**
90
	 * Get the current page type hooker
91
	 *
92
	 * @param string $page Page to get from.
93
	 *
94
	 * @return callable
95
	 */
96
	public static function get_page_type_hooker( $page ) {
97
		return self::get_settable_handler( $page, 'Page_Hook' );
98
	}
99
100
	/**
101
	 * Run the handler for the specified page
102
	 *
103
	 * @param string $page Page to call handler for
104
	 * @param Stencil_Handler_Interface $controller Controller that initiated the call
105
	 */
106
	public static function run_page_type_handler( $page, Stencil_Handler_Interface $controller ) {
107
		self::execute_handler( self::get_page_type_handler( $page ), $controller );
0 ignored issues
show
Documentation introduced by
self::get_page_type_handler($page) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
	}
109
110
	/**
111
	 * Run the hooker for the specified page
112
	 *
113
	 * @param string $page Page to call handler for
114
	 * @param Stencil_Handler_Interface $controller Controller that initiated the call
115
	 */
116
	public static function run_page_type_hook( $page, Stencil_Handler_Interface $controller ) {
117
		self::execute_handler( self::get_page_type_hooker( $page ), $controller );
0 ignored issues
show
Documentation introduced by
self::get_page_type_hooker($page) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
	}
119
120
	/**
121
	 * Execute a handler if it is callable
122
	 *
123
	 * @param string $handler The handler for the page requested
124
	 * @param Stencil_Handler_Interface $controller Controller that initiated the call
125
	 */
126
	private static function execute_handler( $handler, Stencil_Handler_Interface $controller ) {
127
		if ( is_callable( $handler ) ) {
128
			call_user_func( $handler, $controller );
129
		}
130
	}
131
132
	/**
133
	 * Unified setter function
134
	 *
135
	 * @param string     $type Type of object.
136
	 * @param string     $page Page name.
137
	 * @param mixed|null $handler Handler to apply.
138
	 *
139
	 * @throws InvalidArgumentException For invalid argument types.
140
	 */
141
	private static function set_settable_handler( $type, $page, $handler = null ) {
142
		// Create NullObject for empty handler, i.e. remove functionality.
143
		if ( empty( $handler ) ) {
144
			$handler = Stencil_Subclass_Factory::create_null( sprintf( 'Stencil_%s', $type ) );
145
		}
146
147
		switch ( $type ) {
148
			case 'Hierarchy':
149
				if ( ! is_array( $handler ) && ! ( $handler instanceof Traversable ) ) {
150
					throw new InvalidArgumentException( 'Expected $handler to be array or Traversable.' );
151
				}
152
				break;
153
154
			default:
155
				if ( ! is_callable( $handler ) ) {
156
					throw new InvalidArgumentException( 'Expected $handler to be callable.' );
157
				}
158
				break;
159
		}
160
161
		self::$handlers[ $type ][ $page ] = $handler;
162
	}
163
164
	/**
165
	 * Unified getter function
166
	 *
167
	 * @param string $page Page to get from.
168
	 * @param string $type Type to get.
169
	 *
170
	 * @return array Callable function
171
	 */
172
	private static function get_settable_handler( $page, $type ) {
173
		if ( ! isset( self::$handlers[ $type ][ $page ] ) ) {
174
			$handler = Stencil_Cached_Subclass_Factory::create_or_null( $page, sprintf( 'Stencil_%s', $type ) );
175
176
			switch ( $type ) {
177
				case 'Hierarchy':
178
					break;
179
				default:
180
					$handler = array( $handler, 'execute' );
181
					break;
182
			}
183
184
			self::$handlers[ $type ][ $page ] = $handler;
185
		}
186
187
		return self::$handlers[ $type ][ $page ];
188
	}
189
}
190