| Conditions | 11 |
| Paths | 52 |
| Total Lines | 45 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 40 | public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = array()) { |
||
| 41 | // Find appropriate record, with fallback for error handlers |
||
| 42 | $record = static::find_shortcode_record($args, $errorCode); |
||
| 43 | if($errorCode) { |
||
|
|
|||
| 44 | $record = static::find_error_record($errorCode); |
||
| 45 | } |
||
| 46 | if (!$record) { |
||
| 47 | return null; // There were no suitable matches at all. |
||
| 48 | } |
||
| 49 | |||
| 50 | // Check if a resize is required |
||
| 51 | $src = $record->Link(); |
||
| 52 | if($record instanceof Image) { |
||
| 53 | $width = isset($args['width']) ? $args['width'] : null; |
||
| 54 | $height = isset($args['height']) ? $args['height'] : null; |
||
| 55 | $hasCustomDimensions = ($width && $height); |
||
| 56 | if ($hasCustomDimensions && (($width != $record->getWidth()) || ($height != $record->getHeight()))) { |
||
| 57 | $resized = $record->ResizedImage($width, $height); |
||
| 58 | // Make sure that the resized image actually returns an image |
||
| 59 | if($resized) { |
||
| 60 | $src = $resized->getURL(); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | // Build the HTML tag |
||
| 66 | $attrs = array_merge( |
||
| 67 | // Set overrideable defaults |
||
| 68 | ['src' => '', 'alt' => $record->Title], |
||
| 69 | // Use all other shortcode arguments |
||
| 70 | $args, |
||
| 71 | // But enforce some values |
||
| 72 | ['id' => '', 'src' => $src] |
||
| 73 | ); |
||
| 74 | |||
| 75 | // Clean out any empty attributes |
||
| 76 | $attrs = array_filter($attrs, function($v) {return (bool)$v;}); |
||
| 77 | |||
| 78 | // Condense to HTML attribute string |
||
| 79 | $attrsStr = join(' ', array_map(function($name) use ($attrs) { |
||
| 80 | return Convert::raw2att($name) . '="' . Convert::raw2att($attrs[$name]) . '"'; |
||
| 81 | }, array_keys($attrs))); |
||
| 82 | |||
| 83 | return '<img ' . $attrsStr . ' />'; |
||
| 84 | } |
||
| 85 | |||
| 124 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: