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

namespace yii\apidoc\models;

10
use phpDocumentor\Reflection\DocBlock\Tag\ParamTag;
11
use phpDocumentor\Reflection\DocBlock\Tag\PropertyTag;
12 13
use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
use phpDocumentor\Reflection\DocBlock\Tag\ThrowsTag;
14
use yii\base\Exception;
15

16
/**
17
 * Represents API documentation information for a `function`.
18 19 20 21
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
22 23
class FunctionDoc extends BaseDoc
{
24 25 26 27 28 29 30 31 32 33 34 35 36 37
	/**
	 * @var ParamDoc[]
	 */
	public $params = [];
	public $exceptions = [];
	public $return;
	public $returnType;
	public $returnTypes;
	public $isReturnByReference;

	/**
	 * @param \phpDocumentor\Reflection\FunctionReflector $reflector
	 * @param array $config
	 */
38
	public function __construct($reflector = null, $config = [])
39 40 41
	{
		parent::__construct($reflector, $config);

42 43 44 45
		if ($reflector === null) {
			return;
		}

46 47 48 49 50 51 52 53
		$this->isReturnByReference = $reflector->isByRef();

		foreach($reflector->getArguments() as $arg) {
			$arg = new ParamDoc($arg);
			$this->params[$arg->name] = $arg;
		}

		foreach($this->tags as $i => $tag) {
54 55
			if ($tag instanceof ThrowsTag) {
				$this->exceptions[$tag->getType()] = $tag->getDescription();
56
				unset($this->tags[$i]);
57
			} elseif ($tag instanceof PropertyTag) {
58
				 // ignore property tag
59 60
			} elseif ($tag instanceof ParamTag) {
				$paramName = $tag->getVariableName();
61
				if (!isset($this->params[$paramName])) {
Carsten Brandt committed
62 63
					echo 'undefined parameter documented: ' . $paramName . ' in ' . $this->name . "()\n"; // TODO log these messages somewhere
					continue;
64
				}
65
				$this->params[$paramName]->description = ucfirst($tag->getDescription());
66 67 68
				$this->params[$paramName]->type = $tag->getType();
				$this->params[$paramName]->types = $tag->getTypes();
				unset($this->tags[$i]);
69 70 71 72
			} elseif ($tag instanceof ReturnTag) {
				$this->returnType = $tag->getType();
				$this->returnTypes = $tag->getTypes();
				$this->return = $tag->getDescription();
73 74 75 76
				unset($this->tags[$i]);
			}
		}
	}
77
}