| Conditions | 10 |
| Paths | 32 |
| Total Lines | 56 |
| Code Lines | 40 |
| 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 |
||
| 103 | function performAction( User $user ) { |
||
| 104 | if ( empty( $this->mode ) ) { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | if ( !in_array( $this->func_name, $this->config->get( 'AjaxExportList' ) ) ) { |
||
| 109 | wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" ); |
||
| 110 | wfHttpError( |
||
| 111 | 400, |
||
| 112 | 'Bad Request', |
||
| 113 | "unknown function " . $this->func_name |
||
| 114 | ); |
||
| 115 | } elseif ( !User::isEveryoneAllowed( 'read' ) && !$user->isAllowed( 'read' ) ) { |
||
| 116 | wfHttpError( |
||
| 117 | 403, |
||
| 118 | 'Forbidden', |
||
| 119 | 'You are not allowed to view pages.' ); |
||
| 120 | } else { |
||
| 121 | wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" ); |
||
| 122 | try { |
||
| 123 | $result = call_user_func_array( $this->func_name, $this->args ); |
||
| 124 | |||
| 125 | if ( $result === false || $result === null ) { |
||
| 126 | wfDebug( __METHOD__ . ' ERROR while dispatching ' . |
||
| 127 | $this->func_name . "(" . var_export( $this->args, true ) . "): " . |
||
| 128 | "no data returned\n" ); |
||
| 129 | |||
| 130 | wfHttpError( 500, 'Internal Error', |
||
| 131 | "{$this->func_name} returned no data" ); |
||
| 132 | } else { |
||
| 133 | if ( is_string( $result ) ) { |
||
| 134 | $result = new AjaxResponse( $result ); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Make sure DB commit succeeds before sending a response |
||
| 138 | wfGetLBFactory()->commitMasterChanges( __METHOD__ ); |
||
| 139 | |||
| 140 | $result->sendHeaders(); |
||
| 141 | $result->printText(); |
||
| 142 | |||
| 143 | wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" ); |
||
| 144 | } |
||
| 145 | } catch ( Exception $e ) { |
||
| 146 | wfDebug( __METHOD__ . ' ERROR while dispatching ' . |
||
| 147 | $this->func_name . "(" . var_export( $this->args, true ) . "): " . |
||
| 148 | get_class( $e ) . ": " . $e->getMessage() . "\n" ); |
||
| 149 | |||
| 150 | if ( !headers_sent() ) { |
||
| 151 | wfHttpError( 500, 'Internal Error', |
||
| 152 | $e->getMessage() ); |
||
| 153 | } else { |
||
| 154 | print $e->getMessage(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: