1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\Util; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* To change this template, choose Tools | Templates |
7
|
|
|
* and open the template in the editor. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Description of Test. |
12
|
|
|
* |
13
|
|
|
* @author timo |
14
|
|
|
*/ |
15
|
|
|
class Test |
16
|
|
|
{ |
17
|
|
|
public static function assertClassHasConstructor($className) |
18
|
|
|
{ |
19
|
|
|
return (bool) ReflectionPool::getInstance($className)->getConstructor(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function assertRequiredConstructorParameters($className, $parameters) |
23
|
|
|
{ |
24
|
|
|
return (bool) |
25
|
|
|
!self::assertClassHasConstructor($className) || |
26
|
|
|
count($parameters) >= ReflectionPool::getInstance($className) |
27
|
|
|
->getConstructor() |
28
|
|
|
->getNumberOfRequiredParameters(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function assertInstanceOf($className, $object) |
32
|
|
|
{ |
33
|
|
|
return (bool) ReflectionPool::getInstance($className) |
34
|
|
|
->isInstance($object); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function assertClassOrInterfaceExists($className) |
38
|
|
|
{ |
39
|
|
|
return (bool) class_exists($className) || interface_exists($className); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function assertImplements($className, $interfaceName) |
43
|
|
|
{ |
44
|
|
|
$className = is_object($className) ? get_class($className) : $className; |
45
|
|
|
$res = false; //throw new \Exception(); |
|
|
|
|
46
|
|
|
try { |
47
|
|
|
class_exists($className); |
48
|
|
|
$res = ReflectionPool::getInstance($className) |
49
|
|
|
->implementsInterface($interfaceName); |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
$res = false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $res; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function assertMessage($message) |
58
|
|
|
{ |
59
|
|
|
return self::assertImplements($message, '\PEIP\INF\Message\Message'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function assertEvent($event) |
63
|
|
|
{ |
64
|
|
|
return self::assertImplements($event, '\PEIP\INF\Event\Event'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function assertEventSubject($event) |
68
|
|
|
{ |
69
|
|
|
return self::assertImplements($event, '\PEIP\INF\Event\Event') |
70
|
|
|
&& $event->getSubject(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function assertEventObjectSubject($event) |
74
|
|
|
{ |
75
|
|
|
return self::assertEventSubject($event) |
76
|
|
|
&& is_object($event->getSubject()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function assertArrayAccess($var) |
80
|
|
|
{ |
81
|
|
|
return (bool) is_array($var) || $var instanceof \ArrayAccess; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function assertHandler($var) |
85
|
|
|
{ |
86
|
|
|
return (bool) is_callable($var) || $var instanceof \PEIP\INF\Handler\Handler; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function castType($var, $type) |
90
|
|
|
{ |
91
|
|
|
switch ($type) { |
92
|
|
|
case 'string': |
93
|
|
|
$var = (string) $var; |
94
|
|
|
break; |
95
|
|
|
case 'integer': |
96
|
|
|
$var = (int) $var; |
97
|
|
|
break; |
98
|
|
|
case 'float': |
99
|
|
|
$var = (float) $var; |
100
|
|
|
break; |
101
|
|
|
case 'boolean': |
102
|
|
|
$var = (bool) $var; |
103
|
|
|
break; |
104
|
|
|
case 'object': |
105
|
|
|
$var = (object) $var; |
106
|
|
|
break; |
107
|
|
|
case 'array': |
108
|
|
|
$var = (array) $var; |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $var; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function ensureArrayAccess($var) |
116
|
|
|
{ |
117
|
|
|
if (!self::assertArrayAccess($var)) { |
118
|
|
|
throw new \InvalidArgumentException( |
119
|
|
|
'Value is not an array nor an instance of ArrayAccess' |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $var; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function ensureHandler($var) |
127
|
|
|
{ |
128
|
|
|
if (!self::assertHandler($var)) { |
129
|
|
|
throw new \InvalidArgumentException( |
130
|
|
|
'Value is not an callable nor an instance of \PEIP\INF\Handler\Handler' |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $var; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function ensureImplements($className, $interfaceName) |
138
|
|
|
{ |
139
|
|
|
if (!self::assertImplements($className, $interfaceName)) { |
140
|
|
|
throw new \InvalidArgumentException( |
141
|
|
|
'Class "'.$className.'" is not an instanceof "'.$interfaceName.'"' |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $className; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public static function assertMethod($className, $methodname) |
149
|
|
|
{ |
150
|
|
|
return ReflectionPool::getInstance($className)->hasMethod($methodname); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.