PropertyDoc.php 2.68 KB
Newer Older
1 2
<?php
/**
3 4 5
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
6 7
 */

8
namespace yii\apidoc\models;
9

10
use phpDocumentor\Reflection\DocBlock\Tag\VarTag;
Carsten Brandt committed
11
use yii\apidoc\helpers\PrettyPrinter;
12

13
/**
14
 * Represents API documentation information for a `property`.
15
 *
Qiang Xue committed
16 17 18
 * @property bool $isReadOnly If property is read only. This property is read-only.
 * @property bool $isWriteOnly If property is write only. This property is read-only.
 *
19 20 21
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
22 23
class PropertyDoc extends BaseDoc
{
24 25 26 27 28 29 30 31 32 33
    public $visibility;
    public $isStatic;
    public $type;
    public $types;
    public $defaultValue;
    // will be set by creating class
    public $getter;
    public $setter;
    // will be set by creating class
    public $definedBy;
34

35

36 37 38
    /**
     * @return bool if property is read only
     */
39 40 41 42
    public function getIsReadOnly()
    {
        return $this->getter !== null && $this->setter === null;
    }
43

44 45 46
    /**
     * @return bool if property is write only
     */
47 48 49 50
    public function getIsWriteOnly()
    {
        return $this->getter === null && $this->setter !== null;
    }
51

52 53
    /**
     * @param \phpDocumentor\Reflection\ClassReflector\PropertyReflector $reflector
54 55
     * @param Context $context
     * @param array $config
56 57 58 59
     */
    public function __construct($reflector = null, $context = null, $config = [])
    {
        parent::__construct($reflector, $context, $config);
60

61 62 63
        if ($reflector === null) {
            return;
        }
64

65 66
        $this->visibility = $reflector->getVisibility();
        $this->isStatic = $reflector->isStatic();
67

68 69 70 71
        // bypass $reflector->getDefault() for short array syntax
        if ($reflector->getNode()->default) {
            $this->defaultValue = PrettyPrinter::getRepresentationOfValue($reflector->getNode()->default);
        }
72

73
        $hasInheritdoc = false;
74
        foreach ($this->tags as $tag) {
75 76 77
            if ($tag->getName() === 'inheritdoc') {
                $hasInheritdoc = true;
            }
78 79 80 81
            if ($tag instanceof VarTag) {
                $this->type = $tag->getType();
                $this->types = $tag->getTypes();
                $this->description = ucfirst($tag->getDescription());
82
                $this->shortDescription = BaseDoc::extractFirstSentence($this->description);
83 84
            }
        }
85
        if (empty($this->shortDescription) && $context !== null && !$hasInheritdoc) {
86 87 88 89 90 91 92
            $context->errors[] = [
                'line' => $this->startLine,
                'file' => $this->sourceFile,
                'message' => "No short description for element '{$this->name}'",
            ];
        }
    }
AlexGx committed
93
}