SequenceMatcher::matchesList()   C
last analyzed

Complexity

Conditions 8
Paths 16

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 38
ccs 23
cts 23
cp 1
rs 5.3846
cc 8
eloc 20
nc 16
nop 1
crap 8
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 * 
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Framework\Test\Matchers;
26
27
use Hamcrest\Description;
28
29
/**
30
 * A matcher that will match if all the given <code>matchers</code> each match against an item that the previous
31
 * matcher matched against. That means the second matcher should match an item that follow the item that the first
32
 * matcher matched.
33
 * <p/>
34
 * If the number of items is larger than the number of matchers, the excess items are not evaluated. Use {@link
35
 * Matchers#exactSequenceOf(Hamcrest\Matcher[])} to match the sequence exactly. If the last item of the list
36
 * has been evaluated, and Matchers still remain, they are evaluated against a <code>null</code> value.
37
 * 
38
 * @author    "David Kalosi" <[email protected]>  
39
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> 
40
 */
41
class SequenceMatcher extends ListMatcher
42
{
43
44
    /**
45
     * Construct a matcher that will return true if all the given <code>matchers</code> match against an item
46
     * positioned after the item that the previous matcher matched against.
47
     *
48
     * @param array $matchers The matchers that must match against at least one item in the list.
49
     */
50 6
    public function __construct(array $matchers)
51
    {
52 6
        parent::__construct($matchers);
53 6
    }
54
55 6
    public function matchesList(array $items)
56
    {
57 6
        $itemIterator = new \ArrayIterator($items);
58 6
        $matcherIterator = new \ArrayIterator($this->getMatchers());
59 6
        $currentMatcher = null;
60
61 6
        if ($matcherIterator->valid()) {
62 6
            $currentMatcher = $matcherIterator->current();            
63 6
        }
64
65 6
        while ($itemIterator->valid() && null !== $currentMatcher) {
66 6
            $hasMatch = $currentMatcher->matches($itemIterator->current());
67
68 6
            if ($hasMatch) {
69 5
                $matcherIterator->next();
70
                
71 5
                if ($matcherIterator->valid()) {
72 5
                    $currentMatcher = $matcherIterator->current();
73 5
                } else {
74 1
                    $currentMatcher = null;
75
                }
76 5
            }
77
78 6
            $itemIterator->next();
79 6
        }
80
81
        //echo sprintf("%s->%s, %s->%s\n", $itemIterator->key(), $itemIterator->count(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
        //      $matcherIterator->key(), $matcherIterator->count());
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
84 6
        if (null !== $currentMatcher && !$currentMatcher->matches(null)) {
85 2
            $this->reportFailed($currentMatcher);
86 2
            return false;
87
        }
88
        
89 4
        $matcherIterator->next();
90
91 4
        return $this->matchRemainder($matcherIterator);
92
    }
93
94 2
    protected function describeCollectionType(Description $description)
95
    {
96 2
        $description->appendText("sequence");
97 2
    }
98
99
}
100