Completed
Branch TemplateNormalizations (ae42e4)
by Josh
33:16
created

ConvertCurlyExpressionsInText::normalize()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 29
cts 29
cp 1
rs 8.7592
c 0
b 0
f 0
cc 6
eloc 29
nc 8
nop 1
crap 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ConvertCurlyExpressionsInText::insertTextBefore() 0 4 1
B ConvertCurlyExpressionsInText::normalizeNode() 0 37 3

How to fix   Long Method   

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-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
9
10
use DOMNode;
11
12
/**
13
* Convert simple expressions in curly brackets in text into xsl:value-of elements
14
*
15
* Will replace
16
*     <span>{$FOO}{@bar}</span>
17
* with
18
*     <span><xsl:value-of value="$FOO"/><xsl:value-of value="@bar"/></span>
19
*/
20
class ConvertCurlyExpressionsInText extends AbstractNormalization
21
{
22
	/**
23
	* {@inheritdoc}
24
	*/
25
	protected $queries = ['//*[namespace-uri() != $XSL]/text()[contains(., "{@") or contains(., "{$")]'];
26
27
	/**
28
	* Insert a text node before given node
29
	*
30
	* @param  string  $text
31
	* @param  DOMNode $node
32
	* @return void
33
	*/
34
	protected function insertTextBefore($text, $node)
35
	{
36
		$node->parentNode->insertBefore($this->createTextNode($text), $node);
37
	}
38
39
	/**
40
	* {@inheritdoc}
41
	*/
42
	protected function normalizeNode(DOMNode $node)
43
	{
44
		$parentNode = $node->parentNode;
45
46
		preg_match_all(
47
			'#\\{([$@][-\\w]+)\\}#',
48
			$node->textContent,
49
			$matches,
50
			PREG_SET_ORDER | PREG_OFFSET_CAPTURE
51
		);
52
53
		$lastPos = 0;
54
		foreach ($matches as $m)
55
		{
56
			$pos = $m[0][1];
57
58
			// Catch up to current position
59
			if ($pos > $lastPos)
60
			{
61
				$text = substr($node->textContent, $lastPos, $pos - $lastPos);
62
				$this->insertTextBefore($text, $node);
63
			}
64
			$lastPos = $pos + strlen($m[0][0]);
65
66
			// Add the xsl:value-of element
67
			$parentNode
68
				->insertBefore($this->createElement('xsl:value-of'), $node)
69
				->setAttribute('select', $m[1][0]);
70
		}
71
72
		// Append the rest of the text
73
		$text = substr($node->textContent, $lastPos);
74
		$this->insertTextBefore($text, $node);
75
76
		// Now remove the old text node
77
		$parentNode->removeChild($node);
78
	}
79
}