Completed
Push — master ( aec2d9...ec72f4 )
by Josh
15:30
created

XPathHelper::getVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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