Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
OffsetTracker
94.44% covered (success)
94.44%
17 / 18
80.00% covered (warning)
80.00%
4 / 5
12.02
0.00% covered (danger)
0.00%
0 / 1
 append
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 appendNodes
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 overlap
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 getOffsets
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMinimalUnconsumedOffset
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace CirrusSearch\Parser\QueryStringRegex;
4
5use Wikimedia\Assert\Assert;
6
7/**
8 * Simple class to track offsets
9 * Supports only simple cases (no support for overlapping offsets)
10 */
11class OffsetTracker {
12
13    /**
14     * Always ordered with no overlapping offsets
15     * @var int[] array of end offsets indexed by start offsets
16     */
17    private $offsets = [];
18
19    /**
20     * @param int $start start offset (inclusive)
21     * @param int $end end offset (exclusive)
22     */
23    public function append( $start, $end ) {
24        Assert::precondition( $start < $end, '$start < $end' );
25        $this->offsets[$start] = $end;
26        ksort( $this->offsets );
27    }
28
29    /**
30     * Append an array of ParsedNode into the tracker.
31     * The array must not contain any overlapping node.
32     * @param \CirrusSearch\Parser\AST\ParsedNode[] $nodes
33     */
34    public function appendNodes( array $nodes ) {
35        foreach ( $nodes as $node ) {
36            // slow: Assert::invariant( $this->overlap( $node->getStartOffset(), $node->getEndOffset() ) );
37            $this->offsets[$node->getStartOffset()] = $node->getEndOffset();
38        }
39        ksort( $this->offsets );
40    }
41
42    /**
43     * @param int $start
44     * @param int $end
45     * @return bool
46     */
47    public function overlap( $start, $end ) {
48        Assert::precondition( $start < $end, '$start < $end' );
49        foreach ( $this->offsets as $s => $e ) {
50            if ( $e > $start && $s < $end ) {
51                return true;
52            }
53        }
54        return false;
55    }
56
57    /**
58     * @return int[]
59     */
60    public function getOffsets() {
61        return $this->offsets;
62    }
63
64    /**
65     * return this first unconsumed offset in this tracker
66     * greater than $offset
67     * @param int $offset
68     * @return int
69     */
70    public function getMinimalUnconsumedOffset( $offset = 0 ) {
71        Assert::precondition( $offset >= 0, '$offset >= 0' );
72        foreach ( $this->offsets as $start => $end ) {
73            if ( $offset < $start ) {
74                return $offset;
75            }
76            $offset = $end > $offset ? $end : $offset;
77        }
78        return $offset;
79    }
80}