Lexer::getNonCatchablePatterns()   A
last analyzed

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
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)
28
    {
29
        try {
30
            return $this->getType($type);
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
     * @return int|string|null
59
     */
60
    protected function getType(&$value)
61
    {
62
        $type = self::T_UNKNOWN;
63
64
        switch (true) {
65
            // Recognize numeric values
66
            case is_numeric($value):
67
                if (false !== strpos($value, '.') || false !== stripos($value, 'e')) {
68
                    return self::T_FLOAT;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_FLOAT returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
69
                }
70
71
                return self::T_INTEGER;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_INTEGER returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
72
73
            // Recognize quoted strings
74
            case "'" === $value[0]:
75
                $value = str_replace("''", "'", substr($value, 1, strlen($value) - 2));
76
77
                return self::T_STRING;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_STRING returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
78
79
            case '"' === $value[0]:
80
                $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
81
82
                return self::T_STRING;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_STRING returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
83
84
            case 'null' === $value:
85
                return self::T_NULL;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_NULL returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
86
87
            // Recognize identifiers, aliased or qualified names
88
            case ctype_alpha($value[0]) || '\\' === $value[0]:
89
                return self::T_IDENTIFIER;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_IDENTIFIER returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
90
91
            case ',' === $value:
92
                return self::T_COMMA;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_COMMA returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
93
94
            case '>' === $value:
95
                return self::T_TYPE_END;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_TYPE_END returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
96
97
            case '<' === $value:
98
                return self::T_TYPE_START;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_TYPE_START returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
99
100
            case ']' === $value:
101
                return self::T_ARRAY_END;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_ARRAY_END returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
102
103
            case '[' === $value:
104
                return self::T_ARRAY_START;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::T_ARRAY_START returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
105
106
            // Default
107
            default:
108
                // Do nothing
109
        }
110
111
        return $type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $type returns the type integer which is incompatible with the return type mandated by Doctrine\Common\Lexer\AbstractLexer::getType() of Doctrine\Common\Lexer\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
112
    }
113
}
114