FunctionDoc.php 2.26 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
	/**
	 * @var ParamDoc[]
	 */
	public $params = [];
	public $exceptions = [];
	public $return;
	public $returnType;
	public $returnTypes;
	public $isReturnByReference;

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

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

47 48 49
		$this->isReturnByReference = $reflector->isByRef();

		foreach($reflector->getArguments() as $arg) {
50
			$arg = new ParamDoc($arg, $context, ['sourceFile' => $this->sourceFile]);
51 52 53 54
			$this->params[$arg->name] = $arg;
		}

		foreach($this->tags as $i => $tag) {
55 56
			if ($tag instanceof ThrowsTag) {
				$this->exceptions[$tag->getType()] = $tag->getDescription();
57
				unset($this->tags[$i]);
58
			} elseif ($tag instanceof PropertyTag) {
59
				 // ignore property tag
60 61
			} elseif ($tag instanceof ParamTag) {
				$paramName = $tag->getVariableName();
62 63 64 65 66 67
				if (!isset($this->params[$paramName]) && $context !== null) {
					$context->errors[] = [
						'line' => $this->startLine,
						'file' => $this->sourceFile,
						'message' => "Undefined parameter documented: $paramName in {$this->name}().",
					];
Carsten Brandt committed
68
					continue;
69
				}
70
				$this->params[$paramName]->description = ucfirst($tag->getDescription());
71 72 73
				$this->params[$paramName]->type = $tag->getType();
				$this->params[$paramName]->types = $tag->getTypes();
				unset($this->tags[$i]);
74 75 76 77
			} elseif ($tag instanceof ReturnTag) {
				$this->returnType = $tag->getType();
				$this->returnTypes = $tag->getTypes();
				$this->return = $tag->getDescription();
78 79 80 81
				unset($this->tags[$i]);
			}
		}
	}
82
}