DefinitionLexer::getNextToken()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 0
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (c) 2015 Juan José Torroglosa Ramón
4
 *
5
 * This file is part of the Cliphar package.
6
 *
7
 * For the full copyright and license information, please view
8
 * the LICENSE file that was distributed with this source code.
9
 */
10
11
namespace Cliphar\InputDefinition\Lexer;
12
13
14
use Cliphar\InputDefinition\Exception\LexerException;
15
16
class DefinitionLexer
17
{
18
    const T_OPEN_OPTION_SYMBOL    = "T_OPEN_OPTION_SYMBOL";
19
    const T_CLOSE_OPTION_SYMBOL   = "T_CLOSE_OPTION_SYMBOL";
20
    const T_OPEN_ARGUMENT_SYMBOL  = "T_OPEN_ARGUMENT_SYMBOL";
21
    const T_CLOSE_ARGUMENT_SYMBOL = "T_CLOSE_ARGUMENT_SYMBOL";
22
    const T_NAME                  = "T_NAME";
23
    const T_ABBREV                = "T_ABBREV";
24
    const T_ABBREV_SEPARATOR      = "T_ABBREV_SEPARATOR";
25
    const T_STRING_WITH_SPACES    = "T_STRING_WITH_SPACES";
26
    const T_EQUAL_SIGN            = "T_EQUAL_SIGN";
27
    const T_OPTIONAL_MARK         = "T_OPTIONAL_MARK";
28
    const T_WHITESPACES           = "T_WHITESPACES";
29
30
    private static $tokenToRegex = null;
31
32
    private static function getTokenToRegex()
33
    {
34
        if (self::$tokenToRegex === null) {
35
            self::$tokenToRegex = array(
36
                self::T_OPEN_OPTION_SYMBOL    => '^\[',
37
                self::T_CLOSE_OPTION_SYMBOL   => '^\]',
38
                self::T_OPEN_ARGUMENT_SYMBOL  => '^\<',
39
                self::T_CLOSE_ARGUMENT_SYMBOL => '^\>',
40
                self::T_OPTIONAL_MARK         => '^\?',
41
                self::T_EQUAL_SIGN            => '^=',
42
                self::T_STRING_WITH_SPACES    => '^"[^"]*"',
43
                self::T_NAME                  => '^[A-Za-z0-9\-\_]{2,}',
44
                self::T_ABBREV                => '^[A-Za-z]',
45
                self::T_ABBREV_SEPARATOR      => '^\|',
46
                self::T_WHITESPACES           => '^\s+'
47
            );
48
        }
49
50
        return self::$tokenToRegex;
51
    }
52
53
    /**
54
     * @var string
55
     */
56
    private $string;
57
58
    /**
59
     * @var int
60
     */
61
    private $strlen;
62
63
    /**
64
     * @var int
65
     */
66
    private $offset = 0;
67
68
    /**
69
     * @var string
70
     */
71
    private $lastOccurrence;
72
73
    /**
74
     * DefinitionLexer constructor.
75
     * @param string $string
76
     */
77
    public function __construct($string)
78
    {
79
        $this->string = $string;
80
        $this->strlen = strlen($string);
81
    }
82
83
    public function getNextToken()
84
    {
85
        foreach (self::getTokenToRegex() as $token => $regex) {
86
            if ($this->offset >= $this->strlen) {
87
                $this->lastOccurrence = array(null, null, null);
0 ignored issues
show
Documentation Bug introduced by
It seems like array(null, null, null) of type array<integer,null,{"0":..."1":"null","2":"null"}> is incompatible with the declared type string of property $lastOccurrence.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
                return $this->lastOccurrence;
89
            }
90
            $substr = substr($this->string, $this->offset);
91
            $result = preg_match('~'. $regex . '~', $substr, $matches, 0);
92
            if ($result) {
93
                $this->offset += strlen($matches[0]);
94
                $this->lastOccurrence = array($token, $matches[0], $this->offset);
0 ignored issues
show
Documentation Bug introduced by
It seems like array($token, $matches[0], $this->offset) of type array<integer,?,{"0":"in..."1":"?","2":"integer"}> is incompatible with the declared type string of property $lastOccurrence.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
95
                return $this->lastOccurrence;
96
            }
97
        }
98
99
        throw new LexerException("Unexpected character found at: {$substr}". PHP_EOL);
100
    }
101
102
    public function getLastOccurrence()
103
    {
104
        return $this->lastOccurrence;
105
    }
106
}