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

ClassChecker   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.15%

Importance

Changes 5
Bugs 3 Features 0
Metric Value
wmc 11
c 5
b 3
f 0
lcom 1
cbo 1
dl 0
loc 74
ccs 25
cts 26
cp 0.9615
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isConfirmed() 0 4 1
C exists() 0 46 9
A confirm() 0 5 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