Completed
Push — master ( d91fed...fd66aa )
by Josh
17:36
created

Parser::parse()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 7.6666
c 0
b 0
f 0
ccs 18
cts 18
cp 1
cc 10
nc 8
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2018 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Preg;
9
10
use s9e\TextFormatter\Plugins\ParserBase;
11
12
class Parser extends ParserBase
13
{
14
	/**
15
	* {@inheritdoc}
16
	*/
17 15
	public function parse($text, array $matches)
18
	{
19 15
		foreach ($this->config['generics'] as list($tagName, $regexp, $passthroughIdx, $map))
20
		{
21 15
			preg_match_all($regexp, $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
22
23 15
			foreach ($matches as $m)
24
			{
25 15
				$startTagPos = $m[0][1];
26 15
				$matchLen    = strlen($m[0][0]);
27
28 15
				if ($passthroughIdx && isset($m[$passthroughIdx]) && $m[$passthroughIdx][0] !== '')
29
				{
30
					// Compute the position and length of the start tag, end tag, and the content in
31
					// between. PREG_OFFSET_CAPTURE gives us the position of the content, and we
32
					// know its length. Everything before is considered part of the start tag, and
33
					// everything after is considered part of the end tag
34 9
					$contentPos  = $m[$passthroughIdx][1];
35 9
					$contentLen  = strlen($m[$passthroughIdx][0]);
36 9
					$startTagLen = $contentPos - $startTagPos;
37 9
					$endTagPos   = $contentPos + $contentLen;
38 9
					$endTagLen   = $matchLen - ($startTagLen + $contentLen);
39
40 9
					$tag = $this->parser->addTagPair($tagName, $startTagPos, $startTagLen, $endTagPos, $endTagLen, -100);
41
				}
42
				else
43
				{
44 6
					$tag = $this->parser->addSelfClosingTag($tagName, $startTagPos, $matchLen, -100);
45
				}
46
47 15
				foreach ($map as $i => $attrName)
48
				{
49 15
					if ($attrName && isset($m[$i]) && $m[$i][0] !== '')
50
					{
51 15
						$tag->setAttribute($attrName, $m[$i][0]);
52
					}
53
				}
54
			}
55
		}
56
	}
57
}