r70109 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70108‎ | r70109 | r70110 >
Date:21:05, 28 July 2010
Author:nikerabbit
Status:ok (Comments)
Tags:
Comment:
Better error message if hook function signature does not match parameters.

Also took the opportunity to write a short essay why this made me annoyed.
Modified paths:
  • /trunk/phase3/includes/Hooks.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Hooks.php
@@ -55,6 +55,7 @@
5656 $data = null;
5757 $have_data = false;
5858 $closure = false;
 59+ $badhookmsg = false;
5960
6061 /* $hook can be: a function, an object, an array of $function and $data,
6162 * an array of just a function, an array of object and method, or an
@@ -128,10 +129,34 @@
129130 // Run autoloader (workaround for call_user_func_array bug)
130131 is_callable( $callback );
131132
132 - /* Call the hook. */
 133+ /* Call the hook. The documentation of call_user_func_array clearly
 134+ * states that FALSE is returned on failure. However this is not
 135+ * case always. In some version of PHP if the function signature
 136+ * does not match the call signature, PHP will issue an warning:
 137+ * Param y in x expected to be a reference, value given.
 138+ *
 139+ * In that case the call will also return null. The following code
 140+ * catches that warning and provides better error message. The
 141+ * function documentation also says that:
 142+ * In other words, it does not depend on the function signature
 143+ * whether the parameter is passed by a value or by a reference.
 144+ * There is also PHP bug http://bugs.php.net/bug.php?id=47554 which
 145+ * is unsurprisingly marked as bogus. In short handling of failures
 146+ * with call_user_func_array is a failure, the documentation for that
 147+ * function is wrong and misleading and PHP developers don't see any
 148+ * problem here.
 149+ */
 150+ $retval = null;
 151+ $handler = set_error_handler( 'hookErrorHandler' );
133152 wfProfileIn( $func );
134 - $retval = call_user_func_array( $callback, $hook_args );
 153+ try {
 154+ $retval = call_user_func_array( $callback, $hook_args );
 155+ } catch ( MWHookException $e ) {
 156+ $badhookmsg = $e->getMessage();
 157+ }
135158 wfProfileOut( $func );
 159+ // Need to check for null, because set_error_handler borks on it... sigh
 160+ if ( $handler !== null ) set_error_handler( $handler );
136161
137162 /* String return is an error; false return means stop processing. */
138163
@@ -152,9 +177,14 @@
153178 } else {
154179 $prettyFunc = strval( $callback );
155180 }
156 - throw new MWException( "Detected bug in an extension! " .
157 - "Hook $prettyFunc failed to return a value; " .
158 - "should return true to continue hook processing or false to abort." );
 181+ if ( $badhookmsg ) {
 182+ throw new MWException( "Detected bug in an extension! " .
 183+ "Hook $prettyFunc has invalid call signature; " . $badhookmsg );
 184+ } else {
 185+ throw new MWException( "Detected bug in an extension! " .
 186+ "Hook $prettyFunc failed to return a value; " .
 187+ "should return true to continue hook processing or false to abort." );
 188+ }
159189 } else if ( !$retval ) {
160190 return false;
161191 }
@@ -162,3 +192,12 @@
163193
164194 return true;
165195 }
 196+
 197+function hookErrorHandler( $errno, $errstr ) {
 198+ if ( strpos( $errstr, 'expected to be a reference, value given' ) !== false ) {
 199+ throw new MWHookException( $errstr );
 200+ }
 201+ return false;
 202+}
 203+
 204+class MWHookException extends MWException {}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r70406Follow-up r70109: use restore_error_handler()nikerabbit19:46, 3 August 2010

Comments

#Comment by 😂 (talk | contribs)   18:29, 7 February 2011

This annoyed me the first time I hit it. But it's correct and keeps people from accidentally breaking hooks with reference changes.

Status & tagging log