| Conditions | 10 |
| Paths | 11 |
| Total Lines | 67 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 110 | public static function allowedXPathFilter( |
||
| 111 | string $value, |
||
| 112 | array $allowed_axes = C::DEFAULT_ALLOWED_AXES, |
||
| 113 | array $allowed_functions = C::DEFAULT_ALLOWED_FUNCTIONS, |
||
| 114 | string $message = '', |
||
| 115 | ): void { |
||
| 116 | BaseAssert::allString($allowed_axes); |
||
| 117 | BaseAssert::allString($allowed_functions); |
||
| 118 | BaseAssert::maxLength( |
||
| 119 | $value, |
||
| 120 | C::XPATH_FILTER_MAX_LENGTH, |
||
| 121 | sprintf('XPath Filter exceeds the limit of 100 characters.'), |
||
| 122 | ); |
||
| 123 | |||
| 124 | $strippedValue = preg_replace( |
||
| 125 | self::$regex_xpfilter_remove_strings, |
||
| 126 | // Replace the content with two of the quotes that were matched |
||
| 127 | "\\1\\1", |
||
| 128 | $value, |
||
| 129 | ); |
||
| 130 | |||
| 131 | if ($strippedValue === null) { |
||
| 132 | throw new Exception("Error in preg_replace."); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Check if the $xpath_expression uses an XPath function that is not in the list of allowed functions |
||
| 137 | * |
||
| 138 | * Look for the function specifier '(' and look for a function name before it. |
||
| 139 | * Ignoring whitespace before the '(' and the function name. |
||
| 140 | * All functions must match a string on a list of allowed function names |
||
| 141 | */ |
||
| 142 | $matches = []; |
||
| 143 | $res = preg_match_all(self::$regex_xpfilter_functions, $strippedValue, $matches); |
||
| 144 | if ($res === false) { |
||
| 145 | throw new Exception("Error in preg_match_all."); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Check that all the function names we found are in the list of allowed function names |
||
| 149 | foreach ($matches[1] as $match) { |
||
| 150 | if (!in_array($match, $allowed_functions)) { |
||
| 151 | throw new AssertionFailedException(sprintf( |
||
| 152 | $message ?: '\'%s\' is not an allowed XPath function.', |
||
| 153 | $match, |
||
| 154 | )); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Check if the $xpath_expression uses an XPath axis that is not in the list of allowed axes |
||
| 160 | * |
||
| 161 | * Look for the axis specifier '::' and look for a function name before it. |
||
| 162 | * Ignoring whitespace before the '::' and the axis name. |
||
| 163 | * All axes must match a string on a list of allowed axis names |
||
| 164 | */ |
||
| 165 | $matches = []; |
||
| 166 | $res = preg_match_all(self::$regex_xpfilter_axes, $strippedValue, $matches); |
||
| 167 | if ($res === false) { |
||
| 168 | throw new Exception("Error in preg_match_all."); |
||
| 169 | } |
||
| 170 | |||
| 171 | // Check that all the axes names we found are in the list of allowed axes names |
||
| 172 | foreach ($matches[1] as $match) { |
||
| 173 | if (!in_array($match, $allowed_axes)) { |
||
| 174 | throw new AssertionFailedException(sprintf( |
||
| 175 | $message ?: '\'%s\' is not an allowed XPath axis.', |
||
| 176 | $match, |
||
| 177 | )); |
||
| 182 |