Conditions | 13 |
Paths | 32 |
Total Lines | 97 |
Code Lines | 58 |
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 |
||
199 | public function closePrinter() { |
||
200 | if ( $this->mDisabled ) { |
||
201 | return; |
||
202 | } |
||
203 | |||
204 | $mime = $this->getMimeType(); |
||
205 | if ( $this->getIsHtml() && $mime !== null ) { |
||
206 | $format = $this->getFormat(); |
||
207 | $lcformat = strtolower( $format ); |
||
208 | $result = $this->getBuffer(); |
||
209 | |||
210 | $context = new DerivativeContext( $this->getMain() ); |
||
211 | $context->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'apioutput' ) ); |
||
212 | $context->setTitle( SpecialPage::getTitleFor( 'ApiHelp' ) ); |
||
213 | $out = new OutputPage( $context ); |
||
214 | $context->setOutput( $out ); |
||
215 | |||
216 | $out->addModuleStyles( 'mediawiki.apipretty' ); |
||
217 | $out->setPageTitle( $context->msg( 'api-format-title' ) ); |
||
218 | |||
219 | if ( !$this->getIsWrappedHtml() ) { |
||
220 | // When the format without suffix 'fm' is defined, there is a non-html version |
||
221 | if ( $this->getMain()->getModuleManager()->isDefined( $lcformat, 'format' ) ) { |
||
222 | $msg = $context->msg( 'api-format-prettyprint-header' )->params( $format, $lcformat ); |
||
223 | } else { |
||
224 | $msg = $context->msg( 'api-format-prettyprint-header-only-html' )->params( $format ); |
||
225 | } |
||
226 | |||
227 | $header = $msg->parseAsBlock(); |
||
228 | $out->addHTML( |
||
229 | Html::rawElement( 'div', [ 'class' => 'api-pretty-header' ], |
||
230 | ApiHelp::fixHelpLinks( $header ) |
||
231 | ) |
||
232 | ); |
||
233 | |||
234 | if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) { |
||
235 | $out->addHTML( |
||
236 | Html::rawElement( 'div', [ 'class' => 'api-pretty-header api-pretty-status' ], |
||
237 | $this->msg( |
||
238 | 'api-format-prettyprint-status', |
||
239 | $this->mHttpStatus, |
||
240 | HttpStatus::getMessage( $this->mHttpStatus ) |
||
241 | )->parse() |
||
242 | ) |
||
243 | ); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | if ( Hooks::run( 'ApiFormatHighlight', [ $context, $result, $mime, $format ] ) ) { |
||
248 | $out->addHTML( |
||
249 | Html::element( 'pre', [ 'class' => 'api-pretty-content' ], $result ) |
||
250 | ); |
||
251 | } |
||
252 | |||
253 | if ( $this->getIsWrappedHtml() ) { |
||
254 | // This is a special output mode mainly intended for ApiSandbox use |
||
255 | $time = microtime( true ) - $this->getConfig()->get( 'RequestTime' ); |
||
256 | $json = FormatJson::encode( |
||
257 | [ |
||
258 | 'status' => (int)( $this->mHttpStatus ?: 200 ), |
||
259 | 'statustext' => HttpStatus::getMessage( $this->mHttpStatus ?: 200 ), |
||
260 | 'html' => $out->getHTML(), |
||
261 | 'modules' => array_values( array_unique( array_merge( |
||
262 | $out->getModules(), |
||
263 | $out->getModuleScripts(), |
||
264 | $out->getModuleStyles() |
||
265 | ) ) ), |
||
266 | 'continue' => $this->getResult()->getResultData( 'continue' ), |
||
267 | 'time' => round( $time * 1000 ), |
||
268 | ], |
||
269 | false, FormatJson::ALL_OK |
||
270 | ); |
||
271 | |||
272 | // Bug 66776: wfMangleFlashPolicy() is needed to avoid a nasty bug in |
||
273 | // Flash, but what it does isn't friendly for the API, so we need to |
||
274 | // work around it. |
||
275 | if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $json ) ) { |
||
276 | $json = preg_replace( |
||
277 | '/\<(\s*cross-domain-policy\s*)\>/i', '\\u003C$1\\u003E', $json |
||
278 | ); |
||
279 | } |
||
280 | |||
281 | echo $json; |
||
282 | } else { |
||
283 | // API handles its own clickjacking protection. |
||
284 | // Note, that $wgBreakFrames will still override $wgApiFrameOptions for format mode. |
||
285 | $out->allowClickjacking(); |
||
286 | $out->output(); |
||
287 | } |
||
288 | } else { |
||
289 | // For non-HTML output, clear all errors that might have been |
||
290 | // displayed if display_errors=On |
||
291 | ob_clean(); |
||
292 | |||
293 | echo $this->getBuffer(); |
||
294 | } |
||
295 | } |
||
296 | |||
342 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.