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

AbstractInlineMarkup   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 11
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A parseInlineMarkup() 0 17 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
}