| Conditions | 16 |
| Paths | 194 |
| Total Lines | 129 |
| Code Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 2 | 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 |
||
| 168 | protected function blockMedia($Excerpt) |
||
| 169 | { |
||
| 170 | if (1 !== preg_match($this->MarkdownMediaRegex, $Excerpt['text'])) { |
||
| 171 | return; |
||
| 172 | } |
||
| 173 | |||
| 174 | $InlineImage = $this->inlineImage($Excerpt); |
||
| 175 | if (!isset($InlineImage)) { |
||
| 176 | return; |
||
| 177 | } |
||
| 178 | $block = $InlineImage; |
||
| 179 | |||
| 180 | switch ($block['element']['attributes']['alt']) { |
||
| 181 | case 'audio': |
||
| 182 | $audio = []; |
||
| 183 | $audio['element'] = [ |
||
| 184 | 'name' => 'audio', |
||
| 185 | 'handler' => 'element', |
||
| 186 | ]; |
||
| 187 | $audio['element']['attributes'] = $block['element']['attributes']; |
||
| 188 | unset($audio['element']['attributes']['loading']); |
||
| 189 | $block = $audio; |
||
| 190 | unset($block['element']['attributes']['alt']); |
||
| 191 | break; |
||
| 192 | case 'video': |
||
| 193 | $video = []; |
||
| 194 | $video['element'] = [ |
||
| 195 | 'name' => 'video', |
||
| 196 | 'handler' => 'element', |
||
| 197 | ]; |
||
| 198 | $video['element']['attributes'] = $block['element']['attributes']; |
||
| 199 | unset($video['element']['attributes']['loading']); |
||
| 200 | if (isset($block['element']['attributes']['poster'])) { |
||
| 201 | $video['element']['attributes']['poster'] = new Asset($this->builder, $block['element']['attributes']['poster'], ['force_slash' => false]); |
||
| 202 | } |
||
| 203 | $block = $video; |
||
| 204 | unset($block['element']['attributes']['alt']); |
||
| 205 | } |
||
| 206 | |||
| 207 | /* |
||
| 208 | <!-- if image has a title: a <figure> is required for <figcaption> --> |
||
| 209 | <figure> |
||
| 210 | <!-- if WebP: a <picture> is required for <source> --> |
||
| 211 | <picture> |
||
| 212 | <source type="image/webp" |
||
| 213 | srcset="..." |
||
| 214 | sizes="..." |
||
| 215 | > |
||
| 216 | <img src="..." |
||
| 217 | srcset="..." |
||
| 218 | sizes="..." |
||
| 219 | > |
||
| 220 | </picture> |
||
| 221 | <!-- title --> |
||
| 222 | <figcaption>...</figcaption> |
||
| 223 | </figure> |
||
| 224 | */ |
||
| 225 | |||
| 226 | // creates a <picture> used to add WebP <source> in addition to the image <img> element |
||
| 227 | if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false |
||
| 228 | && ($InlineImage['element']['attributes']['src'])['type'] == 'image' |
||
| 229 | && ($InlineImage['element']['attributes']['src'])['subtype'] != 'image/webp') { |
||
| 230 | try { |
||
| 231 | // Image src must be an Asset instance |
||
| 232 | if (is_string($InlineImage['element']['attributes']['src'])) { |
||
| 233 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 234 | } |
||
| 235 | // Image asset is an animated GIF |
||
| 236 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
| 237 | throw new RuntimeException(\sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 238 | } |
||
| 239 | $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75); |
||
| 240 | $srcset = ''; |
||
| 241 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 242 | $srcset = Image::buildSrcset( |
||
| 243 | $assetWebp, |
||
| 244 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | if (empty($srcset)) { |
||
| 248 | $srcset = (string) $assetWebp; |
||
| 249 | } |
||
| 250 | $PictureBlock = [ |
||
| 251 | 'element' => [ |
||
| 252 | 'name' => 'picture', |
||
| 253 | 'handler' => 'elements', |
||
| 254 | ], |
||
| 255 | ]; |
||
| 256 | $source = [ |
||
| 257 | 'element' => [ |
||
| 258 | 'name' => 'source', |
||
| 259 | 'attributes' => [ |
||
| 260 | 'type' => 'image/webp', |
||
| 261 | 'srcset' => $srcset, |
||
| 262 | 'sizes' => $this->builder->getConfig()->get('assets.images.responsive.sizes.default'), |
||
| 263 | ], |
||
| 264 | ], |
||
| 265 | ]; |
||
| 266 | $PictureBlock['element']['text'][] = $source['element']; |
||
| 267 | $PictureBlock['element']['text'][] = $InlineImage['element']; |
||
| 268 | $block = $PictureBlock; |
||
| 269 | } catch (\Exception $e) { |
||
| 270 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | // if there is a title: put the <img> (or <picture>) in a <figure> element to use the <figcaption> |
||
| 275 | if ($this->builder->getConfig()->get('body.images.caption.enabled') && !empty($InlineImage['element']['attributes']['title'])) { |
||
| 276 | $FigureBlock = [ |
||
| 277 | 'element' => [ |
||
| 278 | 'name' => 'figure', |
||
| 279 | 'handler' => 'elements', |
||
| 280 | 'text' => [ |
||
| 281 | $block['element'], |
||
| 282 | ], |
||
| 283 | ], |
||
| 284 | ]; |
||
| 285 | $InlineFigcaption = [ |
||
| 286 | 'element' => [ |
||
| 287 | 'name' => 'figcaption', |
||
| 288 | 'text' => $InlineImage['element']['attributes']['title'], |
||
| 289 | ], |
||
| 290 | ]; |
||
| 291 | $FigureBlock['element']['text'][] = $InlineFigcaption['element']; |
||
| 292 | |||
| 293 | return $FigureBlock; |
||
| 294 | } |
||
| 295 | |||
| 296 | return $block; |
||
| 297 | } |
||
| 422 |