PropertyDoc.php 2.07 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 16 17 18
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
19 20
class PropertyDoc extends BaseDoc
{
21
	public $visibility;
22 23 24
	public $isStatic;

	public $type;
25 26
	public $types;
	public $defaultValue;
27

28
	// will be set by creating class
29 30
	public $getter;
	public $setter;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

	// will be set by creating class
	public $definedBy;

	public function getIsReadOnly()
	{
		return $this->getter !== null && $this->setter === null;
	}

	public function getIsWriteOnly()
	{
		return $this->getter === null && $this->setter !== null;
	}

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

		if ($reflector === null) {
			return;
		}

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

Carsten Brandt committed
61 62 63 64
		// bypass $reflector->getDefault() for short array syntax
		if ($reflector->getNode()->default) {
			$this->defaultValue = PrettyPrinter::getRepresentationOfValue($reflector->getNode()->default);
		}
65

Alexander Mohorev committed
66
		foreach ($this->tags as $tag) {
67 68 69
			if ($tag instanceof VarTag) {
				$this->type = $tag->getType();
				$this->types = $tag->getTypes();
70 71
				$this->description = ucfirst($tag->getDescription());
				if (($pos = strpos($this->description, '.')) !== false) {
72
					$this->shortDescription = substr($this->description, 0, $pos + 1);
73 74 75
				} else {
					$this->shortDescription = $this->description;
				}
76 77
			}
		}
78 79 80 81 82 83 84
		if (empty($this->shortDescription) && $context !== null) {
			$context->errors[] = [
				'line' => $this->startLine,
				'file' => $this->sourceFile,
				'message' => "No short description for element '{$this->name}'",
			];
		}
85
	}
86
}