Completed
Push — master ( 56471b...43e0f8 )
by Josh
14:46
created

InlineCode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 87
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addInlineCodeTags() 0 9 1
B parse() 0 27 6
B getInlineCodeMarkers() 0 31 3
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 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
class InlineCode extends AbstractPass
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 26
	public function parse()
16
	{
17 26
		$markers = $this->getInlineCodeMarkers();
18 26
		$i       = -1;
19 26
		$cnt     = count($markers);
20 26
		while (++$i < ($cnt - 1))
21
		{
22 25
			$pos = $markers[$i]['next'];
23 25
			$j   = $i;
24 25
			if ($this->text->charAt($markers[$i]['pos']) !== '`')
25
			{
26
				// Adjust the left marker if its first backtick was escaped
27 1
				++$markers[$i]['pos'];
28 1
				--$markers[$i]['len'];
29
			}
30 25
			while (++$j < $cnt && $markers[$j]['pos'] === $pos)
31
			{
32 24
				if ($markers[$j]['len'] === $markers[$i]['len'])
33
				{
34 22
					$this->addInlineCodeTags($markers[$i], $markers[$j]);
35 22
					$i = $j;
36 22
					break;
37
				}
38 10
				$pos = $markers[$j]['next'];
39
			}
40
		}
41 26
	}
42
43
	/**
44
	* Add the tag pair for an inline code span
45
	*
46
	* @param  array $left  Left marker
47
	* @param  array $right Right marker
48
	* @return void
49
	*/
50 22
	protected function addInlineCodeTags($left, $right)
51
	{
52 22
		$startPos = $left['pos'];
53 22
		$startLen = $left['len'] + $left['trimAfter'];
54 22
		$endPos   = $right['pos'] - $right['trimBefore'];
55 22
		$endLen   = $right['len'] + $right['trimBefore'];
56 22
		$this->parser->addTagPair('C', $startPos, $startLen, $endPos, $endLen);
57 22
		$this->text->overwrite($startPos, $endPos + $endLen - $startPos);
58 22
	}
59
60
	/**
61
	* Capture and return inline code markers
62
	*
63
	* @return array
64
	*/
65 26
	protected function getInlineCodeMarkers()
66
	{
67 26
		$pos = $this->text->indexOf('`');
68 26
		if ($pos === false)
69
		{
70 1
			return [];
71
		}
72
73 25
		preg_match_all(
74 25
			'/(`+)(\\s*)[^\\x17`]*/',
75 25
			str_replace("\x1BB", '\\`', $this->text),
76 25
			$matches,
77 25
			PREG_OFFSET_CAPTURE | PREG_SET_ORDER,
78 25
			$pos
79
		);
80 25
		$trimNext = 0;
81 25
		$markers  = [];
82 25
		foreach ($matches as $m)
83
		{
84 25
			$markers[] = [
85 25
				'pos'        => $m[0][1],
86 25
				'len'        => strlen($m[1][0]),
87 25
				'trimBefore' => $trimNext,
88 25
				'trimAfter'  => strlen($m[2][0]),
89 25
				'next'       => $m[0][1] + strlen($m[0][0])
90
			];
91 25
			$trimNext = strlen($m[0][0]) - strlen(rtrim($m[0][0]));
92
		}
93
94 25
		return $markers;
95
	}
96
}