Passed
Push — develop ( b3a8c4...8c60cf )
by Paul
02:03
created

AnnotationLexer::getPatternsToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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