Completed
Push — master ( ef3fef...114410 )
by Christian
03:26
created

SubStatement   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 43
c 6
b 1
f 2
lcom 1
cbo 5
dl 0
loc 281
ccs 0
cts 74
cp 0
rs 8.3157

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A withActor() 0 7 1
A withVerb() 0 7 1
A withObject() 0 7 1
A withResult() 0 7 1
A withTimestamp() 0 9 1
A withCreated() 0 7 1
A withContext() 0 7 1
A withAttachments() 0 7 2
A getVerb() 0 4 1
A getActor() 0 4 1
A getObject() 0 4 1
A getResult() 0 4 1
A getTimestamp() 0 6 1
A getCreated() 0 4 1
A getContext() 0 4 1
A getAttachments() 0 4 1
A isVoidStatement() 0 4 1
C equals() 0 62 22

How to fix   Complexity   

Complex Class

Complex classes like SubStatement 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 SubStatement, 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
 * A {@link Statement} included as part of a parent Statement.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class SubStatement extends Object
20
{
21
    /**
22
     * @var Verb $verb The {@link Verb}
23
     */
24
    private $verb;
25
26
    /**
27
     * @var Actor The {@link Actor}
28
     */
29
    private $actor;
30
31
    /**
32
     * @var Object The {@link Object}
33
     */
34
    private $object;
35
36
    /**
37
     * @var Result The {@link Activity} {@link Result}
38
     */
39
    private $result;
40
41
    /**
42
     * @var \DateTime The timestamp of when the events described in this statement occurred
43
     */
44
    private $created;
45
46
    /**
47
     * @var Context The {@link Statement} {@link Context}
48
     */
49
    private $context;
50
51
    private $attachments;
52
53
    public function __construct(Actor $actor, Verb $verb, Object $object, Result $result = null, Context $context = null, \DateTime $created = null, array $attachments = null)
54
    {
55
        if ($object instanceof SubStatement) {
56
            throw new \InvalidArgumentException('Nesting sub statements is forbidden by the xAPI spec.');
57
        }
58
59
        $this->actor = $actor;
60
        $this->verb = $verb;
61
        $this->object = $object;
62
        $this->result = $result;
63
        $this->created = $created;
64
        $this->context = $context;
65
        $this->attachments = null !== $attachments ? array_values($attachments) : null;
66
    }
67
68
    public function withActor(Actor $actor)
69
    {
70
        $subStatement = clone $this;
71
        $subStatement->actor = $actor;
72
73
        return $subStatement;
74
    }
75
76
    public function withVerb(Verb $verb)
77
    {
78
        $subStatement = clone $this;
79
        $subStatement->verb = $verb;
80
81
        return $subStatement;
82
    }
83
84
    public function withObject(Object $object)
85
    {
86
        $subStatement = clone $this;
87
        $subStatement->object = $object;
88
89
        return $subStatement;
90
    }
91
92
    public function withResult(Result $result)
93
    {
94
        $subStatement = clone $this;
95
        $subStatement->result = $result;
96
97
        return $subStatement;
98
    }
99
100
    /**
101
     * @deprecated since 1.2, to be removed in 3.0
102
     */
103
    public function withTimestamp(\DateTime $timestamp = null)
104
    {
105
        @trigger_error(sprintf('The "%s()" method is deprecated since 1.2 and will be removed in 3.0. Use "%s::withCreated()" instead.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
106
107
        $statement = clone $this;
108
        $statement->created = $timestamp;
109
110
        return $statement;
111
    }
112
113
    public function withCreated(\DateTime $created = null)
114
    {
115
        $statement = clone $this;
116
        $statement->created = $created;
117
118
        return $statement;
119
    }
120
121
    public function withContext(Context $context)
122
    {
123
        $subStatement = clone $this;
124
        $subStatement->context = $context;
125
126
        return $subStatement;
127
    }
128
129
    /**
130
     * @param Attachment[]|null $attachments
131
     *
132
     * @return self
133
     */
134
    public function withAttachments(array $attachments = null)
135
    {
136
        $statement = clone $this;
137
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
138
139
        return $statement;
140
    }
141
142
    /**
143
     * Returns the Statement's {@link Verb}.
144
     *
145
     * @return Verb The Verb
146
     */
147
    public function getVerb()
148
    {
149
        return $this->verb;
150
    }
151
152
    /**
153
     * Returns the Statement's {@link Actor}.
154
     *
155
     * @return Actor The Actor
156
     */
157
    public function getActor()
158
    {
159
        return $this->actor;
160
    }
161
162
    /**
163
     * Returns the Statement's {@link Object}.
164
     *
165
     * @return \Xabbuh\XApi\Model\Object The Object
166
     */
167
    public function getObject()
168
    {
169
        return $this->object;
170
    }
171
172
    /**
173
     * Returns the {@link Activity} {@link Result}.
174
     *
175
     * @return Result The Result
176
     */
177
    public function getResult()
178
    {
179
        return $this->result;
180
    }
181
182
    /**
183
     * Returns the timestamp of when the events described in this statement
184
     * occurred.
185
     *
186
     * @return \DateTime The timestamp
187
     *
188
     * @deprecated since 1.2, to be removed in 3.0
189
     */
190
    public function getTimestamp()
191
    {
192
        @trigger_error(sprintf('The "%s()" method is deprecated since 1.2 and will be removed in 3.0. Use "%s::getCreated()" instead.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
193
194
        return $this->created;
195
    }
196
197
    /**
198
     * Returns the timestamp of when the events described in this statement
199
     * occurred.
200
     *
201
     * @return \DateTime The timestamp
202
     */
203
    public function getCreated()
204
    {
205
        return $this->created;
206
    }
207
208
    /**
209
     * Returns the {@link Statement} {@link Context}.
210
     *
211
     * @return Context The Context
212
     */
213
    public function getContext()
214
    {
215
        return $this->context;
216
    }
217
218
    public function getAttachments()
219
    {
220
        return $this->attachments;
221
    }
222
223
    /**
224
     * Tests whether or not this Statement is a void Statement (i.e. it voids
225
     * another Statement).
226
     *
227
     * @return bool True if the Statement voids another Statement, false otherwise
228
     */
229
    public function isVoidStatement()
230
    {
231
        return $this->verb->isVoidVerb();
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function equals(Object $statement)
238
    {
239
        if ('Xabbuh\XApi\Model\SubStatement' !== get_class($statement)) {
240
            return false;
241
        }
242
243
        /** @var SubStatement $statement */
244
245
        if (!$this->actor->equals($statement->actor)) {
246
            return false;
247
        }
248
249
        if (!$this->verb->equals($statement->verb)) {
250
            return false;
251
        }
252
253
        if (!$this->object->equals($statement->object)) {
254
            return false;
255
        }
256
257
        if (null === $this->result && null !== $statement->result) {
258
            return false;
259
        }
260
261
        if (null !== $this->result && null === $statement->result) {
262
            return false;
263
        }
264
265
        if (null !== $this->result && !$this->result->equals($statement->result)) {
266
            return false;
267
        }
268
269
        if ($this->created != $statement->created) {
270
            return false;
271
        }
272
273
        if (null !== $this->context xor null !== $statement->context) {
274
            return false;
275
        }
276
277
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
278
            return false;
279
        }
280
281
        if (null !== $this->attachments xor null !== $statement->attachments) {
282
            return false;
283
        }
284
285
        if (null !== $this->attachments && null !== $statement->attachments) {
286
            if (count($this->attachments) !== count($statement->attachments)) {
287
                return false;
288
            }
289
290
            foreach ($this->attachments as $key => $attachment) {
291
                if (!$attachment->equals($statement->attachments[$key])) {
292
                    return false;
293
                }
294
            }
295
        }
296
297
        return true;
298
    }
299
}
300