Context   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 4
dl 0
loc 186
rs 9.2
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A withRegistration() 0 7 1
A withInstructor() 0 7 1
A withTeam() 0 7 1
A withContextActivities() 0 7 1
A withRevision() 0 7 1
A withPlatform() 0 7 1
A withLanguage() 0 7 1
A withStatement() 0 7 1
A withExtensions() 0 7 1
A getRegistration() 0 4 1
A getInstructor() 0 4 1
A getTeam() 0 4 1
A getContextActivities() 0 4 1
A getRevision() 0 4 1
A getPlatform() 0 4 1
A getLanguage() 0 4 1
A getStatement() 0 4 1
A getExtensions() 0 4 1
D equals() 0 56 22

How to fix   Complexity   

Complex Class

Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Model;
13
14
/**
15
 * Contextual information for an xAPI statement.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class Context
20
{
21
    private $registration;
22
    private $instructor;
23
    private $team;
24
    private $contextActivities;
25
    private $revision;
26
    private $platform;
27
    private $language;
28
    private $statement;
29
    private $extensions;
30
31
    public function withRegistration(string $registration): self
32
    {
33
        $context = clone $this;
34
        $context->registration = $registration;
35
36
        return $context;
37
    }
38
39
    public function withInstructor(Actor $instructor): self
40
    {
41
        $context = clone $this;
42
        $context->instructor = $instructor;
43
44
        return $context;
45
    }
46
47
    public function withTeam(Group $team): self
48
    {
49
        $context = clone $this;
50
        $context->team = $team;
51
52
        return $context;
53
    }
54
55
    public function withContextActivities(ContextActivities $contextActivities): self
56
    {
57
        $context = clone $this;
58
        $context->contextActivities = $contextActivities;
59
60
        return $context;
61
    }
62
63
    public function withRevision(string $revision): self
64
    {
65
        $context = clone $this;
66
        $context->revision = $revision;
67
68
        return $context;
69
    }
70
71
    public function withPlatform(string $platform): self
72
    {
73
        $context = clone $this;
74
        $context->platform = $platform;
75
76
        return $context;
77
    }
78
79
    public function withLanguage(string $language): self
80
    {
81
        $context = clone $this;
82
        $context->language = $language;
83
84
        return $context;
85
    }
86
87
    public function withStatement(StatementReference $statement): self
88
    {
89
        $context = clone $this;
90
        $context->statement = $statement;
91
92
        return $context;
93
    }
94
95
    public function withExtensions(Extensions $extensions): self
96
    {
97
        $context = clone $this;
98
        $context->extensions = $extensions;
99
100
        return $context;
101
    }
102
103
    public function getRegistration(): ?string
104
    {
105
        return $this->registration;
106
    }
107
108
    public function getInstructor(): ?Actor
109
    {
110
        return $this->instructor;
111
    }
112
113
    public function getTeam(): ?Group
114
    {
115
        return $this->team;
116
    }
117
118
    public function getContextActivities(): ?ContextActivities
119
    {
120
        return $this->contextActivities;
121
    }
122
123
    public function getRevision(): ?string
124
    {
125
        return $this->revision;
126
    }
127
128
    public function getPlatform(): ?string
129
    {
130
        return $this->platform;
131
    }
132
133
    public function getLanguage(): ?string
134
    {
135
        return $this->language;
136
    }
137
138
    public function getStatement(): ?StatementReference
139
    {
140
        return $this->statement;
141
    }
142
143
    public function getExtensions(): ?Extensions
144
    {
145
        return $this->extensions;
146
    }
147
148
    public function equals(Context $context): bool
149
    {
150
        if ($this->registration !== $context->registration) {
151
            return false;
152
        }
153
154
        if (null !== $this->instructor xor null !== $context->instructor) {
155
            return false;
156
        }
157
158
        if (null !== $this->instructor && null !== $context->instructor && !$this->instructor->equals($context->instructor)) {
159
            return false;
160
        }
161
162
        if (null !== $this->team xor null !== $context->team) {
163
            return false;
164
        }
165
166
        if (null !== $this->team && null !== $context->team && !$this->team->equals($context->team)) {
167
            return false;
168
        }
169
170
        if ($this->contextActivities != $context->contextActivities) {
171
            return false;
172
        }
173
174
        if ($this->revision !== $context->revision) {
175
            return false;
176
        }
177
178
        if ($this->platform !== $context->platform) {
179
            return false;
180
        }
181
182
        if ($this->language !== $context->language) {
183
            return false;
184
        }
185
186
        if (null !== $this->statement xor null !== $context->statement) {
187
            return false;
188
        }
189
190
        if (null !== $this->statement && null !== $context->statement && !$this->statement->equals($context->statement)) {
191
            return false;
192
        }
193
194
        if (null !== $this->extensions xor null !== $context->extensions) {
195
            return false;
196
        }
197
198
        if (null !== $this->extensions && null !== $context->extensions && !$this->extensions->equals($context->extensions)) {
199
            return false;
200
        }
201
202
        return true;
203
    }
204
}
205