Completed
Push — master ( 4c2930...4201b5 )
by Peter
05:41
created

ClassChecker::exists()   C

Complexity

Conditions 9
Paths 42

Size

Total Lines 46
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10.2655

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 12
cts 16
cp 0.75
rs 5.0942
c 0
b 0
f 0
cc 9
eloc 19
nc 42
nop 1
crap 10.2655
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 57
	public static function exists($class)
40
	{
41 57
		if (Blacklister::ignores($class))
42
		{
43 5
			return false;
44
		}
45 57
		if (self::isConfirmed($class))
46
		{
47 57
			return true;
48
		}
49
		try
50
		{
51 50
			if (class_exists($class))
52
			{
53 19
				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 47
			if (trait_exists($class))
63
			{
64
				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 47
			if (interface_exists($class))
74
			{
75 2
				return self::confirm($class);
76
			}
77
		}
78
		catch (Exception $ex)
79
		{
80
			// Some class loaders throw exception if class not found
81
		}
82 47
		Blacklister::ignore($class);
83 47
		return false;
84
	}
85
86 57
	private static function isConfirmed($class)
87
	{
88 57
		return isset(self::$_exists[$class]);
89
	}
90
91 19
	private static function confirm($class)
92
	{
93 19
		self::$_exists[$class] = true;
94 19
		return true;
95
	}
96
97
}
98