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