Completed
Pull Request — master (#5)
by X
02:25
created

OptionalSignParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 8
cts 8
cp 1
rs 10
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 9 2
A isSign() 0 4 2
1
<?php
2
3
/**
4
 * parser for optional sign
5
 */
6
namespace xKerman\Restricted;
7
8
/**
9
 * Parser for optional sign
10
 */
11
class OptionalSignParser implements ParserInterface
12
{
13
    /**
14
     * parse given `$source` and return sign if exist
15
     *
16
     * @param Source $source parser input
17
     * @return array
18
     */
19 14
    public function parse(Source $source)
20
    {
21 14
        if (!$this->isSign($source->peek())) {
22 7
            return ['', $source];
23
        }
24 7
        $sign = $source->peek();
25 7
        $source->next();
26 7
        return [$sign, $source];
27
    }
28
29
    /**
30
     * judge if given `$char` is minus|plus sign
31
     *
32
     * @param string $char target character
33
     * @return boolean
34
     */
35 14
    private function isSign($char)
36
    {
37 14
        return $char === '+' || $char === '-';
38
    }
39
}
40