AnnotationException.php 4.2 KB

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