Completed
Push — master ( 22b422...fbd4ac )
by Peter
05:54
created

ClassChecker::isConfirmed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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 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 55
	public static function exists($class)
40
	{
41 55
		if (Blacklister::ignores($class))
42 55
		{
43 5
			return false;
44
		}
45 55
		if (self::isConfirmed($class))
46 55
		{
47 55
			return true;
48
		}
49
		try
50
		{
51 21
			if (class_exists($class))
52 21
			{
53 18
				return self::confirm($class);
54
			}
55
		}
56 15
		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 15
			{
64
				return self::confirm($class);
65
			}
66
		}
67 15
		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 15
			{
75 2
				return self::confirm($class);
76
			}
77
		}
78 14
		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 55
	private static function isConfirmed($class)
87
	{
88 55
		return isset(self::$_exists[$class]);
89
	}
90
91 18
	private static function confirm($class)
92
	{
93 18
		self::$_exists[$class] = true;
94 18
		return true;
95
	}
96
97
}
98