Passed
Pull Request — master (#1212)
by Asmir
12:10
created

Lexer::getNonCatchablePatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
/**
11
 * @internal
12
 */
13
final class Lexer extends AbstractLexer implements ParserInterface
14
{
15
    public const T_UNKNOWN = 0;
16
    public const T_INTEGER = 1;
17
    public const T_STRING = 2;
18
    public const T_FLOAT = 3;
19
    public const T_ARRAY_START = 4;
20
    public const T_ARRAY_END = 5;
21
    public const T_COMMA = 6;
22
    public const T_TYPE_START = 7;
23
    public const T_TYPE_END = 8;
24
    public const T_IDENTIFIER = 9;
25
    public const T_NULL = 10;
26
27
    public function parse(string $type): array
28
    {
29
        try {
30
            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...
31
        } catch (\Throwable $e) {
32
            throw new SyntaxError($e->getMessage(), 0, $e);
33
        }
34
    }
35
36
    protected function getCatchablePatterns(): array
37
    {
38
        return [
39
            '[a-z][a-z_\\\\0-9]*', // identifier or qualified name
40
            "'(?:[^']|'')*'", // single quoted strings
41
            '(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?', // numbers
42
            '"(?:[^"]|"")*"', // double quoted strings
43
            '<',
44
            '>',
45
            '\\[',
46
            '\\]',
47
        ];
48
    }
49
50
    protected function getNonCatchablePatterns(): array
51
    {
52
        return ['\s+'];
53
    }
54
55
    /**
56
     * {{@inheritDoc}}
57
     */
58
    protected function getType(&$value)
59
    {
60
        $type = self::T_UNKNOWN;
61
62
        switch (true) {
63
            // Recognize numeric values
64
            case is_numeric($value):
65
                if (false !== strpos($value, '.') || false !== stripos($value, 'e')) {
66
                    return self::T_FLOAT;
67
                }
68
69
                return self::T_INTEGER;
70
71
            // Recognize quoted strings
72
            case "'" === $value[0]:
73
                $value = str_replace("''", "'", substr($value, 1, strlen($value) - 2));
74
75
                return self::T_STRING;
76
            case '"' === $value[0]:
77
                $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
78
79
                return self::T_STRING;
80
            case 'null' === $value:
81
                return self::T_NULL;
82
            // Recognize identifiers, aliased or qualified names
83
            case ctype_alpha($value[0]) || '\\' === $value[0]:
84
                return self::T_IDENTIFIER;
85
            case ',' === $value:
86
                return self::T_COMMA;
87
            case '>' === $value:
88
                return self::T_TYPE_END;
89
            case '<' === $value:
90
                return self::T_TYPE_START;
91
            case ']' === $value:
92
                return self::T_ARRAY_END;
93
            case '[' === $value:
94
                return self::T_ARRAY_START;
95
96
            // 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...
97
            default:
98
                // Do nothing
99
        }
100
101
        return $type;
102
    }
103
}
104