AnnotationException::typeError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Common\Annotations;
4
5
/**
6
 * Description of AnnotationException
7
 *
8
 * @since  2.0
9
 * @author Benjamin Eberlei <[email protected]>
10
 * @author Guilherme Blanco <[email protected]>
11
 * @author Jonathan Wage <[email protected]>
12
 * @author Roman Borschel <[email protected]>
13
 */
14
class AnnotationException extends \Exception
15
{
16
    /**
17
     * Creates a new AnnotationException describing a Syntax error.
18
     *
19
     * @param string $message Exception message
20
     *
21
     * @return AnnotationException
22
     */
23
    public static function syntaxError($message)
24
    {
25
        return new self('[Syntax Error] ' . $message);
26
    }
27
28
    /**
29
     * Creates a new AnnotationException describing a Semantical error.
30
     *
31
     * @param string $message Exception message
32
     *
33
     * @return AnnotationException
34
     */
35
    public static function semanticalError($message)
36
    {
37
        return new self('[Semantical Error] ' . $message);
38
    }
39
40
    /**
41
     * Creates a new AnnotationException describing an error which occurred during
42
     * the creation of the annotation.
43
     *
44
     * @since 2.2
45
     *
46
     * @param string $message
47
     *
48
     * @return AnnotationException
49
     */
50
    public static function creationError($message)
51
    {
52
        return new self('[Creation Error] ' . $message);
53
    }
54
55
    /**
56
     * Creates a new AnnotationException describing a type error.
57
     *
58
     * @since 1.1
59
     *
60
     * @param string $message
61
     *
62
     * @return AnnotationException
63
     */
64
    public static function typeError($message)
65
    {
66
        return new self('[Type Error] ' . $message);
67
    }
68
69
    /**
70
     * Creates a new AnnotationException describing a constant semantical error.
71
     *
72
     * @since 2.3
73
     *
74
     * @param string $identifier
75
     * @param string $context
76
     *
77
     * @return AnnotationException
78
     */
79
    public static function semanticalErrorConstants($identifier, $context = null)
80
    {
81
        return self::semanticalError(sprintf(
82
            "Couldn't find constant %s%s.",
83
            $identifier,
84
            $context ? ', ' . $context : ''
85
        ));
86
    }
87
88
    /**
89
     * Creates a new AnnotationException describing an type error of an attribute.
90
     *
91
     * @since 2.2
92
     *
93
     * @param string $attributeName
94
     * @param string $annotationName
95
     * @param string $context
96
     * @param string $expected
97
     * @param mixed  $actual
98
     *
99
     * @return AnnotationException
100
     */
101
    public static function attributeTypeError($attributeName, $annotationName, $context, $expected, $actual)
102
    {
103
        return self::typeError(sprintf(
104
            'Attribute "%s" of @%s declared on %s expects %s, but got %s.',
105
            $attributeName,
106
            $annotationName,
107
            $context,
108
            $expected,
109
            is_object($actual) ? 'an instance of ' . get_class($actual) : gettype($actual)
110
        ));
111
    }
112
113
    /**
114
     * Creates a new AnnotationException describing an required error of an attribute.
115
     *
116
     * @since 2.2
117
     *
118
     * @param string $attributeName
119
     * @param string $annotationName
120
     * @param string $context
121
     * @param string $expected
122
     *
123
     * @return AnnotationException
124
     */
125
    public static function requiredError($attributeName, $annotationName, $context, $expected)
126
    {
127
        return self::typeError(sprintf(
128
            'Attribute "%s" of @%s declared on %s expects %s. This value should not be null.',
129
            $attributeName,
130
            $annotationName,
131
            $context,
132
            $expected
133
        ));
134
    }
135
136
    /**
137
     * Creates a new AnnotationException describing a invalid enummerator.
138
     *
139
     * @since 2.4
140
     *
141
     * @param string $attributeName
142
     * @param string $annotationName
143
     * @param string $context
144
     * @param array  $available
145
     * @param mixed  $given
146
     *
147
     * @return AnnotationException
148
     */
149
    public static function enumeratorError($attributeName, $annotationName, $context, $available, $given)
150
    {
151
        return new self(sprintf(
152
            '[Enum Error] Attribute "%s" of @%s declared on %s accept only [%s], but got %s.',
153
            $attributeName, 
154
            $annotationName,
155
            $context,
156
            implode(', ', $available),
157
            is_object($given) ? get_class($given) : $given
158
        ));
159
    }
160
161
    /**
162
     * @return AnnotationException
163
     */
164
    public static function optimizerPlusSaveComments()
165
    {
166
        return new self(
167
            "You have to enable opcache.save_comments=1 or zend_optimizerplus.save_comments=1."
168
        );
169
    }
170
171
    /**
172
     * @return AnnotationException
173
     */
174
    public static function optimizerPlusLoadComments()
175
    {
176
        return new self(
177
            "You have to enable opcache.load_comments=1 or zend_optimizerplus.load_comments=1."
178
        );
179
    }
180
}
181