1 | <?php |
||
2 | |||
3 | namespace petitparser; |
||
4 | |||
5 | /** |
||
6 | * A parser that uses the first parser that succeeds. |
||
7 | */ |
||
8 | class ChoiceParser extends ListParser |
||
9 | { |
||
10 | /** |
||
11 | * @param Context $context |
||
12 | * |
||
13 | * @return Result[] |
||
14 | */ |
||
15 | 1 | public function parseOn(Context $context) |
|
16 | { |
||
17 | 1 | for ($i=0; $i < count($this->_parsers); $i++) { |
|
0 ignored issues
–
show
|
|||
18 | 1 | $result = $this->_parsers[$i]->parseOn($context); |
|
19 | |||
20 | 1 | if ($result->isSuccess()) { |
|
21 | 1 | return $result; |
|
22 | } |
||
23 | 1 | } |
|
24 | |||
25 | /** @noinspection PhpUndefinedVariableInspection */ |
||
26 | 1 | return $result; |
|
27 | } |
||
28 | |||
29 | /** |
||
30 | * @inheritdoc |
||
31 | */ |
||
32 | 1 | public function or_(Parser $other) |
|
33 | { |
||
34 | 1 | $parsers = $this->_parsers; |
|
35 | 1 | $parsers[] = $other; |
|
36 | |||
37 | 1 | return new ChoiceParser($parsers); |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @return Parser |
||
42 | */ |
||
43 | 1 | function copy() { |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
copy .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||
44 | 1 | return new ChoiceParser($this->_parsers); |
|
45 | } |
||
46 | } |
||
47 |
If you have a function call in the test part of a
for
loop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.