FunctionDoc.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\apidoc\models;
  8. use phpDocumentor\Reflection\DocBlock\Tag\ParamTag;
  9. use phpDocumentor\Reflection\DocBlock\Tag\PropertyTag;
  10. use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
  11. use phpDocumentor\Reflection\DocBlock\Tag\ThrowsTag;
  12. /**
  13. * Represents API documentation information for a `function`.
  14. *
  15. * @author Carsten Brandt <mail@cebe.cc>
  16. * @since 2.0
  17. */
  18. class FunctionDoc extends BaseDoc
  19. {
  20. /**
  21. * @var ParamDoc[]
  22. */
  23. public $params = [];
  24. public $exceptions = [];
  25. public $return;
  26. public $returnType;
  27. public $returnTypes;
  28. public $isReturnByReference;
  29. /**
  30. * @param \phpDocumentor\Reflection\FunctionReflector $reflector
  31. * @param Context $context
  32. * @param array $config
  33. */
  34. public function __construct($reflector = null, $context = null, $config = [])
  35. {
  36. parent::__construct($reflector, $context, $config);
  37. if ($reflector === null) {
  38. return;
  39. }
  40. $this->isReturnByReference = $reflector->isByRef();
  41. foreach ($reflector->getArguments() as $arg) {
  42. $arg = new ParamDoc($arg, $context, ['sourceFile' => $this->sourceFile]);
  43. $this->params[$arg->name] = $arg;
  44. }
  45. foreach ($this->tags as $i => $tag) {
  46. if ($tag instanceof ThrowsTag) {
  47. $this->exceptions[$tag->getType()] = $tag->getDescription();
  48. unset($this->tags[$i]);
  49. } elseif ($tag instanceof PropertyTag) {
  50. // ignore property tag
  51. } elseif ($tag instanceof ParamTag) {
  52. $paramName = $tag->getVariableName();
  53. if (!isset($this->params[$paramName]) && $context !== null) {
  54. $context->errors[] = [
  55. 'line' => $this->startLine,
  56. 'file' => $this->sourceFile,
  57. 'message' => "Undefined parameter documented: $paramName in {$this->name}().",
  58. ];
  59. continue;
  60. }
  61. $this->params[$paramName]->description = static::mbUcFirst($tag->getDescription());
  62. $this->params[$paramName]->type = $tag->getType();
  63. $this->params[$paramName]->types = $tag->getTypes();
  64. unset($this->tags[$i]);
  65. } elseif ($tag instanceof ReturnTag) {
  66. $this->returnType = $tag->getType();
  67. $this->returnTypes = $tag->getTypes();
  68. $this->return = static::mbUcFirst($tag->getDescription());
  69. unset($this->tags[$i]);
  70. }
  71. }
  72. }
  73. }