ClassChecker::getPartials()   B
last analyzed

Complexity

Conditions 9
Paths 26

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 9.0066

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 22
cts 23
cp 0.9565
rs 7.4917
c 0
b 0
f 0
cc 9
nc 26
nop 1
crap 9.0066

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link https://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Utilities;
16
17
use function array_key_exists;
18
use function array_unique;
19
use Exception;
20
use function get_class;
21
use function get_parent_class;
22
use function is_object;
23
use ReflectionClass;
24
use function sort;
25
26
/**
27
 * ClassChecker
28
 *
29
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
30
 */
31
class ClassChecker
32
{
33
34
	/**
35
	 * Array with existent classes
36
	 * @var string[]
37
	 */
38
	private static $_exists = [];
39
40
	/**
41
	 * Partials cache
42
	 * @var array
43
	 */
44
	private static $partials = [];
45
46
	/**
47
	 * Check whenever class is anonymous.
48
	 * @param string|object $class
49
	 * @return bool True if class is anonymous
50
	 */
51 74
	public static function isAnonymous($class)
52
	{
53 74
		if(is_object($class))
54
		{
55
			$class = get_class($class);
56
		}
57 74
		return strpos($class, 'class@anonymous') !== false;
58
	}
59
60
	/**
61
	 * Check whenever class or trait or interface exists.
62
	 * It does autoload if needed.
63
	 * @param string $class
64
	 * @return bool True if class or trait or interface exists
65
	 */
66 72
	public static function exists($class)
67
	{
68 72
		if(is_object($class))
69
		{
70
			$class = get_class($class);
71
		}
72 72
		if (Blacklister::ignores($class))
73
		{
74 9
			return false;
75
		}
76 72
		if (self::isConfirmed($class))
77
		{
78 71
			return true;
79
		}
80
		try
81
		{
82 60
			if (@class_exists($class))
83
			{
84 60
				return self::confirm($class);
85
			}
86
		}
87
		catch (Exception $ex)
88
		{
89
			// Some class loaders throw exception if class not found
90
		}
91
		try
92
		{
93 56
			if (@trait_exists($class))
94
			{
95 56
				return self::confirm($class);
96
			}
97
		}
98
		catch (Exception $ex)
99
		{
100
			// Some class loaders throw exception if class not found
101
		}
102
		try
103
		{
104 55
			if (@interface_exists($class))
105
			{
106 55
				return self::confirm($class);
107
			}
108
		}
109
		catch (Exception $ex)
110
		{
111
			// Some class loaders throw exception if class not found
112
		}
113 55
		Blacklister::ignore($class);
114 55
		return false;
115
	}
116
117
	/**
118
	 * Get class/interface/trait names from which class is composed.
119
	 *
120
	 * @param string $className
121
	 * @return array
122
	 */
123 34
	public static function getPartials($className)
124
	{
125 34
		if (array_key_exists($className, self::$partials))
126
		{
127
			return self::$partials[$className];
128
		}
129 34
		if(!ClassChecker::exists($className))
130
		{
131 1
			self::$partials[$className] = [];
132 1
			return [];
133
		}
134 33
		$partials = [];
135
		// Iterate over traits
136 33
		foreach ((new ReflectionClass($className))->getTraitNames() as $trait)
137
		{
138 9
			$partials[] = $trait;
139 9
			foreach(self::getPartials($trait) as $traitPart)
140
			{
141 9
				$partials[] = $traitPart;
142
			}
143
		}
144
145
		// Iterate over interfaces to get partials
146 33
		foreach ((new ReflectionClass($className))->getInterfaceNames() as $interface)
147
		{
148 33
			$partials[] = $interface;
149
		}
150
151
		// Iterate over parent classes
152
		do
153
		{
154 33
			$partials[] = $className;
155
156
			// Iterate over traits of parent class
157 33
			foreach ((new ReflectionClass($className))->getTraitNames() as $trait)
158
			{
159 9
				$partials[] = $trait;
160
			}
161
162
			// Iterate over interfaces of parent class
163 33
			foreach ((new ReflectionClass($className))->getInterfaceNames() as $interface)
164
			{
165 33
				$partials[] = $interface;
166
			}
167
168
		}
169 33
		while (($className = get_parent_class($className)) !== false);
170 33
		$partials = array_unique($partials);
171 33
		sort($partials);
172 33
		self::$partials[$className] = $partials;
173 33
		return $partials;
174
	}
175
176 72
	private static function isConfirmed($class)
177
	{
178 72
		return isset(self::$_exists[$class]);
179
	}
180
181 39
	private static function confirm($class)
182
	{
183 39
		self::$_exists[$class] = true;
184 39
		return true;
185
	}
186
187
}
188