Completed
Push — master ( 7cb65e...f3ce80 )
by Josh
25:47
created

FoldArithmeticConstants::getOptimizationPasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2016 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 DOMDocument;
11
use DOMXPath;
12
13
class FoldArithmeticConstants extends AbstractConstantFolding
14
{
15
	/**
16
	* @var DOMXPath
17
	*/
18
	protected $xpath;
19
20
	/**
21
	* Constructor
22
	*/
23
	public function __construct()
24
	{
25
		$this->xpath = new DOMXPath(new DOMDocument);
26
	}
27
28
	/**
29
	* {@inheritdoc}
30
	*/
31
	protected function getOptimizationPasses()
32
	{
33
		return [
34
			'(^[-+0-9\\s]+$)'                        => 'foldOperation',
35
			'( \\+ 0(?! [^+\\)])|(?<![-\\w])0 \\+ )' => 'foldAdditiveIdentity',
36
			'(^((?>\\d+ [-+] )*)(\\d+) div (\\d+))'  => 'foldDivision',
37
			'(^((?>\\d+ [-+] )*)(\\d+) \\* (\\d+))'  => 'foldMultiplication',
38
			'(\\( \\d+ (?>(?>[-+*]|div) \\d+ )+\\))' => 'foldSubExpression',
39
			'((?<=[-+*\\(]|\\bdiv|^) \\( ([@$][-\\w]+|\\d+(?>\\.\\d+)?) \\) (?=[-+*\\)]|div|$))' => 'removeParentheses'
40
		];
41
	}
42
43
	/**
44
	* {@inheritdoc}
45
	*/
46
	protected function evaluateExpression($expr)
47
	{
48
		$expr = preg_replace_callback(
49
			'(([\'"])(.*?)\\1)s',
50
			function ($m)
51
			{
52
				return $m[1] . bin2hex($m[2]) . $m[1];
53
			},
54
			$expr
55
		);
56
		$expr = parent::evaluateExpression($expr);
57
		$expr = preg_replace_callback(
58
			'(([\'"])(.*?)\\1)s',
59
			function ($m)
60
			{
61
				return $m[1] . hex2bin($m[2]) . $m[1];
62
			},
63
			$expr
64
		);
65
66
		return $expr;
67
	}
68
69
	/**
70
	* Remove "+ 0" additions
71
	*
72
	* @param  array  $m
73
	* @return string
74
	*/
75
	protected function foldAdditiveIdentity(array $m)
76
	{
77
		return '';
78
	}
79
80
	/**
81
	* Evaluate and return the result of a division
82
	*
83
	* @param  array  $m
84
	* @return string
85
	*/
86
	protected function foldDivision(array $m)
87
	{
88
		return $m[1] . ($m[2] / $m[3]);
89
	}
90
91
	/**
92
	* Evaluate and return the result of a multiplication
93
	*
94
	* @param  array  $m
95
	* @return string
96
	*/
97
	protected function foldMultiplication(array $m)
98
	{
99
		return $m[1] . ($m[2] * $m[3]);
100
	}
101
102
	/**
103
	* Evaluate and replace a constant operation
104
	*
105
	* @param  array  $m
106
	* @return string
107
	*/
108
	protected function foldOperation(array $m)
109
	{
110
		return (string) $this->xpath->evaluate($m[0]);
111
	}
112
113
	/**
114
	* Evaluate and return the result of a simple subexpression
115
	*
116
	* @param  array  $m
117
	* @return string
118
	*/
119
	protected function foldSubExpression(array $m)
120
	{
121
		return '(' . $this->evaluateExpression(trim(substr($m[0], 1, -1))) . ')';
122
	}
123
124
	/**
125
	* Remove the parentheses around an integer
126
	*
127
	* @param  array  $m
128
	* @return string
129
	*/
130
	protected function removeParentheses(array $m)
131
	{
132
		return ' ' . $m[1] . ' ';
133
	}
134
}