Blocks   F
last analyzed

Complexity

Total Complexity 94

Size/Duplication

Total Lines 499
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 94
eloc 210
dl 0
loc 499
ccs 206
cts 206
cp 1
rs 2
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBlockMarks() 0 5 1
A closeList() 0 10 3
B matchSetextLines() 0 31 8
A getAtxHeaderEndTagLen() 0 6 1
A computeBlockIgnoreLen() 0 9 2
F parse() 0 382 79

How to fix   Complexity   

Complex Class

Complex classes like Blocks 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 Blocks, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Litedown\Parser\Passes;
9
10
use s9e\TextFormatter\Parser as Rules;
11
12
class Blocks extends AbstractPass
13
{
14
	/**
15
	* @var array
16
	*/
17
	protected $setextLines = [];
18
19
	/**
20
	* {@inheritdoc}
21
	*/
22 129
	public function parse()
23
	{
24 129
		$this->matchSetextLines();
25
26 129
		$blocks       = [];
27 129
		$blocksCnt    = 0;
28 129
		$codeFence    = null;
29 129
		$codeIndent   = 4;
30 129
		$codeTag      = null;
31 129
		$lineIsEmpty  = true;
32 129
		$lists        = [];
33 129
		$listsCnt     = 0;
34 129
		$newContext   = false;
35 129
		$textBoundary = 0;
36
37 129
		$regexp = '/^(?:(?=[-*+\\d \\t>`~#_])((?: {0,3}>(?:(?!!)|!(?![^\\n>]*?!<)) ?)+)?([ \\t]+)?(\\* *\\* *\\*[* ]*$|- *- *-[- ]*$|_ *_ *_[_ ]*$|=+$)?((?:[-*+]|\\d+\\.)[ \\t]+(?=\\S))?[ \\t]*(#{1,6}[ \\t]+|```+[^`\\n]*$|~~~+[^~\\n]*$)?)?/m';
38 129
		preg_match_all($regexp, $this->text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
39
40 129
		foreach ($matches as $m)
41
		{
42 129
			$blockDepth = 0;
43 129
			$blockMarks = [];
44 129
			$ignoreLen  = 0;
45 129
			$matchLen   = strlen($m[0][0]);
46 129
			$matchPos   = $m[0][1];
47
48
			// If the last line was empty then this is not a continuation, and vice-versa
49 129
			$continuation = !$lineIsEmpty;
50
51
			// Capture the position of the end of the line and determine whether the line is empty
52 129
			$lfPos       = $this->text->indexOf("\n", $matchPos);
53 129
			$lineIsEmpty = ($lfPos === $matchPos + $matchLen && empty($m[3][0]) && empty($m[4][0]) && empty($m[5][0]));
54
55
			// If the line is empty and it's the first empty line then we break current paragraph.
56 129
			$breakParagraph = ($lineIsEmpty && $continuation);
0 ignored issues
show
introduced by
The condition $continuation is always false.
Loading history...
57
58
			// Count block marks
59 129
			if (!empty($m[1][0]))
60
			{
61 37
				$blockMarks = $this->getBlockMarks($m[1][0]);
62 37
				$blockDepth = count($blockMarks);
63 37
				$ignoreLen  = strlen($m[1][0]);
64 37
				if (isset($codeTag) && $codeTag->hasAttribute('blockDepth'))
65
				{
66 5
					$blockDepth = min($blockDepth, $codeTag->getAttribute('blockDepth'));
67 5
					$ignoreLen  = $this->computeBlockIgnoreLen($m[1][0], $blockDepth);
68
				}
69
70
				// Overwrite block markup
71 37
				$this->text->overwrite($matchPos, $ignoreLen);
72
			}
73
74
			// Close supernumerary blocks
75 129
			if ($blockDepth < $blocksCnt && !$continuation)
76
			{
77 36
				$newContext = true;
78
				do
79
				{
80 36
					$startTag = array_pop($blocks);
81 36
					$this->parser->addEndTag($startTag->getName(), $textBoundary, 0)
82 36
					             ->pairWith($startTag);
83
				}
84 36
				while ($blockDepth < --$blocksCnt);
85
			}
86
87
			// Open new blocks
88 129
			if ($blockDepth > $blocksCnt && !$lineIsEmpty)
89
			{
90 36
				$newContext = true;
91
				do
92
				{
93 36
					$tagName  = ($blockMarks[$blocksCnt] === '>!') ? 'SPOILER' : 'QUOTE';
94 36
					$blocks[] = $this->parser->addStartTag($tagName, $matchPos, 0, -999);
95
				}
96 36
				while ($blockDepth > ++$blocksCnt);
97
			}
98
99
			// Compute the width of the indentation
100 129
			$indentWidth = 0;
101 129
			$indentPos   = 0;
102 129
			if (!empty($m[2][0]) && !$codeFence)
103
			{
104 37
				$indentStr = $m[2][0];
105 37
				$indentLen = strlen($indentStr);
106
				do
107
				{
108 37
					if ($indentStr[$indentPos] === ' ')
109
					{
110 35
						++$indentWidth;
111
					}
112
					else
113
					{
114 4
						$indentWidth = ($indentWidth + 4) & ~3;
115
					}
116
				}
117 37
				while (++$indentPos < $indentLen && $indentWidth < $codeIndent);
118
			}
119
120
			// Test whether we're out of a code block
121 129
			if (isset($codeTag) && !$codeFence && $indentWidth < $codeIndent && !$lineIsEmpty)
122
			{
123 16
				$newContext = true;
124
			}
125
126 129
			if ($newContext)
127
			{
128 49
				$newContext = false;
129
130
				// Close the code block if applicable
131 49
				if (isset($codeTag))
132
				{
133 16
					if ($textBoundary > $codeTag->getPos())
134
					{
135
						// Overwrite the whole block
136 14
						$this->text->overwrite($codeTag->getPos(), $textBoundary - $codeTag->getPos());
137 14
						$codeTag->pairWith($this->parser->addEndTag('CODE', $textBoundary, 0, -1));
138
					}
139
					else
140
					{
141
						// The code block is empty
142 2
						$codeTag->invalidate();
143
					}
144
145 16
					$codeTag = null;
146 16
					$codeFence = null;
147
				}
148
149
				// Close all the lists
150 49
				foreach ($lists as $list)
151
				{
152 2
					$this->closeList($list, $textBoundary);
153
				}
154 49
				$lists    = [];
155 49
				$listsCnt = 0;
156
157
				// Mark the block boundary
158 49
				if ($matchPos)
159
				{
160 49
					$this->text->markBoundary($matchPos - 1);
161
				}
162
			}
163
164 129
			if ($indentWidth >= $codeIndent)
165
			{
166 17
				if (isset($codeTag) || !$continuation)
167
				{
168
					// Adjust the amount of text being ignored
169 16
					$ignoreLen += $indentPos;
170
171 16
					if (!isset($codeTag))
172
					{
173
						// Create code block
174 16
						$codeTag = $this->parser->addStartTag('CODE', $matchPos + $ignoreLen, 0, -999);
175
					}
176
177
					// Clear the captures to prevent any further processing
178 17
					$m = [];
179
				}
180
			}
181 129
			elseif (!isset($codeTag))
182
			{
183 129
				$hasListItem = !empty($m[4][0]);
184
185 129
				if (!$indentWidth && !$continuation && !$hasListItem)
186
				{
187
					// Start of a new context
188 129
					$listIndex = -1;
189
				}
190 127
				elseif ($continuation && !$hasListItem)
191
				{
192
					// Continuation of current list item or paragraph
193 127
					$listIndex = $listsCnt - 1;
194
				}
195 32
				elseif (!$listsCnt)
196
				{
197
					// We're not inside of a list already, we can start one if there's a list item
198 32
					$listIndex = ($hasListItem) ? 0 : -1;
199
				}
200
				else
201
				{
202
					// We're inside of a list but we need to compute the depth
203 23
					$listIndex = 0;
204 23
					while ($listIndex < $listsCnt && $indentWidth > $lists[$listIndex]['maxIndent'])
205
					{
206 8
						++$listIndex;
207
					}
208
				}
209
210
				// Close deeper lists
211 129
				while ($listIndex < $listsCnt - 1)
212
				{
213 29
					$this->closeList(array_pop($lists), $textBoundary);
214 29
					--$listsCnt;
215
				}
216
217
				// If there's no list item at current index, we'll need to either create one or
218
				// drop down to previous index, in which case we have to adjust maxIndent
219 129
				if ($listIndex === $listsCnt && !$hasListItem)
220
				{
221 3
					--$listIndex;
222
				}
223
224 129
				if ($hasListItem && $listIndex >= 0)
225
				{
226 30
					$breakParagraph = true;
227
228
					// Compute the position and amount of text consumed by the item tag
229 30
					$tagPos = $matchPos + $ignoreLen + $indentPos;
230 30
					$tagLen = strlen($m[4][0]);
231
232
					// Create a LI tag that consumes its markup
233 30
					$itemTag = $this->parser->addStartTag('LI', $tagPos, $tagLen);
234
235
					// Overwrite the markup
236 30
					$this->text->overwrite($tagPos, $tagLen);
237
238
					// If the list index is within current lists count it means this is not a new
239
					// list and we have to close the last item. Otherwise, it's a new list that we
240
					// have to create
241 30
					if ($listIndex < $listsCnt)
242
					{
243 22
						$this->parser->addEndTag('LI', $textBoundary, 0)
244 22
						             ->pairWith($lists[$listIndex]['itemTag']);
245
246
						// Record the item in the list
247 22
						$lists[$listIndex]['itemTag']    = $itemTag;
248 22
						$lists[$listIndex]['itemTags'][] = $itemTag;
249
					}
250
					else
251
					{
252 30
						++$listsCnt;
253
254 30
						if ($listIndex)
255
						{
256 6
							$minIndent = $lists[$listIndex - 1]['maxIndent'] + 1;
257 6
							$maxIndent = max($minIndent, $listIndex * 4);
258
						}
259
						else
260
						{
261 30
							$minIndent = 0;
262 30
							$maxIndent = $indentWidth;
263
						}
264
265
						// Create a 0-width LIST tag right before the item tag LI
266 30
						$listTag = $this->parser->addStartTag('LIST', $tagPos, 0);
267
268
						// Test whether the list item ends with a dot, as in "1."
269 30
						if (strpos($m[4][0], '.') !== false)
270
						{
271 12
							$listTag->setAttribute('type', 'decimal');
272
273 12
							$start = (int) $m[4][0];
274 12
							if ($start !== 1)
275
							{
276 2
								$listTag->setAttribute('start', $start);
277
							}
278
						}
279
280
						// Record the new list depth
281 30
						$lists[] = [
282 30
							'listTag'   => $listTag,
283 30
							'itemTag'   => $itemTag,
284 30
							'itemTags'  => [$itemTag],
285 30
							'minIndent' => $minIndent,
286 30
							'maxIndent' => $maxIndent,
287
							'tight'     => true
288
						];
289
					}
290
				}
291
292
				// If we're in a list, on a non-empty line preceded with a blank line...
293 129
				if ($listsCnt && !$continuation && !$lineIsEmpty)
294
				{
295
					// ...and this is not the first item of the list...
296 25
					if (count($lists[0]['itemTags']) > 1 || !$hasListItem)
297
					{
298
						// ...every list that is currently open becomes loose
299 7
						foreach ($lists as &$list)
300
						{
301 7
							$list['tight'] = false;
302
						}
303 7
						unset($list);
304
					}
305
				}
306
307 129
				$codeIndent = ($listsCnt + 1) * 4;
308
			}
309
310 129
			if (isset($m[5]))
311
			{
312
				// Headers
313 42
				if ($m[5][0][0] === '#')
314
				{
315 20
					$startLen = strlen($m[5][0]);
316 20
					$startPos = $matchPos + $matchLen - $startLen;
317 20
					$endLen   = $this->getAtxHeaderEndTagLen($matchPos + $matchLen, $lfPos);
318 20
					$endPos   = $lfPos - $endLen;
319
320 20
					$this->parser->addTagPair('H' . strspn($m[5][0], '#', 0, 6), $startPos, $startLen, $endPos, $endLen);
321
322
					// Mark the start and the end of the header as boundaries
323 20
					$this->text->markBoundary($startPos);
324 20
					$this->text->markBoundary($lfPos);
325
326 20
					if ($continuation)
327
					{
328 20
						$breakParagraph = true;
329
					}
330
				}
331
				// Code fence
332 22
				elseif ($m[5][0][0] === '`' || $m[5][0][0] === '~')
333
				{
334 22
					$tagPos = $matchPos + $ignoreLen;
335 22
					$tagLen = $lfPos - $tagPos;
336
337 22
					if (isset($codeTag) && $m[5][0] === $codeFence)
338
					{
339 22
						$codeTag->pairWith($this->parser->addEndTag('CODE', $tagPos, $tagLen, -1));
340 22
						$this->parser->addIgnoreTag($textBoundary, $tagPos - $textBoundary);
341
342
						// Overwrite the whole block
343 22
						$this->text->overwrite($codeTag->getPos(), $tagPos + $tagLen - $codeTag->getPos());
344 22
						$codeTag = null;
345 22
						$codeFence = null;
346
					}
347 22
					elseif (!isset($codeTag))
348
					{
349
						// Create code block
350 22
						$codeTag   = $this->parser->addStartTag('CODE', $tagPos, $tagLen);
351 22
						$codeFence = substr($m[5][0], 0, strspn($m[5][0], '`~'));
352 22
						$codeTag->setAttribute('blockDepth', $blockDepth);
353
354
						// Ignore the next character, which should be a newline
355 22
						$this->parser->addIgnoreTag($tagPos + $tagLen, 1);
356
357
						// Add the language if present, e.g. ```php
358 22
						$lang = trim(trim($m[5][0], '`~'));
359 22
						if ($lang !== '')
360
						{
361 42
							$codeTag->setAttribute('lang', $lang);
362
						}
363
					}
364
				}
365
			}
366 129
			elseif (!empty($m[3][0]) && !$listsCnt && $this->text->charAt($matchPos + $matchLen) !== "\x17")
367
			{
368
				// Horizontal rule
369 9
				$this->parser->addSelfClosingTag('HR', $matchPos + $ignoreLen, $matchLen - $ignoreLen);
370 9
				$breakParagraph = true;
371
372
				// Mark the end of the line as a boundary
373 9
				$this->text->markBoundary($lfPos);
374
			}
375 129
			elseif (isset($this->setextLines[$lfPos]) && $this->setextLines[$lfPos]['blockDepth'] === $blockDepth && !$lineIsEmpty && !$listsCnt && !isset($codeTag))
376
			{
377
				// Setext-style header
378 12
				$this->parser->addTagPair(
379 12
					$this->setextLines[$lfPos]['tagName'],
380 12
					$matchPos + $ignoreLen,
381 12
					0,
382 12
					$this->setextLines[$lfPos]['endPos'],
383 12
					$this->setextLines[$lfPos]['endLen']
384
				);
385
386
				// Mark the end of the Setext line
387 12
				$this->text->markBoundary($this->setextLines[$lfPos]['endPos'] + $this->setextLines[$lfPos]['endLen']);
388
			}
389
390 129
			if ($breakParagraph)
391
			{
392 127
				$this->parser->addParagraphBreak($textBoundary);
393 127
				$this->text->markBoundary($textBoundary);
394
			}
395
396 129
			if (!$lineIsEmpty)
397
			{
398 129
				$textBoundary = $lfPos;
399
			}
400
401 129
			if ($ignoreLen)
402
			{
403 49
				$this->parser->addIgnoreTag($matchPos, $ignoreLen, 1000);
404
			}
405
		}
406
	}
407
408
	/**
409
	* Close a list at given offset
410
	*
411
	* @param  array   $list
412
	* @param  integer $textBoundary
413
	* @return void
414
	*/
415 30
	protected function closeList(array $list, $textBoundary)
416
	{
417 30
		$this->parser->addEndTag('LIST', $textBoundary, 0)->pairWith($list['listTag']);
418 30
		$this->parser->addEndTag('LI',   $textBoundary, 0)->pairWith($list['itemTag']);
419
420 30
		if ($list['tight'])
421
		{
422 26
			foreach ($list['itemTags'] as $itemTag)
423
			{
424 26
				$itemTag->removeFlags(Rules::RULE_CREATE_PARAGRAPHS);
425
			}
426
		}
427
	}
428
429
	/**
430
	* Compute the amount of text to ignore at the start of a block line
431
	*
432
	* @param  string  $str           Original block markup
433
	* @param  integer $maxBlockDepth Maximum block depth
434
	* @return integer                Number of characters to ignore
435
	*/
436 5
	protected function computeBlockIgnoreLen($str, $maxBlockDepth)
437
	{
438 5
		$remaining = $str;
439 5
		while (--$maxBlockDepth >= 0)
440
		{
441 4
			$remaining = preg_replace('/^ *>!? ?/', '', $remaining);
442
		}
443
444 5
		return strlen($str) - strlen($remaining);
445
	}
446
447
	/**
448
	* Return the length of the markup at the end of an ATX header
449
	*
450
	* @param  integer $startPos Start of the header's text
451
	* @param  integer $endPos   End of the header's text
452
	* @return integer
453
	*/
454 20
	protected function getAtxHeaderEndTagLen($startPos, $endPos)
455
	{
456 20
		$content = substr($this->text, $startPos, $endPos - $startPos);
457 20
		preg_match('/[ \\t]*#*[ \\t]*$/', $content, $m);
458
459 20
		return strlen($m[0]);
460
	}
461
462
	/**
463
	* Capture and return block marks from given string
464
	*
465
	* @param  string   $str Block markup, composed of ">", "!" and whitespace
466
	* @return string[]
467
	*/
468 37
	protected function getBlockMarks($str)
469
	{
470 37
		preg_match_all('(>!?)', $str, $m);
471
472 37
		return $m[0];
473
	}
474
475
	/**
476
	* Capture and store lines that contain a Setext-tyle header
477
	*
478
	* @return void
479
	*/
480 129
	protected function matchSetextLines()
481
	{
482 129
		if ($this->text->indexOf('-') === false && $this->text->indexOf('=') === false)
483
		{
484 95
			return;
485
		}
486
487
		// Capture the any series of - or = alone on a line, optionally preceded with the
488
		// angle brackets notation used in block markup
489 34
		$regexp = '/^(?=[-=>])(?:>!? ?)*(?=[-=])(?:-+|=+) *$/m';
490 34
		if (!preg_match_all($regexp, $this->text, $matches, PREG_OFFSET_CAPTURE))
491
		{
492 14
			return;
493
		}
494
495 20
		foreach ($matches[0] as list($match, $matchPos))
496
		{
497
			// Compute the position of the end tag. We start on the LF character before the
498
			// match and keep rewinding until we find a non-space character
499 20
			$endPos = $matchPos - 1;
500 20
			while ($endPos > 0 && $this->text->charAt($endPos - 1) === ' ')
501
			{
502 4
				--$endPos;
503
			}
504
505
			// Store at the offset of the LF character
506 20
			$this->setextLines[$matchPos - 1] = [
507 20
				'endLen'     => $matchPos + strlen($match) - $endPos,
508 20
				'endPos'     => $endPos,
509 20
				'blockDepth' => substr_count($match, '>'),
510 20
				'tagName'    => ($match[0] === '=') ? 'H1' : 'H2'
511
			];
512
		}
513
	}
514
}