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 Weew\ConsoleArguments; |
||
| 4 | |||
| 5 | use Weew\ConsoleArguments\Exceptions\AmbiguousCommandException; |
||
| 6 | use Weew\ConsoleArguments\Exceptions\CommandNotFoundException; |
||
| 7 | use Weew\ConsoleArguments\Exceptions\InvalidOptionValueException; |
||
| 8 | use Weew\ConsoleArguments\Exceptions\MissingArgumentValueException; |
||
| 9 | use Weew\ConsoleArguments\Exceptions\MissingCommandNameException; |
||
| 10 | use Weew\ConsoleArguments\Exceptions\MissingOptionValueException; |
||
| 11 | use Weew\ConsoleArguments\Exceptions\TooManyArgumentValuesException; |
||
| 12 | use Weew\ConsoleArguments\Exceptions\UnknownOptionException; |
||
| 13 | |||
| 14 | class ArgumentsMatcher implements IArgumentsMatcher { |
||
| 15 | /** |
||
| 16 | * @var IArgumentsParser |
||
| 17 | */ |
||
| 18 | protected $argumentsParser; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * ArgumentsMatcher constructor. |
||
| 22 | * |
||
| 23 | * @param IArgumentsParser $argumentsParser |
||
| 24 | */ |
||
| 25 | public function __construct(IArgumentsParser $argumentsParser = null) { |
||
| 26 | if ( ! $argumentsParser instanceof IArgumentsParser) { |
||
| 27 | $argumentsParser = $this->createArgumentsParser(); |
||
| 28 | } |
||
| 29 | |||
| 30 | $this->argumentsParser = $argumentsParser; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $args |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | * @throws MissingCommandNameException |
||
| 38 | */ |
||
| 39 | public function matchCommandName(array $args) { |
||
| 40 | $commandName = null; |
||
| 41 | |||
| 42 | if (array_has($args, 'arguments')) { |
||
| 43 | $commandName = (string) array_shift($args['arguments']); |
||
| 44 | } |
||
| 45 | |||
| 46 | if ( ! $commandName || str_starts_with($commandName, '-')) { |
||
|
0 ignored issues
–
show
|
|||
| 47 | throw new MissingCommandNameException( |
||
| 48 | 'You must provide a valid command name.' |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | return [$commandName, $args]; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param array $commands |
||
| 57 | * @param array $groupedArgs |
||
| 58 | * @param bool $strict |
||
| 59 | * |
||
| 60 | * @return array[ICommand, array] |
||
|
0 ignored issues
–
show
The doc-type
array[ICommand, could not be parsed: Expected "]" at position 2, but found "ICommand". (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 61 | * @throws AmbiguousCommandException |
||
| 62 | * @throws CommandNotFoundException |
||
| 63 | * @throws MissingCommandNameException |
||
| 64 | * @throws TooManyArgumentValuesException |
||
| 65 | * @throws UnknownOptionException |
||
| 66 | */ |
||
| 67 | public function matchCommands(array $commands, array $groupedArgs, $strict = true) { |
||
| 68 | list($commandName, $groupedArgs) = $this->matchCommandName($groupedArgs); |
||
| 69 | $command = $this->findCommand($commands, $commandName); |
||
| 70 | |||
| 71 | $groupedArgs = $this->matchCommand($command, $groupedArgs, $strict); |
||
| 72 | |||
| 73 | return [$command, $groupedArgs]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param ICommand $command |
||
| 78 | * @param array $groupedArgs |
||
| 79 | * @param bool $strict |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | * @throws MissingArgumentValueException |
||
| 83 | * @throws TooManyArgumentValuesException |
||
| 84 | * @throws UnknownOptionException |
||
| 85 | */ |
||
| 86 | public function matchCommand(ICommand $command, array $groupedArgs, $strict = true) { |
||
| 87 | foreach ($command->getArguments() as $argument) { |
||
| 88 | $groupedArgs = $this->matchArgument($argument, $groupedArgs, $strict); |
||
| 89 | } |
||
| 90 | |||
| 91 | $leftoverArgs = array_get($groupedArgs, 'arguments', []); |
||
| 92 | |||
| 93 | if (is_array($leftoverArgs) && count($leftoverArgs) > 0 && $strict) { |
||
| 94 | throw new TooManyArgumentValuesException(s( |
||
| 95 | 'Too many arguments, remove: %s.', |
||
| 96 | implode(', ', $leftoverArgs) |
||
| 97 | )); |
||
| 98 | } |
||
| 99 | |||
| 100 | $foundOptions = ['arguments', 'options']; |
||
| 101 | |||
| 102 | foreach ($command->getOptions() as $option) { |
||
| 103 | $groupedArgs = $this->matchOption($option, $groupedArgs, $strict); |
||
| 104 | $foundOptions[] = $option->getNameOrAlias(); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($strict) { |
||
| 108 | foreach ($groupedArgs as $key => $arg) { |
||
| 109 | if ( ! array_contains($foundOptions, $key)) { |
||
| 110 | throw new UnknownOptionException(s( |
||
| 111 | 'Invalid option "%s".', |
||
| 112 | $key |
||
| 113 | )); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return $groupedArgs; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param ICommand[] $commands |
||
| 123 | * @param $commandName |
||
| 124 | * |
||
| 125 | * @return ICommand |
||
| 126 | * @throws AmbiguousCommandException |
||
| 127 | * @throws CommandNotFoundException |
||
| 128 | * @throws MissingCommandNameException |
||
| 129 | */ |
||
| 130 | public function findCommand(array $commands, $commandName) { |
||
| 131 | $matches = []; |
||
| 132 | |||
| 133 | foreach ($commands as $command) { |
||
| 134 | if ($this->compareCommandName($command->getName(), $commandName)) { |
||
| 135 | $matches[] = $command; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | if (count($matches) === 1) { |
||
| 140 | return $matches[0]; |
||
| 141 | } else if (count($matches) === 0) { |
||
| 142 | throw new CommandNotFoundException(s( |
||
| 143 | 'Could not find a command that matches "%s".', |
||
| 144 | $commandName |
||
| 145 | )); |
||
| 146 | } else { |
||
| 147 | $qualifiedCommandNames = ''; |
||
| 148 | |||
| 149 | foreach ($matches as $command) { |
||
| 150 | $qualifiedCommandNames .= "\n" . $command->getName(); |
||
| 151 | } |
||
| 152 | |||
| 153 | throw new AmbiguousCommandException(s( |
||
| 154 | 'Please be more precise with the command name. ' . |
||
| 155 | 'There are several commands that qualify for "%s": %s', |
||
| 156 | $commandName, |
||
| 157 | $qualifiedCommandNames |
||
| 158 | )); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $commandName |
||
| 164 | * @param string $search |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function compareCommandName($commandName, $search) { |
||
| 169 | $commandName = preg_split('/(:|-|_)/', $commandName); |
||
| 170 | $search = preg_split('/(:|-|_)/', $search); |
||
| 171 | |||
| 172 | foreach ($search as $index => $searchPart) { |
||
| 173 | $stringPart = array_get($commandName, $index); |
||
| 174 | |||
| 175 | if ($stringPart === null) { |
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | if ( ! str_starts_with($stringPart, $searchPart)) { |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return true; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param IArgument $argument |
||
| 189 | * @param array $groupedArgs |
||
| 190 | * @param bool $strict |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | * @throws MissingArgumentValueException |
||
| 194 | */ |
||
| 195 | public function matchArgument(IArgument $argument, array $groupedArgs, $strict = true) { |
||
| 196 | $arg = null; |
||
| 197 | |||
| 198 | if (array_has($groupedArgs, 'arguments')) { |
||
| 199 | if ($argument->isSingle()) { |
||
| 200 | $arg = array_shift($groupedArgs['arguments']); |
||
| 201 | } else if ($argument->isMultiple()) { |
||
| 202 | $arg = array_get($groupedArgs, 'arguments'); |
||
| 203 | array_set($groupedArgs, 'arguments', []); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($argument->isRequired() && $strict && ($arg === null || $arg === [])) { |
||
| 208 | throw new MissingArgumentValueException(s( |
||
| 209 | 'Missing argument value "%s".', $argument->getName() |
||
| 210 | )); |
||
| 211 | } |
||
| 212 | |||
| 213 | $argument->setValue($arg); |
||
| 214 | |||
| 215 | return $groupedArgs; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param IOption $option |
||
| 220 | * @param array $groupedArgs |
||
| 221 | * @param bool $strict |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | * @throws InvalidOptionValueException |
||
| 225 | * @throws MissingOptionValueException |
||
| 226 | */ |
||
| 227 | public function matchOption(IOption $option, array $groupedArgs, $strict = true) { |
||
| 228 | $groupedArgs = $this->argumentsParser |
||
| 229 | ->mergeNameAndAlias($groupedArgs, $option->getName(), $option->getAlias()); |
||
| 230 | |||
| 231 | if ($option->isIncremental()) { |
||
| 232 | $groupedArgs = $this->matchIncrementalOption($option, $groupedArgs, $strict); |
||
| 233 | } else if ($option->isBoolean()) { |
||
| 234 | $groupedArgs = $this->matchBooleanOption($option, $groupedArgs, $strict); |
||
| 235 | } else { |
||
| 236 | $groupedArgs = $this->matchRegularOption($option, $groupedArgs, $strict); |
||
| 237 | } |
||
| 238 | |||
| 239 | return $groupedArgs; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param IOption $option |
||
| 244 | * @param array $groupedArgs |
||
| 245 | * @param bool $strict |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | protected function matchIncrementalOption(IOption $option, array $groupedArgs, $strict) { |
||
|
0 ignored issues
–
show
|
|||
| 250 | if (array_has($groupedArgs, $option->getNameOrAlias())) { |
||
| 251 | $values = array_take($groupedArgs, $option->getNameOrAlias()); |
||
| 252 | $optionCount = array_get($groupedArgs, s('options.%s', $option->getNameOrAlias()), 0); |
||
| 253 | $valueProvided = false; |
||
| 254 | |||
| 255 | foreach ($values as $value) { |
||
| 256 | if (is_numeric($value)) { |
||
| 257 | $option->setValue(intval($value)); |
||
| 258 | $valueProvided = true; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | if ( ! $valueProvided) { |
||
| 263 | $option->setValue($optionCount); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | return $groupedArgs; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param IOption $option |
||
| 272 | * @param array $groupedArgs |
||
| 273 | * @param bool $strict |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | * @throws InvalidOptionValueException |
||
| 277 | */ |
||
| 278 | protected function matchBooleanOption(IOption $option, array $groupedArgs, $strict) { |
||
| 279 | if (array_has($groupedArgs, $option->getNameOrAlias())) { |
||
| 280 | $values = array_take($groupedArgs, $option->getNameOrAlias()); |
||
| 281 | |||
| 282 | if ($values === []) { |
||
| 283 | $option->setValue(true); |
||
| 284 | } else { |
||
| 285 | $value = array_pop($values); |
||
| 286 | |||
| 287 | if (array_contains([1, '1', true, 'true'], $value)) { |
||
| 288 | $option->setValue(true); |
||
| 289 | } else if (array_contains([0, '0', false, 'false'], $value)) { |
||
| 290 | $option->setValue(false); |
||
| 291 | } else if ($strict) { |
||
| 292 | throw new InvalidOptionValueException(s( |
||
| 293 | 'Boolean option "%s" expects one of these values: 0, 1, true, false.', |
||
| 294 | $option->getNameOrAlias() |
||
| 295 | )); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | return $groupedArgs; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param IOption $option |
||
| 305 | * @param array $groupedArgs |
||
| 306 | * @param bool $strict |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | * @throws MissingOptionValueException |
||
| 310 | */ |
||
| 311 | protected function matchRegularOption(IOption $option, array $groupedArgs, $strict) { |
||
| 312 | $isRequired = $option->isRequired(); |
||
| 313 | $hasValue = array_has($groupedArgs, $option->getNameOrAlias()) && |
||
| 314 | count(array_get($groupedArgs, $option->getNameOrAlias())) > 0; |
||
| 315 | |||
| 316 | if ($isRequired && ! $hasValue && $strict) { |
||
| 317 | throw new MissingOptionValueException(s( |
||
| 318 | 'Missing option value "%s".', $option->getNameOrAlias() |
||
| 319 | )); |
||
| 320 | } |
||
| 321 | |||
| 322 | if (array_has($groupedArgs, $option->getNameOrAlias())) { |
||
| 323 | $values = array_take($groupedArgs, $option->getNameOrAlias()); |
||
| 324 | |||
| 325 | if ($option->isSingle()) { |
||
| 326 | $option->setValue(array_pop($values)); |
||
| 327 | } else if ($option->isMultiple()) { |
||
| 328 | $option->setValue($values); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | return $groupedArgs; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return IArgumentsParser |
||
| 337 | */ |
||
| 338 | protected function createArgumentsParser() { |
||
| 339 | return new ArgumentsParser(); |
||
| 340 | } |
||
| 341 | } |
||
| 342 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: