Passed
Push — master ( 86fa88...ed1da9 )
by teng
01:20
created

ClassInspector::getTraits()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 3
nop 2
dl 0
loc 25
rs 9.8333
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright (C) 2019 pokerapp.cn
4
 * @license   https://pokerapp.cn/license
5
 */
6
namespace poker\container;
7
8
use function array_pop;
9
use function class_uses;
10
use function get_parent_class;
11
12
/**
13
 * 类检查器
14
 *
15
 * @author Levine <[email protected]>
16
 */
17
class ClassInspector
18
{
19
	/**
20
	 * 返回一个类使用的所有trait的数组
21
	 *
22
	 * @param  string|object $class    类名或类实例
23
	 * @param  bool          $autoload 是否自动加载
24
	 * @return array
25
	 */
26
	public static function getTraits($class, bool $autoload = true): array
27
	{
28
		// 获取类及其父类使用的所有trial
29
		$traits = [];
30
		do {
31
			$traits += class_uses($class, $autoload);
32
		} while($class = get_parent_class($class));
33
34
		// 寻找所有的trait
35
		$search   = $traits;
36
		$searched = [];
37
		while (!empty($search)) {
38
			$trait = array_pop($search);
39
40
			if (isset($searched[$trait])) {
41
				continue;
42
			}
43
44
			$traits += $search += class_uses($trait, $autoload);
45
46
			$searched[$trait] = $trait;
47
		}
48
49
		// 返回该类使用的trait的完整列表
50
		return $traits;
51
	}
52
}
53