Passed
Pull Request — master (#315)
by Théo
02:34
created

SyntaxException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A expectedToken() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Annotation\Exception;
16
17
use Doctrine\Common\Annotations\DocLexer;
18
19
/**
20
 * This exception is thrown if the annotation syntax is not valid.
21
 *
22
 * @author Kevin Herrera <[email protected]>
23
 */
24
class SyntaxException extends Exception
25
{
26
    /**
27
     * Creates a new exception for the expected token.
28
     *
29
     * @param string   $expected the expected token
30
     * @param array    $token    the actual token
31
     * @param DocLexer $lexer    the lexer
32
     *
33
     * @return SyntaxException the new exception
34
     */
35
    public static function expectedToken(
36
        $expected,
37
        array $token = null,
38
        DocLexer $lexer = null
39
    ): self {
40
        if ((null === $token) && $lexer) {
41
            $token = $lexer->lookahead;
42
        }
43
44
        $message = "Expected $expected, received ";
45
46
        if ($token) {
47
            $message .= "'{$token['value']}' at position {$token['position']}.";
48
        } else {
49
            $message .= 'end of string.';
50
        }
51
52
        return new self($message);
53
    }
54
}
55