Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

Integer::__construct()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 11
nc 5
nop 3
dl 0
loc 20
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Terminal;
4
5
use Vanderlee\Comprehend\Core\Context;
6
use Vanderlee\Comprehend\Parser\Parser;
7
8
/**
9
 * Matches an integer within the specified range
10
 *
11
 * @author Martijn
12
 */
13
class Integer extends Parser
14
{
15
    use CaseSensitiveTrait;
16
17
    /**
18
     * List of digits to use for the different bases (upto 36)
19
     * @var string
20
     */
21
    private static $set = '0123456789abcdefghijklmnopqrstuvwxyz';
22
23
    /**
24
     * @var int|null
25
     */
26
    private $minimum;
27
28
    /**
29
     * @var int|null
30
     */
31
    private $maximum;
32
33
    /**
34
     * @var int
35
     */
36
    private $base;
37
38
    public function __construct($minimum = 0, $maximum = null, $base = 10)
39
    {
40
        if ($minimum !== null && !is_int($minimum)) {
41
            throw new \Exception('Minimum must be integer or `null`');
42
        }
43
44
        if ($maximum !== null && !is_int($maximum)) {
45
            throw new \Exception('Maximum must be integer or `null`');
46
        }
47
48
        if ($minimum !== null && $maximum !== null && $minimum > $maximum) {
49
            throw new \Exception('Maximum must be greater than minimum');
50
        }
51
52
        $this->minimum = $minimum;
53
        $this->maximum = $maximum;
54
55
        $this->base = intval($base);
56
        if ($base < 2 || $base > strlen(self::$set)) {
57
            throw new \Exception('Invalid base');
58
        }
59
    }
60
61
    protected function parse(&$input, $offset, Context $context)
62
    {
63
        $this->pushCaseSensitivityToContext($context);
64
65
        // Build pattern
66
        $set0    = substr(self::$set, 0, $this->base);
67
        $set1    = substr(self::$set, 1, $this->base - 1);
68
        $pattern = '/^(?:0|-?[' . $set1 . '][' . $set0 . ']*)/' . ($context->isCaseSensitive() ? '' : 'i');
69
70
        $this->popCaseSensitivityFromContext($context);
71
72
        if (preg_match($pattern, $input, $match, 0, $offset) === 1) {
73
            do {
74
                $integer = intval($match[0], $this->base);
75
                if (($this->minimum === null || $integer >= $this->minimum)
76
                    && ($this->maximum === null || $integer <= $this->maximum)
77
                    && $match[0] !== '-') {
78
                    return $this->success($input, $offset, mb_strlen($match[0]));
79
                }
80
81
                $match[0] = substr($match[0], 0, -1); // strip off last char
82
            } while ($match[0] !== '');
83
        }
84
85
        return $this->failure($input, $offset);
86
    }
87
88
    public function __toString()
89
    {
90
        return ($this->minimum === null ? '<-INF' : ('[' . $this->minimum)) . ',' . ($this->maximum === null ? 'INF>' : ($this->maximum . ']'));
91
    }
92
93
}
94