FunctionDoc.php 2 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 18 19 20
/**
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
21 22
class FunctionDoc extends BaseDoc
{
23 24 25 26 27 28 29 30 31 32 33 34 35 36
	/**
	 * @var ParamDoc[]
	 */
	public $params = [];
	public $exceptions = [];
	public $return;
	public $returnType;
	public $returnTypes;
	public $isReturnByReference;

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

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

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

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

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