Total Complexity | 51 |
Total Lines | 371 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 5 | Features | 0 |
Complex classes like Parsedown often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Parsedown, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Parsedown extends \ParsedownToC |
||
24 | { |
||
25 | /** @var Builder */ |
||
26 | protected $builder; |
||
27 | |||
28 | /** {@inheritdoc} */ |
||
29 | protected $regexAttribute = '(?:[#.][-\w:\\\]+[ ]*|[-\w:\\\]+(?:=(?:["\'][^\n]*?["\']|[^\s]+)?)?[ ]*)'; |
||
30 | |||
31 | /** Valid a media block (image, audio or video) */ |
||
32 | protected $MarkdownMediaRegex = "~^!\[.*?\]\(.*?\)\|*(audio|video)*~"; |
||
33 | |||
34 | /** @var Highlighter */ |
||
35 | protected $highlighter; |
||
36 | |||
37 | public function __construct(Builder $builder) |
||
38 | { |
||
39 | $this->builder = $builder; |
||
40 | |||
41 | // "insert" line block: ++text++ -> <ins>text</ins> |
||
42 | $this->InlineTypes['+'][] = 'Insert'; |
||
43 | $this->inlineMarkerList = implode('', array_keys($this->InlineTypes)); |
||
44 | $this->specialCharacters[] = '+'; |
||
45 | |||
46 | // Media (image, audio or video) block |
||
47 | $this->BlockTypes['!'][] = 'Media'; |
||
48 | |||
49 | // "notes" block |
||
50 | if ($this->builder->getConfig()->get('body.notes.enabled')) { |
||
51 | $this->BlockTypes[':'][] = 'Note'; |
||
52 | } |
||
53 | |||
54 | // code highlight |
||
55 | $this->highlighter = new Highlighter(); |
||
56 | |||
57 | // Table of Content |
||
58 | parent::__construct(['selectors' => $this->builder->getConfig()->get('body.toc')]); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Insert inline. |
||
63 | * e.g.: ++text++ -> <ins>text</ins>. |
||
64 | */ |
||
65 | protected function inlineInsert($Excerpt) |
||
66 | { |
||
67 | if (!isset($Excerpt['text'][1])) { |
||
68 | return; |
||
69 | } |
||
70 | |||
71 | if ($Excerpt['text'][1] === '+' && preg_match('/^\+\+(?=\S)(.+?)(?<=\S)\+\+/', $Excerpt['text'], $matches)) { |
||
72 | return [ |
||
73 | 'extent' => strlen($matches[0]), |
||
74 | 'element' => [ |
||
75 | 'name' => 'ins', |
||
76 | 'text' => $matches[1], |
||
77 | 'handler' => 'line', |
||
78 | ], |
||
79 | ]; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | protected function inlineImage($Excerpt) |
||
87 | { |
||
88 | $image = parent::inlineImage($Excerpt); |
||
89 | if (!isset($image)) { |
||
90 | return null; |
||
91 | } |
||
92 | |||
93 | // clean source path / URL |
||
94 | $image['element']['attributes']['src'] = $this->cleanUrl($image['element']['attributes']['src']); |
||
95 | |||
96 | // should be lazy loaded? |
||
97 | if ($this->builder->getConfig()->get('body.images.lazy.enabled')) { |
||
98 | $image['element']['attributes']['loading'] = 'lazy'; |
||
99 | } |
||
100 | |||
101 | // disable remote image handling? |
||
102 | if (Util\Url::isUrl($image['element']['attributes']['src']) && !$this->builder->getConfig()->get('body.images.remote.enabled') ?? true) { |
||
103 | return $image; |
||
104 | } |
||
105 | |||
106 | // create asset |
||
107 | $asset = new Asset($this->builder, $image['element']['attributes']['src'], ['force_slash' => false]); |
||
108 | $image['element']['attributes']['src'] = $asset; |
||
109 | $width = $asset->getWidth(); |
||
110 | |||
111 | /** |
||
112 | * Should be resized? |
||
113 | */ |
||
114 | $assetResized = null; |
||
115 | if (isset($image['element']['attributes']['width']) |
||
116 | && (int) $image['element']['attributes']['width'] < $width |
||
117 | && $this->builder->getConfig()->get('body.images.resize.enabled') |
||
118 | ) { |
||
119 | $width = (int) $image['element']['attributes']['width']; |
||
120 | |||
121 | try { |
||
122 | $assetResized = $asset->resize($width); |
||
123 | } catch (\Exception $e) { |
||
124 | $this->builder->getLogger()->debug($e->getMessage()); |
||
125 | |||
126 | return $image; |
||
127 | } |
||
128 | $image['element']['attributes']['src'] = $assetResized; |
||
129 | } |
||
130 | |||
131 | // set width |
||
132 | if (!isset($image['element']['attributes']['width'])) { |
||
133 | $image['element']['attributes']['width'] = $width; |
||
134 | } |
||
135 | // set height |
||
136 | if (!isset($image['element']['attributes']['height'])) { |
||
137 | $image['element']['attributes']['height'] = ($assetResized ?? $asset)->getHeight(); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Should be responsive? |
||
142 | */ |
||
143 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
144 | if ($srcset = Image::buildSrcset( |
||
145 | $assetResized ?? $asset, |
||
146 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
147 | )) { |
||
148 | $image['element']['attributes']['srcset'] = $srcset; |
||
149 | $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('assets.images.responsive.sizes.default'); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | return $image; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Media block support: |
||
158 | * 1. <picture>/<source> for WebP images |
||
159 | * 2. <audio> and <video> elements |
||
160 | * 3. <figure>/<figcaption> for element with a title. |
||
161 | */ |
||
162 | protected function blockMedia($Excerpt) |
||
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 | } |
||
302 | |||
303 | protected function blockNoteContinue($line, $block) |
||
316 | } |
||
317 | |||
318 | protected function blockNoteComplete($block) |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Apply Highlight to code blocks. |
||
328 | */ |
||
329 | protected function blockFencedCodeComplete($block) |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * {@inheritdoc} |
||
355 | */ |
||
356 | protected function parseAttributeData($attributeString) |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Returns URL without query string. |
||
390 | */ |
||
391 | private function cleanUrl(string $path): string |
||
396 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.