Completed
Push — master ( 702c50...e6039a )
by Peter
04:37
created

ClassChecker::confirm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 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 is anonymous.
35
	 * @param string $class
36
	 * @return bool True if class is anonymous
37
	 */
38 71
	public static function isAnonymous($class)
39
	{
40 71
		return strpos($class, 'class@anonymous') !== false;
41
	}
42
43
	/**
44
	 * Check whenever class or trait or interface exists.
45
	 * It does autoload if needed.
46
	 * @param string $class
47
	 * @return bool True if class or trait or interface exists
48
	 */
49 69
	public static function exists($class)
50
	{
51 69
		if(is_object($class))
52
		{
53
			$class = get_class($class);
54
		}
55 69
		if (Blacklister::ignores($class))
56
		{
57 9
			return false;
58
		}
59 69
		if (self::isConfirmed($class))
60
		{
61 69
			return true;
62
		}
63
		try
64
		{
65 30
			if (@class_exists($class))
66
			{
67 30
				return self::confirm($class);
68
			}
69
		}
70
		catch (Exception $ex)
71
		{
72
			// Some class loaders throw exception if class not found
73
		}
74
		try
75
		{
76 23
			if (@trait_exists($class))
77
			{
78 23
				return self::confirm($class);
79
			}
80
		}
81
		catch (Exception $ex)
82
		{
83
			// Some class loaders throw exception if class not found
84
		}
85
		try
86
		{
87 23
			if (@interface_exists($class))
88
			{
89 23
				return self::confirm($class);
90
			}
91
		}
92
		catch (Exception $ex)
93
		{
94
			// Some class loaders throw exception if class not found
95
		}
96 22
		Blacklister::ignore($class);
97 22
		return false;
98
	}
99
100 69
	private static function isConfirmed($class)
101
	{
102 69
		return isset(self::$_exists[$class]);
103
	}
104
105 27
	private static function confirm($class)
106
	{
107 27
		self::$_exists[$class] = true;
108 27
		return true;
109
	}
110
111
}
112