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