Passed
Branch 2.3/RemoveRedundantParentheses... (0c40fa)
by Josh
02:17
created

XPathHelper::removeRedundantParentheses()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 15
c 5
b 0
f 0
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 9.2222
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2020 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\Helpers;
9
10
use RuntimeException;
11
use s9e\TextFormatter\Configurator\RecursiveParser;
12
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\BooleanFunctions;
13
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\BooleanOperators;
14
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\Comparisons;
15
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\Core;
16
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\Math;
17
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\SingleByteStringFunctions;
18
use s9e\TextFormatter\Utils\XPath;
19
20
abstract class XPathHelper
21
{
22
	/**
23
	* Decode strings inside of an XPath expression
24
	*
25
	* @param  string $expr
26
	* @return string
27
	*/
28 21
	public static function decodeStrings($expr)
29
	{
30 21
		return preg_replace_callback(
31 21
			'(\'[^\']*+\'|"[^"]*+")',
32
			function ($m)
33
			{
34 2
				return $m[0][0] . hex2bin(substr($m[0], 1, -1)) . $m[0][0];
35 21
			},
36
			$expr
37
		);
38
	}
39
40
	/**
41
	* Encode strings inside of an XPath expression
42
	*
43
	* @param  string $expr
44
	* @return string
45
	*/
46 22
	public static function encodeStrings($expr)
47
	{
48 22
		return preg_replace_callback(
49 22
			'(\'[^\']*+\'|"[^"]*+")',
50
			function ($m)
51
			{
52 2
				return $m[0][0] . bin2hex(substr($m[0], 1, -1)) . $m[0][0];
53 22
			},
54
			$expr
55
		);
56
	}
57
58
	/**
59
	* Return the list of variables used in a given XPath expression
60
	*
61
	* @param  string $expr XPath expression
62
	* @return array        Alphabetically sorted list of unique variable names
63
	*/
64 4
	public static function getVariables($expr)
65
	{
66
		// First, remove strings' contents to prevent false-positives
67 4
		$expr = preg_replace('/(["\']).*?\\1/s', '$1$1', $expr);
68
69
		// Capture all the variable names
70 4
		preg_match_all('/\\$(\\w+)/', $expr, $matches);
71
72
		// Dedupe and sort names
73 4
		$varNames = array_unique($matches[1]);
74 4
		sort($varNames);
75
76 4
		return $varNames;
77
	}
78
79
	/**
80
	* Determine whether given XPath expression definitely evaluates to a number
81
	*
82
	* @param  string $expr XPath expression
83
	* @return bool         Whether given XPath expression definitely evaluates to a number
84
	*/
85 21
	public static function isExpressionNumeric($expr)
86
	{
87
		// Detect simple arithmetic operations
88 21
		if (preg_match('(^([$@][-\\w]++|-?[.\\d]++)(?: *(?:[-*+]|div) *(?1))+$)', $expr))
89
		{
90 12
			return true;
91
		}
92
93
		// Try parsing the expression as a math expression
94
		try
95
		{
96 9
			return (bool) self::getXPathParser()->parse($expr, 'Math');
97
		}
98 5
		catch (RuntimeException $e)
99
		{
100
			// Do nothing
101
		}
102
103 5
		return false;
104
	}
105
106
	/**
107
	* Remove extraneous space in a given XPath expression
108
	*
109
	* @param  string $expr Original XPath expression
110
	* @return string       Minified XPath expression
111
	*/
112 24
	public static function minify($expr)
113
	{
114 24
		$expr = trim($expr);
115
116
		// Test whether there's any characters that can be removed
117 24
		if (!preg_match('([\\s\\)])', $expr))
118
		{
119 2
			return $expr;
120
		}
121
122 22
		preg_match_all('("[^"]*+"|\'[^\']*+\'|[\'"](*:X))', $expr, $m);
123 22
		if (!empty($m['MARK']))
124
		{
125 1
			throw new RuntimeException("Cannot parse XPath expression '" . $expr . "'");
126
		}
127
128
		// Temporarily encode the content of literal strings
129 21
		$expr = self::encodeStrings(trim($expr));
130
131
		// Normalize whitespace to a single space
132 21
		$expr = preg_replace('/\\s+/', ' ', $expr);
133
134
		// Remove the space between a non-word character and a word character
135 21
		$expr = preg_replace('/[-a-z_0-9]\\K (?=[^-a-z_0-9])/i', '', $expr);
136 21
		$expr = preg_replace('/[^-a-z_0-9]\\K (?=[-a-z_0-9])/i', '', $expr);
137
138
		// Remove the space between two non-word characters as long as they're not two -
139 21
		$expr = preg_replace('/(?!- -)[^-a-z_0-9]\\K (?=[^-a-z_0-9])/i', '', $expr);
140
141
		// Remove the space between a - and a word character, as long as there's a space before -
142 21
		$expr = preg_replace('/ - ([a-z_0-9])/i', ' -$1', $expr);
143
144
		// Remove the spaces between a number and a div or "-" operator and the next token
145 21
		$expr = preg_replace('/(?:^|[ \\(])\\d+\\K (div|-) ?/', '$1', $expr);
146
147
		// Remove the space between the div operator the next token
148 21
		$expr = preg_replace('/([^-a-z_0-9]div) (?=[$0-9@])/', '$1', $expr);
149
150
		// Remove consecutive parentheses where redundant
151 21
		$expr = self::removeRedundantParentheses($expr);
152
153
		// Restore the literals
154 20
		$expr = self::decodeStrings($expr);
155
156 20
		return $expr;
157
	}
158
159
	/**
160
	* Remove consecutive parentheses where redundant
161
	*
162
	* @param  string $expr
163
	* @return string
164
	*/
165 21
	protected static function removeRedundantParentheses(string $expr): string
166
	{
167 21
		preg_match_all('([\\(\\)]|[^\\(\\)]++)', '(' . $expr . ')', $m);
168 21
		$tokens   = $m[0];
169 21
		$tokens[] = '';
170
171 21
		$depth = 0;
172 21
		$left  = [-1 => -1];
173 21
		foreach ($tokens as $k => $token)
174
		{
175 21
			if ($token === '(')
176
			{
177 21
				$left[$depth] = $k;
178 21
				++$depth;
179
			}
180 21
			elseif ($token === ')')
181
			{
182 21
				if (--$depth < 0)
183
				{
184 1
					throw new RuntimeException("Cannot parse XPath expression '" . $expr . "'");
185
				}
186 21
				if ([$tokens[$k + 1], $left[$depth - 1]] === [')', $left[$depth] - 1])
187
				{
188 5
					unset($tokens[$k], $tokens[$left[$depth]]);
189
				}
190
			}
191
		}
192
193 20
		return implode('', array_slice($tokens, 1, -2));
194
	}
195
196
	/**
197
	* Parse an XPath expression that is composed entirely of equality tests between a variable part
198
	* and a constant part
199
	*
200
	* @param  string      $expr
201
	* @return array|false
202
	*/
203 8
	public static function parseEqualityExpr($expr)
204
	{
205
		// Match an equality between a variable and a literal or the concatenation of strings
206
		$eq = '(?<equality>'
207
		    . '(?<key>@[-\\w]+|\\$\\w+|\\.)'
208
		    . '(?<operator>\\s*=\\s*)'
209
		    . '(?:'
210
		    . '(?<literal>(?<string>"[^"]*"|\'[^\']*\')|0|[1-9][0-9]*)'
211
		    . '|'
212
		    . '(?<concat>concat\\(\\s*(?&string)\\s*(?:,\\s*(?&string)\\s*)+\\))'
213
		    . ')'
214
		    . '|'
215
		    . '(?:(?<literal>(?&literal))|(?<concat>(?&concat)))(?&operator)(?<key>(?&key))'
216 8
		    . ')';
217
218
		// Match a string that is entirely composed of equality checks separated with "or"
219 8
		$regexp = '(^(?J)\\s*' . $eq . '\\s*(?:or\\s*(?&equality)\\s*)*$)';
220 8
		if (!preg_match($regexp, $expr))
221
		{
222 1
			return false;
223
		}
224
225 7
		preg_match_all("((?J)$eq)", $expr, $matches, PREG_SET_ORDER);
226
227 7
		$map = [];
228 7
		foreach ($matches as $m)
229
		{
230 7
			$key   = $m['key'];
231 7
			$value = (!empty($m['concat']))
232 1
			       ? self::evaluateConcat($m['concat'])
233 7
			       : self::evaluateLiteral($m['literal']);
234
235 7
			$map[$key][] = $value;
236
		}
237
238 7
		return $map;
239
	}
240
241
	/**
242
	* Evaluate a concat() expression where all arguments are string literals
243
	*
244
	* @param  string $expr concat() expression
245
	* @return string       Expression's value
246
	*/
247 1
	protected static function evaluateConcat($expr)
248
	{
249 1
		preg_match_all('(\'[^\']*\'|"[^"]*")', $expr, $strings);
250
251 1
		$value = '';
252 1
		foreach ($strings[0] as $string)
253
		{
254 1
			$value .= substr($string, 1, -1);
255
		}
256
257 1
		return $value;
258
	}
259
260
	/**
261
	* Evaluate an XPath literal
262
	*
263
	* @param  string $expr XPath literal
264
	* @return string       Literal's string value
265
	*/
266 6
	protected static function evaluateLiteral($expr)
267
	{
268 6
		if ($expr[0] === '"' || $expr[0] === "'")
269
		{
270 6
			$expr = substr($expr, 1, -1);
271
		}
272
273 6
		return $expr;
274
	}
275
276
	/**
277
	* Generate and return a cached XPath parser with a default set of matchers
278
	*
279
	* @return RecursiveParser
280
	*/
281 9
	protected static function getXPathParser()
282
	{
283 9
		static $parser;
284 9
		if (!isset($parser))
285
		{
286 1
			$parser     = new RecursiveParser;
287 1
			$matchers   = [];
288 1
			$matchers[] = new BooleanFunctions($parser);
289 1
			$matchers[] = new BooleanOperators($parser);
290 1
			$matchers[] = new Comparisons($parser);
291 1
			$matchers[] = new Core($parser);
292 1
			$matchers[] = new Math($parser);
293 1
			$matchers[] = new SingleByteStringFunctions($parser);
294
295 1
			$parser->setMatchers($matchers);
296
		}
297
298 9
		return $parser;
299
	}
300
}