Completed
Branch Scrutinizer (3da711)
by Josh
03:32
created

AbstractInlineMarkup::parseInlineMarkup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.9332
c 1
b 0
f 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 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
abstract class AbstractInlineMarkup extends AbstractPass
11
{
12
	/**
13
	* Parse given inline markup in text
14
	*
15
	* The markup must start and end with exactly 2 characters
16
	*
17
	* @param  string $str     First markup string
18
	* @param  string $regexp  Regexp used to match the markup's span
19
	* @param  string $tagName Name of the tag produced by this markup
20
	* @return void
21
	*/
22 16
	protected function parseInlineMarkup(string $str, string $regexp, string $tagName): void
23
	{
24 16
		$pos = $this->text->indexOf($str);
25 16
		if ($pos === false)
26
		{
27 16
			return;
28
		}
29
30 14
		preg_match_all($regexp, $this->text, $matches, PREG_OFFSET_CAPTURE, $pos);
31 14
		foreach ($matches[0] as [$match, $matchPos])
32
		{
33 11
			$matchLen = strlen($match);
34 11
			$endPos   = $matchPos + $matchLen - 2;
35
36 11
			$this->parser->addTagPair($tagName, $matchPos, 2, $endPos, 2);
37 11
			$this->text->overwrite($matchPos, 2);
38 11
			$this->text->overwrite($endPos, 2);
39
		}
40
	}
41
}