Completed
Pull Request — master (#1212)
by Asmir
11:43
created

Lexer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 90
rs 10
c 1
b 0
f 0
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCatchablePatterns() 0 12 1
A parse() 0 6 2
A getNonCatchablePatterns() 0 3 1
C getType() 0 44 14
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Type;
6
7
use Doctrine\Common\Lexer\AbstractLexer;
8
use JMS\Serializer\Type\Exception\SyntaxError;
9
10
class Lexer extends AbstractLexer implements ParserInterface
11
{
12
    public const T_UNKNOWN = 0;
13
    public const T_INTEGER = 1;
14
    public const T_STRING = 2;
15
    public const T_FLOAT = 3;
16
    public const T_ARRAY_START = 4;
17
    public const T_ARRAY_END = 5;
18
    public const T_COMMA = 6;
19
    public const T_TYPE_START = 7;
20
    public const T_TYPE_END = 8;
21
    public const T_IDENTIFIER = 9;
22
    public const T_NULL = 10;
23
24
    public function parse(string $type): array
25
    {
26
        try {
27
            return $this->getType($type);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getType($type) returns the type integer which is incompatible with the type-hinted return array.
Loading history...
28
        } catch (\Throwable $e) {
29
            throw new SyntaxError($e->getMessage(), 0, $e);
30
        }
31
    }
32
33
    protected function getCatchablePatterns(): array
34
    {
35
        return [
36
            '[a-z][a-z_\\\\0-9]*', // identifier or qualified name
37
            "'(?:[^']|'')*'", // single quoted strings
38
            '(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?', // numbers
39
            '"(?:[^"]|"")*"', // double quoted strings
40
41
            '<',
42
            '>',
43
            '\\[',
44
            '\\]',
45
        ];
46
    }
47
48
    protected function getNonCatchablePatterns(): array
49
    {
50
        return ['\s+'];
51
    }
52
53
    /**
54
     * {{@inheritDoc}}
55
     */
56
    protected function getType(&$value)
57
    {
58
        $type = self::T_UNKNOWN;
59
60
        switch (true) {
61
            // Recognize numeric values
62
            case is_numeric($value):
63
                if (false !== strpos($value, '.') || false !== stripos($value, 'e')) {
64
                    return self::T_FLOAT;
65
                }
66
67
                return self::T_INTEGER;
68
69
            // Recognize quoted strings
70
            case "'" === $value[0]:
71
                $value = str_replace("''", "'", substr($value, 1, strlen($value) - 2));
72
73
                return self::T_STRING;
74
            case '"' === $value[0]:
75
                $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
76
77
                return self::T_STRING;
78
            case 'null' === $value:
79
                return self::T_NULL;
80
            // Recognize identifiers, aliased or qualified names
81
            case ctype_alpha($value[0]) || '\\' === $value[0]:
82
                return self::T_IDENTIFIER;
83
            case ',' === $value:
84
                return self::T_COMMA;
85
            case '>' === $value:
86
                return self::T_TYPE_END;
87
            case '<' === $value:
88
                return self::T_TYPE_START;
89
            case ']' === $value:
90
                return self::T_ARRAY_END;
91
            case '[' === $value:
92
                return self::T_ARRAY_START;
93
94
            // Default
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
            default:
96
                // Do nothing
97
        }
98
99
        return $type;
100
    }
101
}
102