r81032 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81031‎ | r81032 | r81033 >
Date:16:50, 26 January 2011
Author:ashley
Status:ok
Tags:
Comment:
Hooks.php: trim trailing spaces, tweak docs, etc.
Modified paths:
  • /trunk/phase3/includes/Hooks.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Hooks.php
@@ -4,26 +4,25 @@
55 *
66 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
77 *
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.
 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.
1212 *
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.
 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.
1717 *
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
 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
2121 *
2222 * @author Evan Prodromou <evan@wikitravel.org>
2323 * @see hooks.txt
2424 * @file
2525 */
2626
27 -
2827 /**
2928 * Call hook functions defined in $wgHooks
3029 *
@@ -45,72 +44,58 @@
4645
4746 class MWHookException extends MWException {}
4847
49 -
5048 /**
5149 * Hooks class.
52 - *
53 - * Used to supersede $wgHooks, because globals are EVIL.
5450 *
 51+ * Used to supersede $wgHooks, because globals are EVIL.
5552 */
5653 class Hooks {
57 -
 54+
5855 protected static $handlers = array();
59 -
 56+
6057 /**
6158 * Attach an event handler to a given hook
62 - *
63 - * @access public
64 - * @param mixed $name Name of hook
65 - * @param mixed $callback Callback function to attach
 59+ *
 60+ * @param $name Mixed: name of hook
 61+ * @param $callback Mixed: callback function to attach
6662 * @return void
6763 */
6864 public static function register( $name, $callback ) {
69 -
7065 if( !isset( self::$handlers[$name] ) ) {
7166 self::$handlers[$name] = array();
7267 }
73 -
 68+
7469 self::$handlers[$name][] = $callback;
75 -
7670 }
77 -
 71+
7872 /**
7973 * Returns true if a hook has a function registered to it.
80 - *
81 - * @access public
82 - * @param mixed $name Name of hook
83 - * @return bool
 74+ *
 75+ * @param $name Mixed: name of hook
 76+ * @return Boolean: true if a hook has a function registered to it
8477 */
8578 public static function isRegistered( $name ) {
86 -
8779 if( !isset( self::$handlers[$name] ) ) {
8880 self::$handlers[$name] = array();
8981 }
90 -
 82+
9183 return ( count( self::$handlers[$name] ) != 0 );
92 -
9384 }
94 -
 85+
9586 /**
9687 * Returns an array of all the event functions attached to a hook
97 - *
98 - * @access public
99 - * @param mixed $name Name of hook
 88+ *
 89+ * @param $name Mixed: name of the hook
10090 * @return array
10191 */
10292 public static function getHandlers( $name ) {
103 -
10493 if( !isset( self::$handlers[$name] ) ) {
10594 return array();
10695 }
107 -
 96+
10897 return self::$handlers[$name];
109 -
11098 }
111 -
112 -
113 -
114 -
 99+
115100 /**
116101 * Call hook functions defined in Hooks::register
117102 *
@@ -123,33 +108,31 @@
124109 * @return Boolean
125110 */
126111 public static function run( $event, $args = array() ) {
127 -
128112 global $wgHooks;
129 -
 113+
130114 // Return quickly in the most common case
131115 if ( !isset( self::$handlers[$event] ) && !isset( $wgHooks[$event] ) ) {
132116 return true;
133117 }
134 -
135 - if (!is_array(self::$handlers)) {
136 - throw new MWException("Local hooks array is not an array!\n");
 118+
 119+ if ( !is_array( self::$handlers ) ) {
 120+ throw new MWException( "Local hooks array is not an array!\n" );
137121 }
138 -
139 - if (!is_array($wgHooks)) {
140 - throw new MWException("Global hooks array is not an array!\n");
 122+
 123+ if ( !is_array( $wgHooks ) ) {
 124+ throw new MWException( "Global hooks array is not an array!\n" );
141125 }
142 -
 126+
143127 $new_handlers = (array) self::$handlers;
144128 $old_handlers = (array) $wgHooks;
145 -
 129+
146130 $hook_array = array_merge( $new_handlers, $old_handlers );
147 -
148 - if ( !is_array($hook_array[$event]) ) {
149 - throw new MWException("Hooks array for event '$event' is not an array!\n");
 131+
 132+ if ( !is_array( $hook_array[$event] ) ) {
 133+ throw new MWException( "Hooks array for event '$event' is not an array!\n" );
150134 }
151 -
152 - foreach ($hook_array[$event] as $index => $hook) {
153 -
 135+
 136+ foreach ( $hook_array[$event] as $index => $hook ) {
154137 $object = null;
155138 $method = null;
156139 $func = null;
@@ -157,16 +140,16 @@
158141 $have_data = false;
159142 $closure = false;
160143 $badhookmsg = false;
161 -
162 - /* $hook can be: a function, an object, an array of $function and $data,
163 - * an array of just a function, an array of object and method, or an
164 - * array of object, method, and data.
 144+
 145+ /**
 146+ * $hook can be: a function, an object, an array of $function and
 147+ * $data, an array of just a function, an array of object and
 148+ * method, or an array of object, method, and data.
165149 */
166 -
167150 if ( is_array( $hook ) ) {
168151 if ( count( $hook ) < 1 ) {
169 - throw new MWException("Empty array in hooks for " . $event . "\n");
170 - } else if ( is_object( $hook[0] ) ) {
 152+ throw new MWException( 'Empty array in hooks for ' . $event . "\n" );
 153+ } elseif ( is_object( $hook[0] ) ) {
171154 $object = $hook_array[$event][$index][0];
172155 if ( $object instanceof Closure ) {
173156 $closure = true;
@@ -176,7 +159,7 @@
177160 }
178161 } else {
179162 if ( count( $hook ) < 2 ) {
180 - $method = "on" . $event;
 163+ $method = 'on' . $event;
181164 } else {
182165 $method = $hook[1];
183166 if ( count( $hook ) > 2 ) {
@@ -185,18 +168,18 @@
186169 }
187170 }
188171 }
189 - } else if ( is_string( $hook[0] ) ) {
 172+ } elseif ( is_string( $hook[0] ) ) {
190173 $func = $hook[0];
191174 if ( count( $hook ) > 1) {
192175 $data = $hook[1];
193176 $have_data = true;
194177 }
195178 } else {
196 - throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
 179+ throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
197180 }
198 - } else if ( is_string( $hook ) ) { # functions look like strings, too
 181+ } elseif ( is_string( $hook ) ) { # functions look like strings, too
199182 $func = $hook;
200 - } else if ( is_object( $hook ) ) {
 183+ } elseif ( is_object( $hook ) ) {
201184 $object = $hook_array[$event][$index];
202185 if ( $object instanceof Closure ) {
203186 $closure = true;
@@ -204,17 +187,16 @@
205188 $method = "on" . $event;
206189 }
207190 } else {
208 - throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
 191+ throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
209192 }
210 -
 193+
211194 /* We put the first data element on, if needed. */
212 -
213195 if ( $have_data ) {
214 - $hook_args = array_merge(array($data), $args);
 196+ $hook_args = array_merge( array( $data ), $args );
215197 } else {
216198 $hook_args = $args;
217199 }
218 -
 200+
219201 if ( $closure ) {
220202 $callback = $object;
221203 $func = "hook-$event-closure";
@@ -226,11 +208,12 @@
227209 } else {
228210 $callback = $func;
229211 }
230 -
 212+
231213 // Run autoloader (workaround for call_user_func_array bug)
232214 is_callable( $callback );
233 -
234 - /* Call the hook. The documentation of call_user_func_array clearly
 215+
 216+ /**
 217+ * Call the hook. The documentation of call_user_func_array clearly
235218 * states that FALSE is returned on failure. However this is not
236219 * case always. In some version of PHP if the function signature
237220 * does not match the call signature, PHP will issue an warning:
@@ -240,7 +223,7 @@
241224 * catches that warning and provides better error message. The
242225 * function documentation also says that:
243226 * In other words, it does not depend on the function signature
244 - * whether the parameter is passed by a value or by a reference.
 227+ * whether the parameter is passed by a value or by a reference.
245228 * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
246229 * is unsurprisingly marked as bogus. In short handling of failures
247230 * with call_user_func_array is a failure, the documentation for that
@@ -257,7 +240,7 @@
258241 }
259242 wfProfileOut( $func );
260243 restore_error_handler();
261 -
 244+
262245 /* String return is an error; false return means stop processing. */
263246 if ( is_string( $retval ) ) {
264247 global $wgOut;
@@ -277,22 +260,32 @@
278261 $prettyFunc = strval( $callback );
279262 }
280263 if ( $badhookmsg ) {
281 - throw new MWException( "Detected bug in an extension! " .
282 - "Hook $prettyFunc has invalid call signature; " . $badhookmsg );
 264+ throw new MWException(
 265+ 'Detected bug in an extension! ' .
 266+ "Hook $prettyFunc has invalid call signature; " . $badhookmsg
 267+ );
283268 } else {
284 - throw new MWException( "Detected bug in an extension! " .
 269+ throw new MWException(
 270+ 'Detected bug in an extension! ' .
285271 "Hook $prettyFunc failed to return a value; " .
286 - "should return true to continue hook processing or false to abort." );
 272+ 'should return true to continue hook processing or false to abort.'
 273+ );
287274 }
288275 } else if ( !$retval ) {
289276 return false;
290277 }
291278 }
292 -
 279+
293280 return true;
294281 }
295 -
296 - //This REALLY should be protected... but it's public for compatibility
 282+
 283+ /**
 284+ * This REALLY should be protected... but it's public for compatibility
 285+ *
 286+ * @param $errno Unused
 287+ * @param $errstr String: error message
 288+ * @return Boolean: false
 289+ */
297290 public static function hookErrorHandler( $errno, $errstr ) {
298291 if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
299292 throw new MWHookException( $errstr );

Status & tagging log