coduo /
php-matcher
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Coduo\PHPMatcher; |
||
| 4 | |||
| 5 | use Coduo\PHPMatcher\AST; |
||
| 6 | use Coduo\PHPMatcher\Exception\Exception; |
||
| 7 | use Coduo\PHPMatcher\Exception\PatternException; |
||
| 8 | use Coduo\PHPMatcher\Exception\UnknownExpanderException; |
||
| 9 | use Coduo\PHPMatcher\Matcher\Pattern; |
||
| 10 | use Coduo\PHPMatcher\Parser\ExpanderInitializer; |
||
| 11 | |||
| 12 | final class Parser |
||
| 13 | { |
||
| 14 | const NULL_VALUE = 'null'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var Lexer |
||
| 18 | */ |
||
| 19 | private $lexer; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var ExpanderInitializer |
||
| 23 | */ |
||
| 24 | private $expanderInitializer; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param Lexer $lexer |
||
| 28 | */ |
||
| 29 | public function __construct(Lexer $lexer, ExpanderInitializer $expanderInitializer) |
||
| 30 | { |
||
| 31 | $this->lexer = $lexer; |
||
| 32 | $this->expanderInitializer = $expanderInitializer; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param string $pattern |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | public function hasValidSyntax($pattern) |
||
| 40 | { |
||
| 41 | try { |
||
| 42 | $this->getAST($pattern); |
||
| 43 | return true; |
||
| 44 | } catch (Exception $e) { |
||
| 45 | return false; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $pattern |
||
| 51 | * @throws UnknownExpanderException |
||
| 52 | * @return Pattern\TypePattern |
||
| 53 | */ |
||
| 54 | public function parse($pattern) |
||
| 55 | { |
||
| 56 | $AST = $this->getAST($pattern); |
||
| 57 | $pattern = new Pattern\TypePattern((string) $AST->getType()); |
||
| 58 | foreach ($AST->getExpanders() as $expander) { |
||
| 59 | $pattern->addExpander($this->expanderInitializer->initialize($expander)); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $pattern; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param $pattern |
||
| 67 | * @return AST\Pattern |
||
| 68 | */ |
||
| 69 | public function getAST($pattern) |
||
| 70 | { |
||
| 71 | $this->lexer->setInput($pattern); |
||
| 72 | return $this->getPattern(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Create AST root |
||
| 77 | * |
||
| 78 | * @return AST\Pattern |
||
| 79 | */ |
||
| 80 | private function getPattern() |
||
| 81 | { |
||
| 82 | $this->lexer->moveNext(); |
||
| 83 | |||
| 84 | switch ($this->lexer->lookahead['type']) { |
||
| 85 | case Lexer::T_TYPE_PATTERN: |
||
| 86 | $pattern = new AST\Pattern(new AST\Type($this->lexer->lookahead['value'])); |
||
| 87 | break; |
||
| 88 | default: |
||
| 89 | $this->unexpectedSyntaxError($this->lexer->lookahead, "@type@ pattern"); |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->lexer->moveNext(); |
||
| 94 | |||
| 95 | if (!$this->endOfPattern()) { |
||
| 96 | $this->addExpanderNodes($pattern); |
||
|
0 ignored issues
–
show
|
|||
| 97 | } |
||
| 98 | |||
| 99 | return $pattern; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param AST\Pattern $pattern |
||
| 104 | */ |
||
| 105 | private function addExpanderNodes(AST\Pattern $pattern) |
||
| 106 | { |
||
| 107 | while(($expander = $this->getNextExpanderNode()) !== null) { |
||
| 108 | $pattern->addExpander($expander); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Try to get next expander, return null if there is no expander left |
||
| 114 | * @return AST\Expander|null |
||
| 115 | */ |
||
| 116 | private function getNextExpanderNode() |
||
| 117 | { |
||
| 118 | if ($this->endOfPattern()) { |
||
| 119 | return ; |
||
| 120 | } |
||
| 121 | |||
| 122 | $expander = new AST\Expander($this->getExpanderName()); |
||
| 123 | |||
| 124 | if ($this->endOfPattern()) { |
||
| 125 | $this->unexpectedEndOfString(")"); |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->addArgumentValues($expander); |
||
| 129 | |||
| 130 | if ($this->endOfPattern()) { |
||
| 131 | $this->unexpectedEndOfString(")"); |
||
| 132 | } |
||
| 133 | |||
| 134 | if (!$this->isNextCloseParenthesis()) { |
||
| 135 | $this->unexpectedSyntaxError($this->lexer->lookahead, ")"); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $expander; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return mixed |
||
| 143 | * @throws PatternException |
||
| 144 | */ |
||
| 145 | private function getExpanderName() |
||
| 146 | { |
||
| 147 | if ($this->lexer->lookahead['type'] !== Lexer::T_EXPANDER_NAME) { |
||
| 148 | $this->unexpectedSyntaxError($this->lexer->lookahead, ".expanderName(args) definition"); |
||
| 149 | } |
||
| 150 | $expander = $this->lexer->lookahead['value']; |
||
| 151 | $this->lexer->moveNext(); |
||
| 152 | |||
| 153 | return $expander; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add arguments to expander |
||
| 158 | * |
||
| 159 | * @param AST\Expander $expander |
||
| 160 | */ |
||
| 161 | private function addArgumentValues(AST\Expander $expander) |
||
| 162 | { |
||
| 163 | while(($argument = $this->getNextArgumentValue()) !== null) { |
||
| 164 | $argument = ($argument === self::NULL_VALUE) ? null : $argument; |
||
| 165 | $expander->addArgument($argument); |
||
| 166 | if (!$this->lexer->isNextToken(Lexer::T_COMMA)){ |
||
| 167 | break; |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->lexer->moveNext(); |
||
| 171 | |||
| 172 | if ($this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { |
||
| 173 | $this->unexpectedSyntaxError($this->lexer->lookahead, "string, number, boolean or null argument"); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Try to get next argument. Return false if there are no arguments left before ")" |
||
| 180 | * @return null|mixed |
||
| 181 | */ |
||
| 182 | private function getNextArgumentValue() |
||
| 183 | { |
||
| 184 | $validArgumentTypes = array( |
||
| 185 | Lexer::T_STRING, |
||
| 186 | Lexer::T_NUMBER, |
||
| 187 | Lexer::T_BOOLEAN, |
||
| 188 | Lexer::T_NULL |
||
| 189 | ); |
||
| 190 | |||
| 191 | if ($this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { |
||
| 192 | return ; |
||
| 193 | } |
||
| 194 | |||
| 195 | if ($this->lexer->isNextToken(Lexer::T_OPEN_CURLY_BRACE)) { |
||
| 196 | return $this->getArrayArgument(); |
||
| 197 | } |
||
| 198 | |||
| 199 | if ($this->lexer->isNextToken(Lexer::T_EXPANDER_NAME)) { |
||
| 200 | return $this->getNextExpanderNode(); |
||
| 201 | } |
||
| 202 | |||
| 203 | if (!$this->lexer->isNextTokenAny($validArgumentTypes)) { |
||
| 204 | $this->unexpectedSyntaxError($this->lexer->lookahead, "string, number, boolean or null argument"); |
||
| 205 | } |
||
| 206 | |||
| 207 | $tokenType = $this->lexer->lookahead['type']; |
||
| 208 | $argument = $this->lexer->lookahead['value']; |
||
| 209 | $this->lexer->moveNext(); |
||
| 210 | |||
| 211 | if ($tokenType === Lexer::T_NULL) { |
||
| 212 | $argument = self::NULL_VALUE; |
||
| 213 | } |
||
| 214 | |||
| 215 | return $argument; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * return array |
||
| 220 | */ |
||
| 221 | private function getArrayArgument() |
||
| 222 | { |
||
| 223 | $arrayArgument = array(); |
||
| 224 | $this->lexer->moveNext(); |
||
| 225 | |||
| 226 | while ($this->getNextArrayElement($arrayArgument) !== null) { |
||
| 227 | $this->lexer->moveNext(); |
||
| 228 | } |
||
| 229 | |||
| 230 | if (!$this->lexer->isNextToken(Lexer::T_CLOSE_CURLY_BRACE)) { |
||
| 231 | $this->unexpectedSyntaxError($this->lexer->lookahead, "}"); |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->lexer->moveNext(); |
||
| 235 | |||
| 236 | return $arrayArgument; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param array $array |
||
| 241 | * @return bool |
||
| 242 | * @throws PatternException |
||
| 243 | */ |
||
| 244 | private function getNextArrayElement(array &$array) |
||
| 245 | { |
||
| 246 | if ($this->lexer->isNextToken(Lexer::T_CLOSE_CURLY_BRACE)) { |
||
| 247 | return ; |
||
| 248 | } |
||
| 249 | |||
| 250 | $key = $this->getNextArgumentValue(); |
||
| 251 | if ($key === self::NULL_VALUE) { |
||
| 252 | $key = ""; |
||
| 253 | } |
||
| 254 | |||
| 255 | if (!$this->lexer->isNextToken(Lexer::T_COLON)) { |
||
| 256 | $this->unexpectedSyntaxError($this->lexer->lookahead, ":"); |
||
| 257 | } |
||
| 258 | |||
| 259 | $this->lexer->moveNext(); |
||
| 260 | |||
| 261 | $value = $this->getNextArgumentValue(); |
||
| 262 | if ($value === self::NULL_VALUE) { |
||
| 263 | $value = null; |
||
| 264 | } |
||
| 265 | |||
| 266 | $array[$key] = $value; |
||
| 267 | |||
| 268 | if (!$this->lexer->isNextToken(Lexer::T_COMMA)) { |
||
| 269 | return ; |
||
| 270 | } |
||
| 271 | |||
| 272 | return true; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | private function isNextCloseParenthesis() |
||
| 279 | { |
||
| 280 | $isCloseParenthesis = $this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS); |
||
| 281 | $this->lexer->moveNext(); |
||
| 282 | |||
| 283 | return $isCloseParenthesis; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param $unexpectedToken |
||
| 288 | * @param null $expected |
||
| 289 | * @throws PatternException |
||
| 290 | */ |
||
| 291 | private function unexpectedSyntaxError($unexpectedToken, $expected = null) |
||
| 292 | { |
||
| 293 | $tokenPos = (isset($unexpectedToken['position'])) ? $unexpectedToken['position'] : '-1'; |
||
| 294 | $message = sprintf("line 0, col %d: Error: ", $tokenPos); |
||
| 295 | $message .= (isset($expected)) ? sprintf("Expected \"%s\", got ", $expected) : "Unexpected"; |
||
| 296 | $message .= sprintf("\"%s\"", $unexpectedToken['value']); |
||
| 297 | |||
| 298 | throw PatternException::syntaxError($message); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param null $expected |
||
| 303 | * @throws PatternException |
||
| 304 | */ |
||
| 305 | private function unexpectedEndOfString($expected = null) |
||
| 306 | { |
||
| 307 | $tokenPos = (isset($this->lexer->token['position'])) ? $this->lexer->token['position'] + strlen($this->lexer->token['value']) : '-1'; |
||
| 308 | $message = sprintf("line 0, col %d: Error: ", $tokenPos); |
||
| 309 | $message .= (isset($expected)) ? sprintf("Expected \"%s\", got end of string.", $expected) : "Unexpected"; |
||
| 310 | $message .= "end of string"; |
||
| 311 | |||
| 312 | throw PatternException::syntaxError($message); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | private function endOfPattern() |
||
| 319 | { |
||
| 320 | return is_null($this->lexer->lookahead); |
||
| 321 | } |
||
| 322 | } |
||
| 323 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: