Completed
Push — feature/bench ( a60088 )
by Marc
05:26 queued 01:24
created

EnumSet32Bench   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 215
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 50
lcom 1
cbo 2
dl 215
loc 215
rs 8.6206
c 0
b 0
f 0

32 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
A benchGetBit() 6 6 2
A benchSetBit() 6 6 2
A benchUnsetBit() 6 6 2

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
 * @BeforeClassMethods({"initClass"})
10
 * @BeforeMethods({"init"})
11
 * @Revs(1000)
12
 * @Iterations(20)
13
 */
14 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...
15
{
16
    private static $constants;
17
    private static $names;
18
    private static $values;
19
    private static $ordinals;
20
    private static $enumerators;
21
22
    private $emptySet;
23
    private $fullSet;
24
25
    public static function initClass()
26
    {
27
        // initialize the enum to make sure the initialization process
28
        // will not effect the benchmark
29
        self::$constants   = Enum32::getConstants();
30
        self::$names       = Enum32::getNames();
31
        self::$values      = Enum32::getValues();
32
        self::$ordinals    = Enum32::getOrdinals();
33
        self::$enumerators = Enum32::getEnumerators();
34
    }
35
36
    public function init()
37
    {
38
        $this->emptySet = new EnumSet(Enum32::class);
39
        $this->fullSet = new EnumSet(Enum32::class);
40
        foreach (self::$enumerators as $enumerator) {
41
            $this->fullSet->attach($enumerator);
42
        }
43
    }
44
45
    public function benchAttachEnumeratorOnEmpty()
46
    {
47
        foreach (self::$enumerators as $enumerator) {
48
            $this->emptySet->attach($enumerator);
49
        }
50
    }
51
52
    public function benchAttachValueOnEmpty()
53
    {
54
        foreach (self::$values as $value) {
55
            $this->emptySet->attach($value);
56
        }
57
    }
58
59
    public function benchAttachEnumeratorOnFull()
60
    {
61
        foreach (self::$enumerators as $enumerator) {
62
            $this->fullSet->attach($enumerator);
63
        }
64
    }
65
66
    public function benchAttachValueOnFull()
67
    {
68
        foreach (self::$values as $value) {
69
            $this->fullSet->attach($value);
70
        }
71
    }
72
73
    public function benchDetachEnumeratorOnEmpty()
74
    {
75
        foreach (self::$enumerators as $enumerator) {
76
            $this->emptySet->detach($enumerator);
77
        }
78
    }
79
80
    public function benchDetachValueOnEmpty()
81
    {
82
        foreach (self::$values as $value) {
83
            $this->emptySet->detach($value);
84
        }
85
    }
86
87
    public function benchDetachEnumeratorOnFull()
88
    {
89
        foreach (self::$enumerators as $enumerator) {
90
            $this->fullSet->detach($enumerator);
91
        }
92
    }
93
94
    public function benchDetachValueOnFull()
95
    {
96
        foreach (self::$values as $value) {
97
            $this->fullSet->detach($value);
98
        }
99
    }
100
101
    public function benchContainsEnumeratorTrue()
102
    {
103
        foreach (self::$enumerators as $enumerator) {
104
            $this->fullSet->contains($enumerator);
105
        }
106
    }
107
108
    public function benchContainsValueTrue()
109
    {
110
        foreach (self::$values as $value) {
111
            $this->fullSet->contains($value);
112
        }
113
    }
114
115
    public function benchContainsEnumeratorFalse()
116
    {
117
        foreach (self::$enumerators as $enumerator) {
118
            $this->fullSet->contains($enumerator);
119
        }
120
    }
121
122
    public function benchContainsValueFalse()
123
    {
124
        foreach (self::$values as $value) {
125
            $this->fullSet->contains($value);
126
        }
127
    }
128
129
    public function benchIterateFull()
130
    {
131
        foreach ($this->fullSet as $enumerator) {
132
            $enumerator->key();
133
        }
134
    }
135
136
    public function benchIterateEmpty()
137
    {
138
        foreach ($this->emptySet as $enumerator) {
139
            $enumerator->key();
140
        }
141
    }
142
143
    public function benchCountFull()
144
    {
145
        $this->fullSet->count();
146
    }
147
148
    public function benchCountEmpty()
149
    {
150
        $this->emptySet->count();
151
    }
152
153
    public function benchIsEqual()
154
    {
155
        $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...
156
    }
157
158
    public function benchIsSubset()
159
    {
160
        $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...
161
    }
162
163
    public function benchIsSuperset()
164
    {
165
        $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...
166
    }
167
168
    public function benchUnion()
169
    {
170
        $this->fullSet->union($this->emptySet);
171
    }
172
173
    public function benchIntersect()
174
    {
175
        $this->fullSet->intersect($this->emptySet);
176
    }
177
178
    public function benchDiff()
179
    {
180
        $this->fullSet->diff($this->emptySet);
181
    }
182
183
    public function benchSymDiff()
184
    {
185
        $this->fullSet->symDiff($this->emptySet);
186
    }
187
188
    public function benchGetOrdinals()
189
    {
190
        $this->fullSet->getOrdinals();
191
    }
192
193
    public function benchGetValues()
194
    {
195
        $this->fullSet->getValues();
196
    }
197
198
    public function benchGetNames()
199
    {
200
        $this->fullSet->getNames();
201
    }
202
203
    public function benchGetEnumerators()
204
    {
205
        $this->fullSet->getEnumerators();
206
    }
207
208
    public function benchGetBit()
209
    {
210
        foreach (self::$ordinals as $ordinal) {
211
            $this->fullSet->getBit($ordinal);
212
        }
213
    }
214
215
    public function benchSetBit()
216
    {
217
        foreach (self::$ordinals as $ordinal) {
218
            $this->emptySet->setBit($ordinal, true);
219
        }
220
    }
221
222
    public function benchUnsetBit()
223
    {
224
        foreach (self::$ordinals as $ordinal) {
225
            $this->fullSet->setBit($ordinal, false);
226
        }
227
    }
228
}
229