| Conditions | 11 |
| Paths | 50 |
| Total Lines | 104 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| 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 |
||
| 197 | protected function blockImage($Line) |
||
| 198 | { |
||
| 199 | if (1 !== preg_match($this->MarkdownImageRegex, $Line['text'], $matches)) { |
||
| 200 | return; |
||
| 201 | } |
||
| 202 | |||
| 203 | // DEBUG |
||
| 204 | dump($matches[1]); |
||
| 205 | |||
| 206 | $InlineImage = $this->inlineImage($Line); |
||
| 207 | if (!isset($InlineImage)) { |
||
| 208 | return; |
||
| 209 | } |
||
| 210 | |||
| 211 | $block = $InlineImage; |
||
| 212 | |||
| 213 | /* |
||
| 214 | <!-- if image has a title: a <figure> is required for <figcaption> --> |
||
| 215 | <figure> |
||
| 216 | <!-- if WebP: a <picture> is required for <source> --> |
||
| 217 | <picture> |
||
| 218 | <source type="image/webp" |
||
| 219 | srcset="..." |
||
| 220 | sizes="..." |
||
| 221 | > |
||
| 222 | <img src="..." |
||
| 223 | srcset="..." |
||
| 224 | sizes="..." |
||
| 225 | > |
||
| 226 | </picture> |
||
| 227 | <!-- title --> |
||
| 228 | <figcaption>...</figcaption> |
||
| 229 | </figure> |
||
| 230 | */ |
||
| 231 | |||
| 232 | // creates a <picture> used to add WebP <source> in addition to the image <img> element |
||
| 233 | if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false && ($InlineImage['element']['attributes']['src'])['subtype'] != 'image/webp') { |
||
| 234 | try { |
||
| 235 | // Image src must be an Asset instance |
||
| 236 | if (is_string($InlineImage['element']['attributes']['src'])) { |
||
| 237 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 238 | } |
||
| 239 | // Image asset is an animated GIF |
||
| 240 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
| 241 | throw new RuntimeException(\sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 242 | } |
||
| 243 | $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75); |
||
| 244 | $srcset = ''; |
||
| 245 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 246 | $srcset = Image::buildSrcset( |
||
| 247 | $assetWebp, |
||
| 248 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | if (empty($srcset)) { |
||
| 252 | $srcset = (string) $assetWebp; |
||
| 253 | } |
||
| 254 | $PictureBlock = [ |
||
| 255 | 'element' => [ |
||
| 256 | 'name' => 'picture', |
||
| 257 | 'handler' => 'elements', |
||
| 258 | ], |
||
| 259 | ]; |
||
| 260 | $source = [ |
||
| 261 | 'element' => [ |
||
| 262 | 'name' => 'source', |
||
| 263 | 'attributes' => [ |
||
| 264 | 'type' => 'image/webp', |
||
| 265 | 'srcset' => $srcset, |
||
| 266 | 'sizes' => $this->builder->getConfig()->get('assets.images.responsive.sizes.default'), |
||
| 267 | ], |
||
| 268 | ], |
||
| 269 | ]; |
||
| 270 | $PictureBlock['element']['text'][] = $source['element']; |
||
| 271 | $PictureBlock['element']['text'][] = $InlineImage['element']; |
||
| 272 | $block = $PictureBlock; |
||
| 273 | } catch (\Exception $e) { |
||
| 274 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | // if there is a title: put the <img> (or <picture>) in a <figure> element to use the <figcaption> |
||
| 279 | if (!empty($InlineImage['element']['attributes']['title'])) { |
||
| 280 | $FigureBlock = [ |
||
| 281 | 'element' => [ |
||
| 282 | 'name' => 'figure', |
||
| 283 | 'handler' => 'elements', |
||
| 284 | 'text' => [ |
||
| 285 | $block['element'], |
||
| 286 | ], |
||
| 287 | ], |
||
| 288 | ]; |
||
| 289 | $InlineFigcaption = [ |
||
| 290 | 'element' => [ |
||
| 291 | 'name' => 'figcaption', |
||
| 292 | 'text' => $InlineImage['element']['attributes']['title'], |
||
| 293 | ], |
||
| 294 | ]; |
||
| 295 | $FigureBlock['element']['text'][] = $InlineFigcaption['element']; |
||
| 296 | |||
| 297 | return $FigureBlock; |
||
| 298 | } |
||
| 299 | |||
| 300 | return $block; |
||
| 301 | } |
||
| 386 |