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