SpacingContextTrait::skipSpacing()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 11
nc 6
nop 2
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
c 1
b 1
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Core\Context;
4
5
use Vanderlee\Comprehend\Core\ArgumentsTrait;
6
use Vanderlee\Comprehend\Match\Success;
7
use Vanderlee\Comprehend\Parser\Parser;
8
9
trait SpacingContextTrait
10
{
11
    use ArgumentsTrait;
12
13
    /**
14
     * List of spacers.
15
     *  -  Parser for a normal spacer.
16
     *  -  `null` to disable spacing.
17
     *  -  `true` to use the top-most spacer.
18
     *
19
     * @var Parser[]|null[]|bool[]
20
     */
21
    private $spacers = [];
22
23 513
    public function pushSpacer($skipper = null)
24
    {
25 513
        $this->spacers[] = ($skipper === null || $skipper === true)
26 513
            ? $skipper
27 29
            : $this->getArgument($skipper);
28 513
    }
29
30 29
    public function popSpacer()
31
    {
32 29
        array_pop($this->spacers);
33 29
    }
34
35 176
    public function skipSpacing($in, $offset)
36
    {
37 176
        $spacer = end($this->spacers);
38
39
        // If `true`; use top-most Parser instance in stack.
40 176
        if ($spacer === true) {
41
            do {
42 6
                $spacer = prev($this->spacers);
43 6
            } while ($spacer !== false && !($spacer instanceof Parser));
44
        }
45
46 176
        if ($spacer instanceof Parser) {
47 27
            $match = $spacer->match($in, $offset);
48
49 27
            return ($match instanceof Success)
50 26
                ? $match->length
51 27
                : false;
52
        }
53
54 167
        return 0;
55
    }
56
}
57