AnnotationException.php 4.6 KB

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