Conditions | 13 |
Paths | 119 |
Total Lines | 78 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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; |
||
|
|||
187 | $badhookmsg = null; |
||
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 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.