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

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

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

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

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

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