Completed
Pull Request — master (#87)
by Marc
15:48 queued 09:49
created

EnumSet32Bench   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 226
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 44
lcom 1
cbo 2
dl 226
loc 226
rs 8.3396
c 0
b 0
f 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A initClass() 10 10 1
A init() 8 8 2
A benchAttachEnumeratorOnEmpty() 6 6 2
A benchAttachValueOnEmpty() 6 6 2
A benchAttachEnumeratorOnFull() 6 6 2
A benchAttachValueOnFull() 6 6 2
A benchDetachEnumeratorOnEmpty() 6 6 2
A benchDetachValueOnEmpty() 6 6 2
A benchDetachEnumeratorOnFull() 6 6 2
A benchDetachValueOnFull() 6 6 2
A benchContainsEnumeratorTrue() 6 6 2
A benchContainsValueTrue() 6 6 2
A benchContainsEnumeratorFalse() 6 6 2
A benchContainsValueFalse() 6 6 2
A benchIterateFull() 6 6 2
A benchIterateEmpty() 6 6 2
A benchCountFull() 4 4 1
A benchCountEmpty() 4 4 1
A benchIsEqual() 4 4 1
A benchIsSubset() 4 4 1
A benchIsSuperset() 4 4 1
A benchUnion() 4 4 1
A benchIntersect() 4 4 1
A benchDiff() 4 4 1
A benchSymDiff() 4 4 1
A benchGetOrdinals() 4 4 1
A benchGetValues() 4 4 1
A benchGetNames() 4 4 1
A benchGetEnumerators() 4 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like EnumSet32Bench often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EnumSet32Bench, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace MabeEnumBench;
4
5
use MabeEnum\EnumSet;
6
use MabeEnumTest\TestAsset\Enum32;
7
8
/**
9
 * Benchmark of EnumSet used with an enumeration of 32 enumerators.
10
 * (The internal bitset could be both an integer and a binary string)
11
 *
12
 * @BeforeClassMethods({"initClass"})
13
 * @BeforeMethods({"init"})
14
 * @Revs(2000)
15
 * @Iterations(25)
16
 */
17 View Code Duplication
class EnumSet32Bench
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    /**
20
     * @var array
21
     */
22
    private static $constants;
23
24
    /**
25
     * @var string[]
26
     */
27
    private static $names;
28
29
    /**
30
     * @var mixed[]
31
     */
32
    private static $values;
33
34
    /**
35
     * @var int[]
36
     */
37
    private static $ordinals;
38
39
    /**
40
     * @var Enum32[]
41
     */
42
    private static $enumerators;
43
44
    /**
45
     * @var EnumSet
46
     */
47
    private $emptySet;
48
49
    /**
50
     * @var EnumSet
51
     */
52
    private $fullSet;
53
54
    /**
55
     * Initialize class
56
     */
57
    public static function initClass()
58
    {
59
        // initialize the enum to make sure the initialization process
60
        // will not effect the benchmark
61
        self::$constants   = Enum32::getConstants();
62
        self::$names       = Enum32::getNames();
0 ignored issues
show
Documentation Bug introduced by
It seems like \MabeEnumTest\TestAsset\Enum32::getNames() of type array<integer,integer|string> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
        self::$values      = Enum32::getValues();
64
        self::$ordinals    = Enum32::getOrdinals();
65
        self::$enumerators = Enum32::getEnumerators();
0 ignored issues
show
Documentation Bug introduced by
It seems like \MabeEnumTest\TestAsset\Enum32::getEnumerators() of type array<integer,object<MabeEnum\Enum>> is incompatible with the declared type array<integer,object<Mab...Test\TestAsset\Enum32>> of property $enumerators.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
    }
67
68
    /**
69
     * Initialize object
70
     */
71
    public function init()
72
    {
73
        $this->emptySet = new EnumSet(Enum32::class);
74
        $this->fullSet = new EnumSet(Enum32::class);
75
        foreach (self::$enumerators as $enumerator) {
76
            $this->fullSet->attach($enumerator);
77
        }
78
    }
79
80
    public function benchAttachEnumeratorOnEmpty()
81
    {
82
        foreach (self::$enumerators as $enumerator) {
83
            $this->emptySet->attach($enumerator);
84
        }
85
    }
86
87
    public function benchAttachValueOnEmpty()
88
    {
89
        foreach (self::$values as $value) {
90
            $this->emptySet->attach($value);
91
        }
92
    }
93
94
    public function benchAttachEnumeratorOnFull()
95
    {
96
        foreach (self::$enumerators as $enumerator) {
97
            $this->fullSet->attach($enumerator);
98
        }
99
    }
100
101
    public function benchAttachValueOnFull()
102
    {
103
        foreach (self::$values as $value) {
104
            $this->fullSet->attach($value);
105
        }
106
    }
107
108
    public function benchDetachEnumeratorOnEmpty()
109
    {
110
        foreach (self::$enumerators as $enumerator) {
111
            $this->emptySet->detach($enumerator);
112
        }
113
    }
114
115
    public function benchDetachValueOnEmpty()
116
    {
117
        foreach (self::$values as $value) {
118
            $this->emptySet->detach($value);
119
        }
120
    }
121
122
    public function benchDetachEnumeratorOnFull()
123
    {
124
        foreach (self::$enumerators as $enumerator) {
125
            $this->fullSet->detach($enumerator);
126
        }
127
    }
128
129
    public function benchDetachValueOnFull()
130
    {
131
        foreach (self::$values as $value) {
132
            $this->fullSet->detach($value);
133
        }
134
    }
135
136
    public function benchContainsEnumeratorTrue()
137
    {
138
        foreach (self::$enumerators as $enumerator) {
139
            $this->fullSet->contains($enumerator);
140
        }
141
    }
142
143
    public function benchContainsValueTrue()
144
    {
145
        foreach (self::$values as $value) {
146
            $this->fullSet->contains($value);
147
        }
148
    }
149
150
    public function benchContainsEnumeratorFalse()
151
    {
152
        foreach (self::$enumerators as $enumerator) {
153
            $this->fullSet->contains($enumerator);
154
        }
155
    }
156
157
    public function benchContainsValueFalse()
158
    {
159
        foreach (self::$values as $value) {
160
            $this->fullSet->contains($value);
161
        }
162
    }
163
164
    public function benchIterateFull()
165
    {
166
        foreach ($this->fullSet as $enumerator) {
167
            $enumerator->key();
168
        }
169
    }
170
171
    public function benchIterateEmpty()
172
    {
173
        foreach ($this->emptySet as $enumerator) {
174
            $enumerator->key();
175
        }
176
    }
177
178
    public function benchCountFull()
179
    {
180
        $this->fullSet->count();
181
    }
182
183
    public function benchCountEmpty()
184
    {
185
        $this->emptySet->count();
186
    }
187
188
    public function benchIsEqual()
189
    {
190
        $this->fullSet->isEqual($this->fullSet);
0 ignored issues
show
Unused Code introduced by
The call to the method MabeEnum\EnumSet::isEqual() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
191
    }
192
193
    public function benchIsSubset()
194
    {
195
        $this->fullSet->isEqual($this->fullSet);
0 ignored issues
show
Unused Code introduced by
The call to the method MabeEnum\EnumSet::isEqual() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
196
    }
197
198
    public function benchIsSuperset()
199
    {
200
        $this->fullSet->isSuperset($this->fullSet);
0 ignored issues
show
Unused Code introduced by
The call to the method MabeEnum\EnumSet::isSuperset() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
201
    }
202
203
    public function benchUnion()
204
    {
205
        $this->fullSet->union($this->emptySet);
206
    }
207
208
    public function benchIntersect()
209
    {
210
        $this->fullSet->intersect($this->emptySet);
211
    }
212
213
    public function benchDiff()
214
    {
215
        $this->fullSet->diff($this->emptySet);
216
    }
217
218
    public function benchSymDiff()
219
    {
220
        $this->fullSet->symDiff($this->emptySet);
221
    }
222
223
    public function benchGetOrdinals()
224
    {
225
        $this->fullSet->getOrdinals();
226
    }
227
228
    public function benchGetValues()
229
    {
230
        $this->fullSet->getValues();
231
    }
232
233
    public function benchGetNames()
234
    {
235
        $this->fullSet->getNames();
236
    }
237
238
    public function benchGetEnumerators()
239
    {
240
        $this->fullSet->getEnumerators();
241
    }
242
}
243