AnnotationException.php 4.5 KB

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