Completed
Branch 2.3/RemoveRedundantParentheses... (567a0c)
by Josh
03:38
created

XPathHelper::removeRedundantParentheses()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.0099

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 16
c 4
b 0
f 0
dl 0
loc 30
ccs 16
cts 17
cp 0.9412
rs 8.8333
cc 7
nc 6
nop 1
crap 7.0099
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 21
	public static function encodeStrings($expr)
47
	{
48 21
		return preg_replace_callback(
49 21
			'(\'[^\']*+\'|"[^"]*+")',
50
			function ($m)
51
			{
52 2
				return $m[0][0] . bin2hex(substr($m[0], 1, -1)) . $m[0][0];
53 21
			},
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 21
	public static function minify($expr)
113
	{
114 21
		preg_match_all('("[^"]*+"|\'[^\']*+\'|[\'"](*:X))', $expr, $m);
115 21
		if (!empty($m['MARK']))
116
		{
117 1
			throw new RuntimeException("Cannot parse XPath expression '" . $expr . "'");
118
		}
119
120
		// Trim the surrounding whitespace then temporarily remove literal strings
121 20
		$expr = self::encodeStrings(trim($expr));
122
123
		// Normalize whitespace to a single space
124 20
		$expr = preg_replace('/\\s+/', ' ', $expr);
125
126
		// Remove the space between a non-word character and a word character
127 20
		$expr = preg_replace('/[-a-z_0-9]\\K (?=[^-a-z_0-9])/i', '', $expr);
128 20
		$expr = preg_replace('/[^-a-z_0-9]\\K (?=[-a-z_0-9])/i', '', $expr);
129
130
		// Remove the space between two non-word characters as long as they're not two -
131 20
		$expr = preg_replace('/(?!- -)[^-a-z_0-9]\\K (?=[^-a-z_0-9])/i', '', $expr);
132
133
		// Remove the space between a - and a word character, as long as there's a space before -
134 20
		$expr = preg_replace('/ - ([a-z_0-9])/i', ' -$1', $expr);
135
136
		// Remove the spaces between a number and a div or "-" operator and the next token
137 20
		$expr = preg_replace('/(?:^|[ \\(])\\d+\\K (div|-) ?/', '$1', $expr);
138
139
		// Remove the space between the div operator the next token
140 20
		$expr = preg_replace('/([^-a-z_0-9]div) (?=[$0-9@])/', '$1', $expr);
141
142
		// Remove consecutive parentheses where redundant
143 20
		$expr = self::removeRedundantParentheses($expr);
144
145
		// Restore the literals
146 20
		$expr = self::decodeStrings($expr);
147
148 20
		return $expr;
149
	}
150
151
	/**
152
	* Remove consecutive parentheses where redundant
153
	*
154
	* @param  string $expr
155
	* @return string
156
	*/
157 20
	protected static function removeRedundantParentheses(string $expr): string
158
	{
159 20
		preg_match_all('([\\(\\)]|[^\\(\\)]++)', $expr, $m);
160 20
		$tokens   = $m[0];
161 20
		$tokens[] = '';
162
163 20
		$depth = 0;
164 20
		$left  = [-1 => -1];
165 20
		foreach ($tokens as $k => $token)
166
		{
167 20
			if ($token === '(')
168
			{
169 9
				$left[$depth] = $k;
170 9
				++$depth;
171
			}
172 20
			elseif ($token === ')')
173
			{
174 9
				if (--$depth < 0)
175
				{
176
					throw new RuntimeException("Cannot parse XPath expression '" . $expr . "'");
177
				}
178 9
				if ($tokens[$k + 1] === ')' && $left[$depth - 1] === $left[$depth] - 1)
179
				{
180 3
					unset($tokens[$k]);
181 3
					unset($tokens[$left[$depth]]);
182
				}
183
			}
184
		}
185
186 20
		return implode('', $tokens);
187
	}
188
189
	/**
190
	* Parse an XPath expression that is composed entirely of equality tests between a variable part
191
	* and a constant part
192
	*
193
	* @param  string      $expr
194
	* @return array|false
195
	*/
196 8
	public static function parseEqualityExpr($expr)
197
	{
198
		// Match an equality between a variable and a literal or the concatenation of strings
199
		$eq = '(?<equality>'
200
		    . '(?<key>@[-\\w]+|\\$\\w+|\\.)'
201
		    . '(?<operator>\\s*=\\s*)'
202
		    . '(?:'
203
		    . '(?<literal>(?<string>"[^"]*"|\'[^\']*\')|0|[1-9][0-9]*)'
204
		    . '|'
205
		    . '(?<concat>concat\\(\\s*(?&string)\\s*(?:,\\s*(?&string)\\s*)+\\))'
206
		    . ')'
207
		    . '|'
208
		    . '(?:(?<literal>(?&literal))|(?<concat>(?&concat)))(?&operator)(?<key>(?&key))'
209 8
		    . ')';
210
211
		// Match a string that is entirely composed of equality checks separated with "or"
212 8
		$regexp = '(^(?J)\\s*' . $eq . '\\s*(?:or\\s*(?&equality)\\s*)*$)';
213 8
		if (!preg_match($regexp, $expr))
214
		{
215 1
			return false;
216
		}
217
218 7
		preg_match_all("((?J)$eq)", $expr, $matches, PREG_SET_ORDER);
219
220 7
		$map = [];
221 7
		foreach ($matches as $m)
222
		{
223 7
			$key   = $m['key'];
224 7
			$value = (!empty($m['concat']))
225 1
			       ? self::evaluateConcat($m['concat'])
226 7
			       : self::evaluateLiteral($m['literal']);
227
228 7
			$map[$key][] = $value;
229
		}
230
231 7
		return $map;
232
	}
233
234
	/**
235
	* Evaluate a concat() expression where all arguments are string literals
236
	*
237
	* @param  string $expr concat() expression
238
	* @return string       Expression's value
239
	*/
240 1
	protected static function evaluateConcat($expr)
241
	{
242 1
		preg_match_all('(\'[^\']*\'|"[^"]*")', $expr, $strings);
243
244 1
		$value = '';
245 1
		foreach ($strings[0] as $string)
246
		{
247 1
			$value .= substr($string, 1, -1);
248
		}
249
250 1
		return $value;
251
	}
252
253
	/**
254
	* Evaluate an XPath literal
255
	*
256
	* @param  string $expr XPath literal
257
	* @return string       Literal's string value
258
	*/
259 6
	protected static function evaluateLiteral($expr)
260
	{
261 6
		if ($expr[0] === '"' || $expr[0] === "'")
262
		{
263 6
			$expr = substr($expr, 1, -1);
264
		}
265
266 6
		return $expr;
267
	}
268
269
	/**
270
	* Generate and return a cached XPath parser with a default set of matchers
271
	*
272
	* @return RecursiveParser
273
	*/
274 9
	protected static function getXPathParser()
275
	{
276 9
		static $parser;
277 9
		if (!isset($parser))
278
		{
279 1
			$parser     = new RecursiveParser;
280 1
			$matchers   = [];
281 1
			$matchers[] = new BooleanFunctions($parser);
282 1
			$matchers[] = new BooleanOperators($parser);
283 1
			$matchers[] = new Comparisons($parser);
284 1
			$matchers[] = new Core($parser);
285 1
			$matchers[] = new Math($parser);
286 1
			$matchers[] = new SingleByteStringFunctions($parser);
287
288 1
			$parser->setMatchers($matchers);
289
		}
290
291 9
		return $parser;
292
	}
293
}