TypeDoc.php 5.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\apidoc\models;

use phpDocumentor\Reflection\DocBlock\Tag\AuthorTag;
11
use yii\helpers\StringHelper;
12

13
/**
14
 * Base class for API documentation information for classes, interfaces and traits.
15
 *
16 17 18 19 20 21 22
 * @property MethodDoc[] $nativeMethods This property is read-only.
 * @property PropertyDoc[] $nativeProperties This property is read-only.
 * @property MethodDoc[] $protectedMethods This property is read-only.
 * @property PropertyDoc[] $protectedProperties This property is read-only.
 * @property MethodDoc[] $publicMethods This property is read-only.
 * @property PropertyDoc[] $publicProperties This property is read-only.
 *
23 24 25
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
26 27
class TypeDoc extends BaseDoc
{
28 29 30 31 32 33 34 35 36 37 38
    public $authors = [];
    /**
     * @var MethodDoc[]
     */
    public $methods = [];
    /**
     * @var PropertyDoc[]
     */
    public $properties = [];
    public $namespace;

39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    /**
     * Finds subject (method or property) by name
     *
     * If there is a property with the same as a method, the method will be returned if the name is not stated
     * explicitly by prefixing with `$`.
     *
     * Example for method `attributes()` and property `$attributes` which both may exist:
     *
     * - `$subjectName = '$attributes'` finds a property or nothing.
     * - `$subjectName = 'attributes()'` finds a method or nothing.
     * - `$subjectName = 'attributes'` finds the method if it exists, if not it will find the property.
     *
     * @param $subjectName
     * @return null|MethodDoc|PropertyDoc
     */
55 56 57 58 59 60 61 62 63
    public function findSubject($subjectName)
    {
        if ($subjectName[0] != '$') {
            foreach ($this->methods as $name => $method) {
                if (rtrim($subjectName, '()') == $name) {
                    return $method;
                }
            }
        }
64
        if (!empty($subjectName) && substr_compare($subjectName, '()', -2, 2) === 0) {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            return null;
        }
        if ($this->properties === null) {
            return null;
        }
        foreach ($this->properties as $name => $property) {
            if (ltrim($subjectName, '$') == ltrim($name, '$')) {
                return $property;
            }
        }

        return null;
    }

    /**
     * @return MethodDoc[]
     */
    public function getNativeMethods()
    {
        return $this->getFilteredMethods(null, $this->name);
    }

    /**
     * @return MethodDoc[]
     */
    public function getPublicMethods()
    {
        return $this->getFilteredMethods('public');
    }

    /**
     * @return MethodDoc[]
     */
    public function getProtectedMethods()
    {
        return $this->getFilteredMethods('protected');
    }

    /**
Carsten Brandt committed
104 105
     * @param string|null $visibility
     * @param string|null $definedBy
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
     * @return MethodDoc[]
     */
    private function getFilteredMethods($visibility = null, $definedBy = null)
    {
        $methods = [];
        foreach ($this->methods as $name => $method) {
            if ($visibility !== null && $method->visibility != $visibility) {
                continue;
            }
            if ($definedBy !== null && $method->definedBy != $definedBy) {
                continue;
            }
            $methods[$name] = $method;
        }

        return $methods;
    }

    /**
     * @return PropertyDoc[]
     */
    public function getNativeProperties()
    {
        return $this->getFilteredProperties(null, $this->name);
    }

    /**
     * @return PropertyDoc[]
     */
    public function getPublicProperties()
    {
        return $this->getFilteredProperties('public');
    }

    /**
     * @return PropertyDoc[]
     */
    public function getProtectedProperties()
    {
        return $this->getFilteredProperties('protected');
    }

    /**
149 150
     * @param null $visibility
     * @param null $definedBy
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
     * @return PropertyDoc[]
     */
    private function getFilteredProperties($visibility = null, $definedBy = null)
    {
        if ($this->properties === null) {
            return [];
        }
        $properties = [];
        foreach ($this->properties as $name => $property) {
            if ($visibility !== null && $property->visibility != $visibility) {
                continue;
            }
            if ($definedBy !== null && $property->definedBy != $definedBy) {
                continue;
            }
            $properties[$name] = $property;
        }

        return $properties;
    }

    /**
     * @param \phpDocumentor\Reflection\InterfaceReflector $reflector
174 175
     * @param Context $context
     * @param array $config
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
     */
    public function __construct($reflector = null, $context = null, $config = [])
    {
        parent::__construct($reflector, $context, $config);

        $this->namespace = trim(StringHelper::dirname($this->name), '\\');

        if ($reflector === null) {
            return;
        }

        foreach ($this->tags as $i => $tag) {
            if ($tag instanceof AuthorTag) {
                $this->authors[$tag->getAuthorName()] = $tag->getAuthorEmail();
                unset($this->tags[$i]);
            }
        }

        foreach ($reflector->getProperties() as $propertyReflector) {
            if ($propertyReflector->getVisibility() != 'private') {
                $property = new PropertyDoc($propertyReflector, $context, ['sourceFile' => $this->sourceFile]);
                $property->definedBy = $this->name;
                $this->properties[$property->name] = $property;
            }
        }

        foreach ($reflector->getMethods() as $methodReflector) {
            if ($methodReflector->getVisibility() != 'private') {
                $method = new MethodDoc($methodReflector, $context, ['sourceFile' => $this->sourceFile]);
                $method->definedBy = $this->name;
                $this->methods[$method->name] = $method;
            }
        }
    }
AlexGx committed
210
}