Hooks::getHandlers()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * A tool for running hook functions.
5
 *
6
 * Copyright 2004, 2005 Evan Prodromou <[email protected]>.
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21
 *
22
 * @author Evan Prodromou <[email protected]>
23
 * @see hooks.txt
24
 * @file
25
 */
26
27
/**
28
 * Hooks class.
29
 *
30
 * Used to supersede $wgHooks, because globals are EVIL.
31
 *
32
 * @since 1.18
33
 */
34
class Hooks {
35
	/**
36
	 * Array of events mapped to an array of callbacks to be run
37
	 * when that event is triggered.
38
	 */
39
	protected static $handlers = [];
40
41
	/**
42
	 * Attach an event handler to a given hook.
43
	 *
44
	 * @param string $name Name of hook
45
	 * @param callable $callback Callback function to attach
46
	 *
47
	 * @since 1.18
48
	 */
49
	public static function register( $name, $callback ) {
50
		if ( !isset( self::$handlers[$name] ) ) {
51
			self::$handlers[$name] = [];
52
		}
53
54
		self::$handlers[$name][] = $callback;
55
	}
56
57
	/**
58
	 * Clears hooks registered via Hooks::register(). Does not touch $wgHooks.
59
	 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
60
	 *
61
	 * @param string $name The name of the hook to clear.
62
	 *
63
	 * @since 1.21
64
	 * @throws MWException If not in testing mode.
65
	 */
66 View Code Duplication
	public static function clear( $name ) {
67
		if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
68
			throw new MWException( 'Cannot reset hooks in operation.' );
69
		}
70
71
		unset( self::$handlers[$name] );
72
	}
73
74
	/**
75
	 * Returns true if a hook has a function registered to it.
76
	 * The function may have been registered either via Hooks::register or in $wgHooks.
77
	 *
78
	 * @since 1.18
79
	 *
80
	 * @param string $name Name of hook
81
	 * @return bool True if the hook has a function registered to it
82
	 */
83
	public static function isRegistered( $name ) {
84
		global $wgHooks;
85
		return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
86
	}
87
88
	/**
89
	 * Returns an array of all the event functions attached to a hook
90
	 * This combines functions registered via Hooks::register and with $wgHooks.
91
	 *
92
	 * @since 1.18
93
	 *
94
	 * @param string $name Name of the hook
95
	 * @return array
96
	 */
97
	public static function getHandlers( $name ) {
98
		global $wgHooks;
99
100
		if ( !self::isRegistered( $name ) ) {
101
			return [];
102
		} elseif ( !isset( self::$handlers[$name] ) ) {
103
			return $wgHooks[$name];
104
		} elseif ( !isset( $wgHooks[$name] ) ) {
105
			return self::$handlers[$name];
106
		} else {
107
			return array_merge( self::$handlers[$name], $wgHooks[$name] );
108
		}
109
	}
110
111
	/**
112
	 * Call hook functions defined in Hooks::register and $wgHooks.
113
	 *
114
	 * For a certain hook event, fetch the array of hook events and
115
	 * process them. Determine the proper callback for each hook and
116
	 * then call the actual hook using the appropriate arguments.
117
	 * Finally, process the return value and return/throw accordingly.
118
	 *
119
	 * @param string $event Event name
120
	 * @param array $args Array of parameters passed to hook functions
121
	 * @param string|null $deprecatedVersion Optionally, mark hook as deprecated with version number
122
	 * @return bool True if no handler aborted the hook
123
	 *
124
	 * @throws Exception
125
	 * @throws FatalError
126
	 * @throws MWException
127
	 * @since 1.22 A hook function is not required to return a value for
128
	 *   processing to continue. Not returning a value (or explicitly
129
	 *   returning null) is equivalent to returning true.
130
	 */
131
	public static function run( $event, array $args = [], $deprecatedVersion = null ) {
132
		foreach ( self::getHandlers( $event ) as $hook ) {
133
			// Turn non-array values into an array. (Can't use casting because of objects.)
134
			if ( !is_array( $hook ) ) {
135
				$hook = [ $hook ];
136
			}
137
138
			if ( !array_filter( $hook ) ) {
139
				// Either array is empty or it's an array filled with null/false/empty.
140
				continue;
141
			} elseif ( is_array( $hook[0] ) ) {
142
				// First element is an array, meaning the developer intended
143
				// the first element to be a callback. Merge it in so that
144
				// processing can be uniform.
145
				$hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
146
			}
147
148
			/**
149
			 * $hook can be: a function, an object, an array of $function and
150
			 * $data, an array of just a function, an array of object and
151
			 * method, or an array of object, method, and data.
152
			 */
153
			if ( $hook[0] instanceof Closure ) {
154
				$func = "hook-$event-closure";
155
				$callback = array_shift( $hook );
156
			} elseif ( is_object( $hook[0] ) ) {
157
				$object = array_shift( $hook );
158
				$method = array_shift( $hook );
159
160
				// If no method was specified, default to on$event.
161
				if ( $method === null ) {
162
					$method = "on$event";
163
				}
164
165
				$func = get_class( $object ) . '::' . $method;
166
				$callback = [ $object, $method ];
167
			} elseif ( is_string( $hook[0] ) ) {
168
				$func = $callback = array_shift( $hook );
169
			} else {
170
				throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
171
			}
172
173
			// Run autoloader (workaround for call_user_func_array bug)
174
			// and throw error if not callable.
175
			if ( !is_callable( $callback ) ) {
176
				throw new MWException( 'Invalid callback ' . $func . ' in hooks for ' . $event . "\n" );
177
			}
178
179
			/*
180
			 * Call the hook. The documentation of call_user_func_array says
181
			 * false is returned on failure. However, if the function signature
182
			 * does not match the call signature, PHP will issue an warning and
183
			 * return null instead. The following code catches that warning and
184
			 * provides better error message.
185
			 */
186
			$retval = null;
0 ignored issues
show
Unused Code introduced by
$retval is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
187
			$badhookmsg = null;
0 ignored issues
show
Unused Code introduced by
$badhookmsg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
188
			$hook_args = array_merge( $hook, $args );
189
190
			// mark hook as deprecated, if deprecation version is specified
191
			if ( $deprecatedVersion !== null ) {
192
				wfDeprecated( "$event hook (used in $func)", $deprecatedVersion );
193
			}
194
195
			$retval = call_user_func_array( $callback, $hook_args );
196
197
			// Process the return value.
198
			if ( is_string( $retval ) ) {
199
				// String returned means error.
200
				throw new FatalError( $retval );
201
			} elseif ( $retval === false ) {
202
				// False was returned. Stop processing, but no error.
203
				return false;
204
			}
205
		}
206
207
		return true;
208
	}
209
}
210