Completed
Push — develop ( 84bfed...2b68dc )
by Paul
02:42
created

Lexer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNonCatchablePatterns() 0 3 1
A getCatchablePatterns() 0 3 1
A __construct() 0 3 1
A getType() 0 8 3
1
<?php
2
3
namespace PhpUnitGen\Annotation;
4
5
use Doctrine\Common\Lexer\AbstractLexer;
6
7
/**
8
 * Class Lexer.
9
 *
10
 * @author     Paul Thébaud <[email protected]>.
11
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
12
 * @license    https://opensource.org/licenses/MIT The MIT license.
13
 * @link       https://github.com/paul-thebaud/phpunit-generator
14
 * @since      Class available since Release 2.0.0.
15
 */
16
class Lexer extends AbstractLexer
17
{
18
    /**
19
     * All possible tokens.
20
     */
21
    public const T_ANNOTATION    = 0;
22
    public const T_O_PARENTHESIS = 1;
23
    public const T_C_PARENTHESIS = 2;
24
    public const T_BACKSLASH     = 3;
25
    public const T_SINGLE_QUOTE  = 4;
26
    public const T_DOUBLE_QUOTE  = 5;
27
    public const T_ASTERISK      = 6;
28
    public const T_LINE_BREAK    = 7;
29
    public const T_WHITESPACE    = 8;
30
    public const T_OTHER         = 9;
31
32
    /**
33
     * @var array PATTERNS_TOKENS Matching array between regex pattern and token.
34
     */
35
    public const PATTERNS_TOKENS = [
36
        '(@(?i)(PhpUnitGen|Pug)\\\\[a-zA-Z0-9]+)' => Lexer::T_ANNOTATION,
37
        '(\()'                                    => Lexer::T_O_PARENTHESIS,
38
        '(\))'                                    => Lexer::T_C_PARENTHESIS,
39
        '(\\\\)'                                  => Lexer::T_BACKSLASH,
40
        '(\')'                                    => Lexer::T_SINGLE_QUOTE,
41
        '(")'                                     => Lexer::T_DOUBLE_QUOTE,
42
        '(\*)'                                    => Lexer::T_ASTERISK,
43
        '(\r|\n|\r\n|\n\r)'                       => Lexer::T_LINE_BREAK,
44
        '(\s)'                                    => Lexer::T_WHITESPACE,
45
        '([^\\\\"\'\(\)\*\s]+)'                   => Lexer::T_OTHER,
46
    ];
47
48
    /**
49
     * @var string[] $patterns Array containing regex patterns.
50
     */
51
    private $patterns;
52
53
    /**
54
     * Lexer constructor.
55
     */
56
    public function __construct()
57
    {
58
        $this->patterns = array_keys(Lexer::PATTERNS_TOKENS);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function getCatchablePatterns(): array
65
    {
66
        return $this->patterns;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function getNonCatchablePatterns(): array
73
    {
74
        return [];
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected function getType(&$value): int
81
    {
82
        foreach (Lexer::PATTERNS_TOKENS as $pattern => $token) {
83
            if (preg_match($pattern, $value)) {
84
                return $token;
85
            }
86
        }
87
        return Lexer::T_OTHER;
88
    }
89
}
90