Completed
Push — master ( a38f02...4631a3 )
by Peter
02:48
created

ClassChecker::exists()   C

Complexity

Conditions 9
Paths 42

Size

Total Lines 46
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.5338

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 13
cts 16
cp 0.8125
rs 5.0942
c 0
b 0
f 0
cc 9
eloc 19
nc 42
nop 1
crap 9.5338
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 http://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 Exception;
18
19
/**
20
 * ClassChecker
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class ClassChecker
25
{
26
27
	/**
28
	 * Array with existent classes
29
	 * @var string[]
30
	 */
31
	private static $_exists = [];
32
33
	/**
34
	 * Check whenever class or trait or interface exists.
35
	 * It does autoload if needed.
36
	 * @param string $class
37
	 * @return bool True if class or trait or interface exists
38
	 */
39 58
	public static function exists($class)
40
	{
41 58
		if (Blacklister::ignores($class))
42
		{
43 2
			return false;
44
		}
45 58
		if (self::isConfirmed($class))
46
		{
47 58
			return true;
48
		}
49
		try
50
		{
51 22
			if (class_exists($class))
52
			{
53 22
				return self::confirm($class);
54
			}
55
		}
56
		catch (Exception $ex)
57
		{
58
			// Some class loaders throw exception if class not found
59
		}
60
		try
61
		{
62 15
			if (trait_exists($class))
63
			{
64 15
				return self::confirm($class);
65
			}
66
		}
67
		catch (Exception $ex)
68
		{
69
			// Some class loaders throw exception if class not found
70
		}
71
		try
72
		{
73 15
			if (interface_exists($class))
74
			{
75 15
				return self::confirm($class);
76
			}
77
		}
78
		catch (Exception $ex)
79
		{
80
			// Some class loaders throw exception if class not found
81
		}
82 14
		Blacklister::ignore($class);
83 14
		return false;
84
	}
85
86 58
	private static function isConfirmed($class)
87
	{
88 58
		return isset(self::$_exists[$class]);
89
	}
90
91 20
	private static function confirm($class)
92
	{
93 20
		self::$_exists[$class] = true;
94 20
		return true;
95
	}
96
97
}
98