PropertyDoc.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\VarTag;
  9. use yii\apidoc\helpers\PrettyPrinter;
  10. /**
  11. * Represents API documentation information for a `property`.
  12. *
  13. * @property bool $isReadOnly If property is read only. This property is read-only.
  14. * @property bool $isWriteOnly If property is write only. This property is read-only.
  15. *
  16. * @author Carsten Brandt <mail@cebe.cc>
  17. * @since 2.0
  18. */
  19. class PropertyDoc extends BaseDoc
  20. {
  21. public $visibility;
  22. public $isStatic;
  23. public $type;
  24. public $types;
  25. public $defaultValue;
  26. // will be set by creating class
  27. public $getter;
  28. public $setter;
  29. // will be set by creating class
  30. public $definedBy;
  31. /**
  32. * @return bool if property is read only
  33. */
  34. public function getIsReadOnly()
  35. {
  36. return $this->getter !== null && $this->setter === null;
  37. }
  38. /**
  39. * @return bool if property is write only
  40. */
  41. public function getIsWriteOnly()
  42. {
  43. return $this->getter === null && $this->setter !== null;
  44. }
  45. /**
  46. * @param \phpDocumentor\Reflection\ClassReflector\PropertyReflector $reflector
  47. * @param Context $context
  48. * @param array $config
  49. */
  50. public function __construct($reflector = null, $context = null, $config = [])
  51. {
  52. parent::__construct($reflector, $context, $config);
  53. if ($reflector === null) {
  54. return;
  55. }
  56. $this->visibility = $reflector->getVisibility();
  57. $this->isStatic = $reflector->isStatic();
  58. // bypass $reflector->getDefault() for short array syntax
  59. if ($reflector->getNode()->default) {
  60. $this->defaultValue = PrettyPrinter::getRepresentationOfValue($reflector->getNode()->default);
  61. }
  62. $hasInheritdoc = false;
  63. foreach ($this->tags as $tag) {
  64. if ($tag->getName() === 'inheritdoc') {
  65. $hasInheritdoc = true;
  66. }
  67. if ($tag instanceof VarTag) {
  68. $this->type = $tag->getType();
  69. $this->types = $tag->getTypes();
  70. $this->description = static::mbUcFirst($tag->getDescription());
  71. $this->shortDescription = BaseDoc::extractFirstSentence($this->description);
  72. }
  73. }
  74. if (empty($this->shortDescription) && $context !== null && !$hasInheritdoc) {
  75. $context->warnings[] = [
  76. 'line' => $this->startLine,
  77. 'file' => $this->sourceFile,
  78. 'message' => "No short description for element '{$this->name}'",
  79. ];
  80. }
  81. }
  82. }