1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MabeEnumBench; |
4
|
|
|
|
5
|
|
|
use MabeEnumTest\TestAsset\Enum66; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @BeforeClassMethods({"initClass"}) |
9
|
|
|
* @Revs(1000) |
10
|
|
|
* @Iterations(20) |
11
|
|
|
*/ |
12
|
|
|
class EnumBench |
13
|
|
|
{ |
14
|
|
|
private static $constants; |
15
|
|
|
private static $names; |
16
|
|
|
private static $values; |
17
|
|
|
private static $ordinals; |
18
|
|
|
private static $enumerators; |
19
|
|
|
|
20
|
|
|
public static function initClass() |
21
|
|
|
{ |
22
|
|
|
// initialize the enum to make sure the initialization process |
23
|
|
|
// will not effect the benchmark |
24
|
|
|
self::$constants = Enum66::getConstants(); |
25
|
|
|
self::$names = Enum66::getNames(); |
26
|
|
|
self::$values = Enum66::getValues(); |
27
|
|
|
self::$ordinals = Enum66::getOrdinals(); |
28
|
|
|
self::$enumerators = Enum66::getEnumerators(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function benchGetName() |
32
|
|
|
{ |
33
|
|
|
foreach (self::$enumerators as $enumerator) { |
34
|
|
|
$enumerator->getName(); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function benchGetValue() |
39
|
|
|
{ |
40
|
|
|
foreach (self::$enumerators as $enumerator) { |
41
|
|
|
$enumerator->getValue(); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function benchGetOrdinal() |
46
|
|
|
{ |
47
|
|
|
foreach (self::$enumerators as $enumerator) { |
48
|
|
|
$enumerator->getOrdinal(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function benchIsByEnumerator() |
53
|
|
|
{ |
54
|
|
|
foreach (self::$enumerators as $enumerator) { |
55
|
|
|
$enumerator->is($enumerator); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function benchIsByValue() |
60
|
|
|
{ |
61
|
|
|
foreach (self::$enumerators as $enumerator) { |
62
|
|
|
$enumerator->is($enumerator->getValue()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function benchGetConstants() |
67
|
|
|
{ |
68
|
|
|
Enum66::getConstants(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function benchGetValues() |
72
|
|
|
{ |
73
|
|
|
Enum66::getValues(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function benchGetNames() |
77
|
|
|
{ |
78
|
|
|
Enum66::getNames(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function benchGetOrdinals() |
82
|
|
|
{ |
83
|
|
|
Enum66::getOrdinals(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function benchGetEnumerators() |
87
|
|
|
{ |
88
|
|
|
Enum66::getEnumerators(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function benchByValue() |
92
|
|
|
{ |
93
|
|
|
foreach (self::$values as $value) { |
94
|
|
|
Enum66::byValue($value); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function benchByName() |
99
|
|
|
{ |
100
|
|
|
foreach (self::$names as $name) { |
101
|
|
|
Enum66::byName($name); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function benchByOrdinal() |
106
|
|
|
{ |
107
|
|
|
foreach (self::$ordinals as $ord) { |
108
|
|
|
Enum66::byOrdinal($ord); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function benchGetByValues() |
113
|
|
|
{ |
114
|
|
|
foreach (self::$values as $value) { |
115
|
|
|
Enum66::get($value); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function benchGetByEnumerator() |
120
|
|
|
{ |
121
|
|
|
foreach (self::$enumerators as $enumerator) { |
122
|
|
|
Enum66::get($enumerator); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function benchGetByCallStatic() |
127
|
|
|
{ |
128
|
|
|
foreach (self::$names as $name) { |
129
|
|
|
Enum66::$name(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function benchHasByEnumerator() |
134
|
|
|
{ |
135
|
|
|
foreach (self::$enumerators as $enumerator) { |
136
|
|
|
Enum66::has($enumerator); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function benchHasByValue() |
141
|
|
|
{ |
142
|
|
|
foreach (self::$values as $value) { |
143
|
|
|
Enum66::has($value); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: