Completed
Push — master ( ee41a7...105acf )
by Christian
11s
created

SubStatement   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 41
c 1
b 1
f 0
lcom 1
cbo 5
dl 0
loc 253
ccs 0
cts 74
cp 0
rs 8.2769

17 Methods

Rating   Name   Duplication   Size   Complexity  
A withAttachments() 0 7 2
A getObject() 0 4 1
A getResult() 0 4 1
A withActor() 0 7 1
A withVerb() 0 7 1
A withObject() 0 7 1
A withResult() 0 7 1
A __construct() 0 14 3
A withCreated() 0 7 1
A withContext() 0 7 1
A getVerb() 0 4 1
A getActor() 0 4 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 StatementObject
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 StatementObject}
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, StatementObject $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(StatementObject $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
    public function withCreated(\DateTime $created = null)
101
    {
102
        $statement = clone $this;
103
        $statement->created = $created;
104
105
        return $statement;
106
    }
107
108
    public function withContext(Context $context)
109
    {
110
        $subStatement = clone $this;
111
        $subStatement->context = $context;
112
113
        return $subStatement;
114
    }
115
116
    /**
117
     * @param Attachment[]|null $attachments
118
     *
119
     * @return self
120
     */
121
    public function withAttachments(array $attachments = null)
122
    {
123
        $statement = clone $this;
124
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
125
126
        return $statement;
127
    }
128
129
    /**
130
     * Returns the Statement's {@link Verb}.
131
     *
132
     * @return Verb The Verb
133
     */
134
    public function getVerb()
135
    {
136
        return $this->verb;
137
    }
138
139
    /**
140
     * Returns the Statement's {@link Actor}.
141
     *
142
     * @return Actor The Actor
143
     */
144
    public function getActor()
145
    {
146
        return $this->actor;
147
    }
148
149
    /**
150
     * Returns the Statement's {@link StatementObject}.
151
     *
152
     * @return \Xabbuh\XApi\Model\StatementObject The Object
153
     */
154
    public function getObject()
155
    {
156
        return $this->object;
157
    }
158
159
    /**
160
     * Returns the {@link Activity} {@link Result}.
161
     *
162
     * @return Result The Result
163
     */
164
    public function getResult()
165
    {
166
        return $this->result;
167
    }
168
169
    /**
170
     * Returns the timestamp of when the events described in this statement
171
     * occurred.
172
     *
173
     * @return \DateTime The timestamp
174
     */
175
    public function getCreated()
176
    {
177
        return $this->created;
178
    }
179
180
    /**
181
     * Returns the {@link Statement} {@link Context}.
182
     *
183
     * @return Context The Context
184
     */
185
    public function getContext()
186
    {
187
        return $this->context;
188
    }
189
190
    public function getAttachments()
191
    {
192
        return $this->attachments;
193
    }
194
195
    /**
196
     * Tests whether or not this Statement is a void Statement (i.e. it voids
197
     * another Statement).
198
     *
199
     * @return bool True if the Statement voids another Statement, false otherwise
200
     */
201
    public function isVoidStatement()
202
    {
203
        return $this->verb->isVoidVerb();
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function equals(StatementObject $statement)
210
    {
211
        if ('Xabbuh\XApi\Model\SubStatement' !== get_class($statement)) {
212
            return false;
213
        }
214
215
        /** @var SubStatement $statement */
216
217
        if (!$this->actor->equals($statement->actor)) {
218
            return false;
219
        }
220
221
        if (!$this->verb->equals($statement->verb)) {
222
            return false;
223
        }
224
225
        if (!$this->object->equals($statement->object)) {
226
            return false;
227
        }
228
229
        if (null === $this->result && null !== $statement->result) {
230
            return false;
231
        }
232
233
        if (null !== $this->result && null === $statement->result) {
234
            return false;
235
        }
236
237
        if (null !== $this->result && !$this->result->equals($statement->result)) {
238
            return false;
239
        }
240
241
        if ($this->created != $statement->created) {
242
            return false;
243
        }
244
245
        if (null !== $this->context xor null !== $statement->context) {
246
            return false;
247
        }
248
249
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
250
            return false;
251
        }
252
253
        if (null !== $this->attachments xor null !== $statement->attachments) {
254
            return false;
255
        }
256
257
        if (null !== $this->attachments && null !== $statement->attachments) {
258
            if (count($this->attachments) !== count($statement->attachments)) {
259
                return false;
260
            }
261
262
            foreach ($this->attachments as $key => $attachment) {
263
                if (!$attachment->equals($statement->attachments[$key])) {
264
                    return false;
265
                }
266
            }
267
        }
268
269
        return true;
270
    }
271
}
272