| Conditions | 13 |
| Paths | 41 |
| Total Lines | 60 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 33 | protected function inlineImage($excerpt) |
||
| 34 | { |
||
| 35 | $image = parent::inlineImage($excerpt); |
||
| 36 | if (!isset($image)) { |
||
| 37 | return null; |
||
| 38 | } |
||
| 39 | $image['element']['attributes']['src'] = $imageSource = trim($this->removeQuery($image['element']['attributes']['src'])); |
||
| 40 | $asset = new Asset($this->builder, $image['element']['attributes']['src']); |
||
| 41 | $width = $asset->getWidth(); |
||
| 42 | /** |
||
| 43 | * Should be lazy loaded? |
||
| 44 | */ |
||
| 45 | if ($this->builder->getConfig()->get('content.images.lazy.enabled')) { |
||
| 46 | $image['element']['attributes']['loading'] = 'lazy'; |
||
| 47 | } |
||
| 48 | /** |
||
| 49 | * Should be resized? |
||
| 50 | */ |
||
| 51 | if ($image['element']['attributes']['width'] !== null |
||
| 52 | && (int) $image['element']['attributes']['width'] < $width |
||
| 53 | && $this->builder->getConfig()->get('content.images.resize.enabled') |
||
| 54 | ) { |
||
| 55 | $width = (int) $image['element']['attributes']['width']; |
||
| 56 | $imageResized = $asset->resize($width); |
||
| 57 | $image['element']['attributes']['src'] = $imageResized; |
||
| 58 | } |
||
| 59 | if ($image['element']['attributes']['width'] === null) { |
||
| 60 | $image['element']['attributes']['width'] = $width; |
||
| 61 | } |
||
| 62 | /** |
||
| 63 | * Should be responsive? |
||
| 64 | */ |
||
| 65 | if ($this->builder->getConfig()->get('content.images.responsive.enabled')) { |
||
| 66 | $steps = $this->builder->getConfig()->get('content.images.responsive.width.steps'); |
||
| 67 | $wMin = $this->builder->getConfig()->get('content.images.responsive.width.min'); |
||
| 68 | $wMax = $this->builder->getConfig()->get('content.images.responsive.width.max'); |
||
| 69 | $srcset = ''; |
||
| 70 | for ($i = 1; $i <= $steps; $i++) { |
||
| 71 | $w = ceil($wMin * $i); |
||
| 72 | if ($w > $width || $w > $wMax) { |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | $a = new Asset($this->builder, $imageSource); |
||
| 76 | $img = $a->resize($w); |
||
|
|
|||
| 77 | $srcset .= sprintf('%s %sw', $img, $w); |
||
| 78 | if ($i < $steps) { |
||
| 79 | $srcset .= ', '; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | if ($imageResized) { |
||
| 83 | $srcset .= sprintf(',%s %sw', $imageResized, $width); |
||
| 84 | } else { |
||
| 85 | $srcset .= sprintf(',%s %sw', $imageSource, $width); |
||
| 86 | } |
||
| 87 | // ie: srcset="/img-480.jpg 480w, /img-800.jpg 800w" |
||
| 88 | $image['element']['attributes']['srcset'] = $srcset; |
||
| 89 | $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('content.images.responsive.sizes.default'); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $image; |
||
| 93 | } |
||
| 137 |