| Total Complexity | 16 |
| Total Lines | 95 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | class OptionMatcher |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string|string[] |
||
| 18 | */ |
||
| 19 | private $option; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | private $equals; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $option |
||
| 28 | * @param string $arg |
||
| 29 | * @param bool $equals |
||
| 30 | * |
||
| 31 | * @return bool |
||
| 32 | */ |
||
| 33 | 65 | public static function matchOptionArg($option, $arg, $equals) |
|
| 34 | { |
||
| 35 | 65 | $optLen = strlen($option); |
|
| 36 | 65 | if (0 === $optLen) { |
|
| 37 | 12 | return false; |
|
| 38 | } |
||
| 39 | |||
| 40 | 53 | $argLen = strlen($arg); |
|
| 41 | 53 | if ($argLen < 2) { |
|
| 42 | 4 | return false; |
|
| 43 | } |
||
| 44 | |||
| 45 | 49 | if ('--' === $arg) { |
|
| 46 | 4 | return false; |
|
| 47 | } |
||
| 48 | |||
| 49 | 45 | if ('-' !== $arg[0]) { |
|
| 50 | 4 | return false; |
|
| 51 | } |
||
| 52 | |||
| 53 | 41 | if ((1 !== $optLen) !== ('-' === $arg[1])) { |
|
| 54 | 4 | return false; |
|
| 55 | } |
||
| 56 | |||
| 57 | 37 | $buffer = substr($arg, 1 === $optLen ? 1 : 2); |
|
| 58 | |||
| 59 | 37 | if ($equals && false !== $posEquals = strpos($buffer, '=')) { |
|
| 60 | 15 | if (0 === $posEquals) { |
|
| 61 | 2 | return false; |
|
| 62 | } |
||
| 63 | 13 | $buffer = substr($buffer, 0, $posEquals); |
|
| 64 | } |
||
| 65 | |||
| 66 | 35 | if (1 === $optLen) { |
|
| 67 | 22 | return false !== strpos($buffer, $option); |
|
| 68 | } |
||
| 69 | |||
| 70 | 13 | return $buffer === $option; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param $option |
||
| 75 | * |
||
| 76 | * @return OptionMatcher |
||
| 77 | */ |
||
| 78 | 1 | public static function create($option) |
|
| 79 | { |
||
| 80 | 1 | return new self($option, true); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * OptionMatcher constructor. |
||
| 85 | * |
||
| 86 | * @param string|string[] $option |
||
| 87 | * @param bool $equals support equals-sign while matching |
||
| 88 | */ |
||
| 89 | 33 | public function __construct($option, $equals) |
|
| 93 | 33 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $arg |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | 33 | public function match($arg) |
|
| 111 |