FunctionDoc.php 1.93 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
class FunctionDoc extends BaseDoc
{
18 19 20 21 22 23 24 25 26 27 28 29 30 31
	/**
	 * @var ParamDoc[]
	 */
	public $params = [];
	public $exceptions = [];
	public $return;
	public $returnType;
	public $returnTypes;
	public $isReturnByReference;

	/**
	 * @param \phpDocumentor\Reflection\FunctionReflector $reflector
	 * @param array $config
	 */
32
	public function __construct($reflector = null, $config = [])
33 34 35
	{
		parent::__construct($reflector, $config);

36 37 38 39
		if ($reflector === null) {
			return;
		}

40 41 42 43 44 45 46 47
		$this->isReturnByReference = $reflector->isByRef();

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

		foreach($this->tags as $i => $tag) {
48 49
			if ($tag instanceof ThrowsTag) {
				$this->exceptions[$tag->getType()] = $tag->getDescription();
50
				unset($this->tags[$i]);
51 52
			} elseif ($tag instanceof PropertyTag) {
				 // TODO handle property tag
53 54
			} elseif ($tag instanceof ParamTag) {
				$paramName = $tag->getVariableName();
55 56 57
				if (!isset($this->params[$paramName])) {
					echo 'undefined parameter documented: ' . $paramName . ' in ' . $this->name . "\n"; // todo add this to a log file
				}
58 59 60 61
				$this->params[$paramName]->description = $tag->getDescription();
				$this->params[$paramName]->type = $tag->getType();
				$this->params[$paramName]->types = $tag->getTypes();
				unset($this->tags[$i]);
62 63 64 65
			} elseif ($tag instanceof ReturnTag) {
				$this->returnType = $tag->getType();
				$this->returnTypes = $tag->getTypes();
				$this->return = $tag->getDescription();
66 67 68 69
				unset($this->tags[$i]);
			}
		}
	}
70
}