FunctionDoc.php 2.24 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 14
use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
use phpDocumentor\Reflection\DocBlock\Tag\ThrowsTag;

15
/**
16
 * Represents API documentation information for a `function`.
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
	/**
	 * @var ParamDoc[]
	 */
	public $params = [];
	public $exceptions = [];
	public $return;
	public $returnType;
	public $returnTypes;
	public $isReturnByReference;

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

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

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

		foreach($reflector->getArguments() as $arg) {
49
			$arg = new ParamDoc($arg, $context, ['sourceFile' => $this->sourceFile]);
50 51 52 53
			$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 62 63 64 65 66
				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
67
					continue;
68
				}
69
				$this->params[$paramName]->description = ucfirst($tag->getDescription());
70 71 72
				$this->params[$paramName]->type = $tag->getType();
				$this->params[$paramName]->types = $tag->getTypes();
				unset($this->tags[$i]);
73 74 75 76
			} elseif ($tag instanceof ReturnTag) {
				$this->returnType = $tag->getType();
				$this->returnTypes = $tag->getTypes();
				$this->return = $tag->getDescription();
77 78 79 80
				unset($this->tags[$i]);
			}
		}
	}
81
}