| Conditions | 12 |
| Paths | 52 |
| Total Lines | 64 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | 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 | protected function inlineImage($excerpt) |
||
| 41 | { |
||
| 42 | $image = parent::inlineImage($excerpt); |
||
| 43 | if (!isset($image)) { |
||
| 44 | return null; |
||
| 45 | } |
||
| 46 | // clean source path / URL |
||
| 47 | $image['element']['attributes']['src'] = trim($this->removeQuery($image['element']['attributes']['src'])); |
||
| 48 | // create asset |
||
| 49 | $asset = new Asset($this->builder, $image['element']['attributes']['src']); |
||
| 50 | // is asset is valid? (if yes get width) |
||
| 51 | if (false === $width = $asset->getWidth()) { |
||
| 52 | return $image; |
||
| 53 | } |
||
| 54 | $image['element']['attributes']['src'] = $asset; |
||
| 55 | /** |
||
| 56 | * Should be lazy loaded? |
||
| 57 | */ |
||
| 58 | if ($this->builder->getConfig()->get('body.images.lazy.enabled')) { |
||
| 59 | $image['element']['attributes']['loading'] = 'lazy'; |
||
| 60 | } |
||
| 61 | /** |
||
| 62 | * Should be resized? |
||
| 63 | */ |
||
| 64 | $assetResized = null; |
||
| 65 | if (isset($image['element']['attributes']['width']) |
||
| 66 | && (int) $image['element']['attributes']['width'] < $width |
||
| 67 | && $this->builder->getConfig()->get('body.images.resize.enabled') |
||
| 68 | ) { |
||
| 69 | $width = (int) $image['element']['attributes']['width']; |
||
| 70 | |||
| 71 | try { |
||
| 72 | $assetResized = $asset->resize($width); |
||
| 73 | } catch (\Exception $e) { |
||
| 74 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 75 | |||
| 76 | return $image; |
||
| 77 | } |
||
| 78 | $image['element']['attributes']['src'] = $assetResized; |
||
| 79 | } |
||
| 80 | // set width |
||
| 81 | if (!isset($image['element']['attributes']['width'])) { |
||
| 82 | $image['element']['attributes']['width'] = $width; |
||
| 83 | } |
||
| 84 | // set height |
||
| 85 | if (!isset($image['element']['attributes']['height'])) { |
||
| 86 | $image['element']['attributes']['height'] = $asset->getHeight(); |
||
| 87 | } |
||
| 88 | /** |
||
| 89 | * Should be responsive? |
||
| 90 | */ |
||
| 91 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 92 | if ($srcset = Image::getSrcset( |
||
| 93 | $assetResized ?? $asset, |
||
| 94 | $this->builder->getConfig()->get('assets.images.responsive.width.steps') ?? 5, |
||
| 95 | $this->builder->getConfig()->get('assets.images.responsive.width.min') ?? 320, |
||
| 96 | $this->builder->getConfig()->get('assets.images.responsive.width.max') ?? 1280 |
||
| 97 | )) { |
||
| 98 | $image['element']['attributes']['srcset'] = $srcset; |
||
| 99 | $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('assets.images.responsive.sizes.default'); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | return $image; |
||
| 104 | } |
||
| 239 |