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

8
namespace yii\apidoc\models;
9

10
use phpDocumentor\Reflection\FileReflector;
11 12
use yii\base\Component;

13 14 15 16 17
/**
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
18 19
class Context extends Component
{
20 21 22
	/**
	 * @var array list of php files that have been added to this context.
	 */
23 24 25 26 27 28 29 30 31 32 33 34 35 36
	public $files = [];
	/**
	 * @var ClassDoc[]
	 */
	public $classes = [];
	/**
	 * @var InterfaceDoc[]
	 */
	public $interfaces = [];
	/**
	 * @var TraitDoc[]
	 */
	public $traits = [];

37 38
	public $errors = [];

39

40 41
	public function getType($type)
	{
42
		$type = ltrim($type, '\\');
43 44 45 46 47 48 49 50 51 52
		if (isset($this->classes[$type])) {
			return $this->classes[$type];
		} elseif (isset($this->interfaces[$type])) {
			return $this->interfaces[$type];
		} elseif (isset($this->traits[$type])) {
			return $this->traits[$type];
		}
		return null;
	}

53 54
	public function addFile($fileName)
	{
55
		$this->files[$fileName] = sha1_file($fileName);
56 57 58 59

		$reflection = new FileReflector($fileName, true);
		$reflection->process();

AlexGx committed
60
		foreach ($reflection->getClasses() as $class) {
61
			$class = new ClassDoc($class, $this, ['sourceFile' => $fileName]);
62
			$this->classes[$class->name] = $class;
63
		}
AlexGx committed
64
		foreach ($reflection->getInterfaces() as $interface) {
65
			$interface = new InterfaceDoc($interface, $this, ['sourceFile' => $fileName]);
66
			$this->interfaces[$interface->name] = $interface;
67
		}
AlexGx committed
68
		foreach ($reflection->getTraits() as $trait) {
69
			$trait = new TraitDoc($trait, $this, ['sourceFile' => $fileName]);
70
			$this->traits[$trait->name] = $trait;
71 72 73 74 75 76
		}
	}

	public function updateReferences()
	{
		// update all subclass references
AlexGx committed
77
		foreach ($this->classes as $class) {
78 79 80 81 82 83 84
			$className = $class->name;
			while (isset($this->classes[$class->parentClass])) {
				$class = $this->classes[$class->parentClass];
				$class->subclasses[] = $className;
			}
		}
		// update interfaces of subclasses
AlexGx committed
85
		foreach ($this->classes as $class) {
86 87
			$this->updateSubclassInferfacesTraits($class);
		}
88
		// update implementedBy and usedBy for interfaces and traits
AlexGx committed
89 90
		foreach ($this->classes as $class) {
			foreach ($class->traits as $trait) {
91
				if (isset($this->traits[$trait])) {
92 93
					$trait = $this->traits[$trait];
					$trait->usedBy[] = $class->name;
94 95
					$class->properties = array_merge($trait->properties, $class->properties);
					$class->methods = array_merge($trait->methods, $class->methods);
96 97
				}
			}
AlexGx committed
98
			foreach ($class->interfaces as $interface) {
99 100 101 102
				if (isset($this->interfaces[$interface])) {
					$this->interfaces[$interface]->implementedBy[] = $class->name;
					if ($class->isAbstract) {
						// add not implemented interface methods
AlexGx committed
103
						foreach ($this->interfaces[$interface]->methods as $method) {
104 105 106 107 108 109 110
							if (!isset($class->methods[$method->name])) {
								$class->methods[$method->name] = $method;
							}
						}
					}
				}
			}
111
		}
112
		// inherit docs
113
		foreach ($this->classes as $class) {
114 115
			$this->inheritDocs($class);
		}
116
		// inherit properties, methods, contants and events to subclasses
AlexGx committed
117
		foreach ($this->classes as $class) {
118 119
			$this->updateSubclassInheritance($class);
		}
120
		// add properties from getters and setters
AlexGx committed
121
		foreach ($this->classes as $class) {
122 123 124 125
			$this->handlePropertyFeature($class);
		}

		// TODO reference exceptions to methods where they are thrown
126 127 128 129 130 131 132 133
	}

	/**
	 * Add implemented interfaces and used traits to subclasses
	 * @param ClassDoc $class
	 */
	protected function updateSubclassInferfacesTraits($class)
	{
AlexGx committed
134
		foreach ($class->subclasses as $subclass) {
135 136 137 138 139 140
			$subclass = $this->classes[$subclass];
			$subclass->interfaces = array_unique(array_merge($subclass->interfaces, $class->interfaces));
			$subclass->traits = array_unique(array_merge($subclass->traits, $class->traits));
			$this->updateSubclassInferfacesTraits($subclass);
		}
	}
141 142 143 144 145 146 147

	/**
	 * Add implemented interfaces and used traits to subclasses
	 * @param ClassDoc $class
	 */
	protected function updateSubclassInheritance($class)
	{
AlexGx committed
148
		foreach ($class->subclasses as $subclass) {
149
			$subclass = $this->classes[$subclass];
150 151 152 153
			$subclass->events = array_merge($class->events, $subclass->events);
			$subclass->constants = array_merge($class->constants, $subclass->constants);
			$subclass->properties = array_merge($class->properties, $subclass->properties);
			$subclass->methods = array_merge($class->methods, $subclass->methods);
154 155 156
			$this->updateSubclassInheritance($subclass);
		}
	}
157

158 159 160 161 162 163 164 165
	/**
	 * Inhertit docsblocks using `@inheritDoc` tag.
	 * @param ClassDoc $class
	 * @see http://phpdoc.org/docs/latest/guides/inheritance.html
	 */
	protected function inheritDocs($class)
	{
		// TODO also for properties?
Luciano Baraglia committed
166
		foreach ($class->methods as $m) {
167
			$inheritedMethod = $this->inheritMethodRecursive($m, $class);
Luciano Baraglia committed
168
			foreach (['shortDescription', 'description', 'params', 'return', 'returnType', 'returnTypes', 'exceptions'] as $property) {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
				if (empty($m->$property)) {
					$m->$property = $inheritedMethod->$property;
				}
			}
		}
	}

	/**
	 * @param MethodDoc $method
	 * @param ClassDoc $parent
	 */
	private function inheritMethodRecursive($method, $class)
	{
		if (!isset($this->classes[$class->parentClass])) {
			return $method;
		}
		$parent = $this->classes[$class->parentClass];
		foreach ($method->tags as $tag) {
187
			if (strtolower($tag->getName()) == 'inheritdoc') {
188 189 190 191 192 193 194 195 196
				if (isset($parent->methods[$method->name])) {
					$method = $parent->methods[$method->name];
				}
				return $this->inheritMethodRecursive($method, $parent);
			}
		}
		return $method;
	}

197
	/**
198
	 * Add properties for getters and setters if class is subclass of [[\yii\base\Object]].
199 200 201 202 203 204 205
	 * @param ClassDoc $class
	 */
	protected function handlePropertyFeature($class)
	{
		if (!$this->isSubclassOf($class, 'yii\base\Object')) {
			return;
		}
AlexGx committed
206
		foreach ($class->getPublicMethods() as $name => $method) {
207 208 209
			if ($method->isStatic) {
				continue;
			}
210 211 212 213 214
			if (!strncmp($name, 'get', 3) && $this->paramsOptional($method)) {
				$propertyName = '$' . lcfirst(substr($method->name, 3));
				if (isset($class->properties[$propertyName])) {
					$property = $class->properties[$propertyName];
					if ($property->getter === null && $property->setter === null) {
215 216 217 218 219
						$this->errors[] = [
							'line' => $property->startLine,
							'file' => $class->sourceFile,
							'message' => "Property $propertyName conflicts with a defined getter {$method->name} in {$class->name}.",
						];
220 221 222
					}
					$property->getter = $method;
				} else {
223
					$class->properties[$propertyName] = new PropertyDoc(null, $this, [
224 225
						'name' => $propertyName,
						'definedBy' => $class->name,
226
						'sourceFile' => $class->sourceFile,
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
						'visibility' => 'public',
						'isStatic' => false,
						'type' => $method->returnType,
						'types' => $method->returnTypes,
						'shortDescription' => (($pos = strpos($method->return, '.')) !== false) ?
								substr($method->return, 0, $pos) : $method->return,
						'description' => $method->return,
						'getter' => $method
						// TODO set default value
					]);
				}
			}
			if (!strncmp($name, 'set', 3) && $this->paramsOptional($method, 1)) {
				$propertyName = '$' . lcfirst(substr($method->name, 3));
				if (isset($class->properties[$propertyName])) {
					$property = $class->properties[$propertyName];
					if ($property->getter === null && $property->setter === null) {
244 245 246 247 248
						$this->errors[] = [
							'line' => $property->startLine,
							'file' => $class->sourceFile,
							'message' => "Property $propertyName conflicts with a defined setter {$method->name} in {$class->name}.",
						];
249 250 251 252
					}
					$property->setter = $method;
				} else {
					$param = $this->getFirstNotOptionalParameter($method);
253
					$class->properties[$propertyName] = new PropertyDoc(null, $this, [
254 255
						'name' => $propertyName,
						'definedBy' => $class->name,
256
						'sourceFile' => $class->sourceFile,
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
						'visibility' => 'public',
						'isStatic' => false,
						'type' => $param->type,
						'types' => $param->types,
						'shortDescription' => (($pos = strpos($param->description, '.')) !== false) ?
								substr($param->description, 0, $pos) : $param->description,
						'description' => $param->description,
						'setter' => $method
					]);
				}
			}
		}
	}

	/**
	 * @param MethodDoc $method
	 * @param integer $number number of not optional parameters
	 * @return bool
	 */
	private function paramsOptional($method, $number = 0)
	{
AlexGx committed
278
		foreach ($method->params as $param) {
279 280 281 282 283 284 285 286 287 288 289 290 291
			if (!$param->isOptional && $number-- <= 0) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @param MethodDoc $method
	 * @return ParamDoc
	 */
	private function getFirstNotOptionalParameter($method)
	{
AlexGx committed
292
		foreach ($method->params as $param) {
293 294 295 296 297 298 299 300 301
			if (!$param->isOptional) {
				return $param;
			}
		}
		return null;
	}

	/**
	 * @param ClassDoc $classA
Carsten Brandt committed
302 303
	 * @param ClassDoc|string $classB
	 * @return boolean
304 305 306 307 308 309 310 311 312
	 */
	protected function isSubclassOf($classA, $classB)
	{
		if (is_object($classB)) {
			$classB = $classB->name;
		}
		if ($classA->name == $classB) {
			return true;
		}
AlexGx committed
313
		while ($classA->parentClass !== null && isset($this->classes[$classA->parentClass])) {
314 315 316 317 318 319 320
			$classA = $this->classes[$classA->parentClass];
			if ($classA->name == $classB) {
				return true;
			}
		}
		return false;
	}
AlexGx committed
321
}