Issues (12)

src/ChoiceParser.php (1 issue)

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
Performance Best Practice introduced by
Consider avoiding function calls on each iteration of the for loop.

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.

// count() is called on each iteration
for ($i=0; $i < count($collection); $i++) { }

// count() is only called once
for ($i=0, $c=count($collection); $i<$c; $i++) { }
Loading history...
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() {
44 1
        return new ChoiceParser($this->_parsers);
45
    }
46
}
47