| 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 | 1 | <img src="..." |
|
| 198 | srcset="..." |
||
| 199 | 1 | sizes="..." |
|
| 200 | > |
||
| 201 | </picture> |
||
| 202 | <!-- title --> |
||
| 203 | <figcaption>...</figcaption> |
||
| 204 | 1 | </figure> |
|
| 205 | */ |
||
| 206 | |||
| 207 | // creates a <picture> used to add WebP <source> in addition to the image <img> element |
||
| 208 | if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false && ($InlineImage['element']['attributes']['src'])['subtype'] != 'image/webp') { |
||
| 209 | try { |
||
| 210 | // Image src must be an Asset instance |
||
| 211 | if (is_string($InlineImage['element']['attributes']['src'])) { |
||
| 212 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 213 | } |
||
| 214 | // Image asset is an animated GIF |
||
| 215 | if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
||
| 216 | throw new RuntimeException(\sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP', $InlineImage['element']['attributes']['src'])); |
||
| 217 | } |
||
| 218 | $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75); |
||
| 219 | $srcset = ''; |
||
| 220 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 221 | $srcset = Image::buildSrcset( |
||
| 222 | $assetWebp, |
||
| 223 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | if (empty($srcset)) { |
||
| 227 | $srcset = (string) $assetWebp; |
||
| 228 | } |
||
| 229 | $PictureBlock = [ |
||
| 230 | 'element' => [ |
||
| 231 | 'name' => 'picture', |
||
| 232 | 'handler' => 'elements', |
||
| 233 | ], |
||
| 234 | ]; |
||
| 235 | $source = [ |
||
| 236 | 'element' => [ |
||
| 237 | 'name' => 'source', |
||
| 238 | 'attributes' => [ |
||
| 239 | 'type' => 'image/webp', |
||
| 240 | 'srcset' => $srcset, |
||
| 241 | 'sizes' => $this->builder->getConfig()->get('assets.images.responsive.sizes.default'), |
||
| 242 | ], |
||
| 243 | ], |
||
| 244 | ]; |
||
| 245 | $PictureBlock['element']['text'][] = $source['element']; |
||
| 246 | $PictureBlock['element']['text'][] = $InlineImage['element']; |
||
| 247 | $block = $PictureBlock; |
||
| 248 | } catch (\Exception $e) { |
||
| 249 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | // if there is a title: put the <img> (or <picture>) in a <figure> element to use the <figcaption> |
||
| 254 | if ($this->builder->getConfig()->get('body.images.caption.enabled') && !empty($InlineImage['element']['attributes']['title'])) { |
||
| 255 | $FigureBlock = [ |
||
| 256 | 'element' => [ |
||
| 257 | 'name' => 'figure', |
||
| 258 | 'handler' => 'elements', |
||
| 259 | 'text' => [ |
||
| 260 | $block['element'], |
||
| 261 | ], |
||
| 262 | ], |
||
| 263 | ]; |
||
| 264 | $InlineFigcaption = [ |
||
| 265 | 'element' => [ |
||
| 266 | 'name' => 'figcaption', |
||
| 267 | 'text' => $InlineImage['element']['attributes']['title'], |
||
| 268 | ], |
||
| 269 | ]; |
||
| 270 | $FigureBlock['element']['text'][] = $InlineFigcaption['element']; |
||
| 271 | |||
| 272 | return $FigureBlock; |
||
| 273 | } |
||
| 274 | |||
| 275 | return $block; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Note block-level markup. |
||
| 280 | * |
||
| 281 | * :::tip |
||
| 282 | * **Tip:** This is an advice. |
||
| 283 | * ::: |
||
| 284 | * |
||
| 285 | * Code inspired by https://github.com/sixlive/parsedown-alert from TJ Miller (@sixlive). |
||
| 286 | */ |
||
| 287 | protected function blockNote($block) |
||
| 288 | { |
||
| 289 | if (preg_match('/:::(.*)/', $block['text'], $matches)) { |
||
| 290 | return [ |
||
| 291 | 'char' => ':', |
||
| 292 | 'element' => [ |
||
| 293 | 'name' => 'aside', |
||
| 294 | 'text' => '', |
||
| 295 | 'attributes' => [ |
||
| 296 | 'class' => "note note-{$matches[1]}", |
||
| 297 | ], |
||
| 298 | ], |
||
| 299 | ]; |
||
| 300 | } |
||
| 301 | } |
||
| 396 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.