Statement   D
last analyzed

Complexity

Total Complexity 59

Size/Duplication

Total Lines 325
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 8

Test Coverage

Coverage 71.67%

Importance

Changes 0
Metric Value
wmc 59
lcom 3
cbo 8
dl 0
loc 325
rs 4.08
c 0
b 0
f 0
ccs 43
cts 60
cp 0.7167

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A withId() 0 7 1
A withActor() 0 7 1
A withVerb() 0 7 1
A withObject() 0 7 1
A withResult() 0 7 1
A withAuthority() 0 7 1
A withCreated() 0 7 1
A withStored() 0 7 1
A withContext() 0 7 1
A withAttachments() 0 7 2
A withVersion() 0 7 1
A getId() 0 4 1
A getVerb() 0 4 1
A getActor() 0 4 1
A getObject() 0 4 1
A getResult() 0 4 1
A getAuthority() 0 4 1
A getCreated() 0 4 1
A getStored() 0 4 1
A getContext() 0 4 1
A getAttachments() 0 4 1
A getVersion() 0 4 1
A isVoidStatement() 0 4 1
A getStatementReference() 0 4 1
A getVoidStatement() 0 9 1
D equals() 0 76 31

How to fix   Complexity   

Complex Class

Complex classes like Statement 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 Statement, 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
 * An Experience API {@link https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#statement Statement}.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class Statement
20
{
21
    private $id;
22
    private $verb;
23
    private $actor;
24
    private $object;
25
    private $result;
26
    private $authority;
27
    private $created;
28
    private $stored;
29
    private $context;
30
    private $attachments;
31
    private $version;
32
33
    /**
34
     * @param Attachment[]|null $attachments
35
     */
36
    public function __construct(StatementId $id = null, Actor $actor, Verb $verb, StatementObject $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null, array $attachments = null, string $version = null)
37
    {
38
        $this->id = $id;
39
        $this->actor = $actor;
40
        $this->verb = $verb;
41
        $this->object = $object;
42
        $this->result = $result;
43
        $this->authority = $authority;
44
        $this->created = $created;
45
        $this->stored = $stored;
46
        $this->context = $context;
47
        $this->attachments = null !== $attachments ? array_values($attachments) : null;
48
        $this->version = $version;
49
    }
50
51
    public function withId(StatementId $id = null): self
52
    {
53
        $statement = clone $this;
54
        $statement->id = $id;
55
56
        return $statement;
57
    }
58
59
    public function withActor(Actor $actor): self
60
    {
61 4
        $statement = clone $this;
62
        $statement->actor = $actor;
63 4
64 4
        return $statement;
65 4
    }
66 4
67 4
    public function withVerb(Verb $verb): self
68 4
    {
69 4
        $statement = clone $this;
70 4
        $statement->verb = $verb;
71 4
72
        return $statement;
73
    }
74
75
    public function withObject(StatementObject $object): self
76
    {
77
        $statement = clone $this;
78 2
        $statement->object = $object;
79
80 2
        return $statement;
81
    }
82
83
    public function withResult(Result $result = null): self
84
    {
85
        $statement = clone $this;
86
        $statement->result = $result;
87
88 3
        return $statement;
89
    }
90 3
91
    /**
92
     * Creates a new Statement based on the current one containing an Authority
93
     * that asserts the Statement true.
94
     */
95
    public function withAuthority(Actor $authority = null): self
96
    {
97
        $statement = clone $this;
98 3
        $statement->authority = $authority;
99
100 3
        return $statement;
101
    }
102
103
    public function withCreated(\DateTime $created = null): self
104
    {
105
        $statement = clone $this;
106
        $statement->created = $created;
107
108 3
        return $statement;
109
    }
110 3
111
    public function withStored(\DateTime $stored = null): self
112
    {
113
        $statement = clone $this;
114
        $statement->stored = $stored;
115
116
        return $statement;
117
    }
118
119
    public function withContext(Context $context = null): self
120
    {
121
        $statement = clone $this;
122
        $statement->context = $context;
123
124
        return $statement;
125
    }
126
127
    /**
128 2
     * @param Attachment[]|null $attachments
129
     */
130 2
    public function withAttachments(array $attachments = null): self
131
    {
132
        $statement = clone $this;
133
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
134
135
        return $statement;
136
    }
137
138
    public function withVersion(string $version = null): self
139
    {
140
        $statement = clone $this;
141
        $statement->version = $version;
142
143
        return $statement;
144
    }
145
146
    /**
147
     * Returns the Statement's unique identifier.
148
     */
149
    public function getId(): ?StatementId
150
    {
151
        return $this->id;
152
    }
153
154
    /**
155
     * Returns the Statement's {@link Verb}.
156
     */
157
    public function getVerb(): Verb
158
    {
159
        return $this->verb;
160
    }
161
162
    /**
163
     * Returns the Statement's {@link Actor}.
164
     */
165
    public function getActor(): Actor
166
    {
167
        return $this->actor;
168
    }
169
170 2
    /**
171
     * Returns the Statement's {@link StatementObject}.
172 2
     */
173
    public function getObject(): StatementObject
174 2
    {
175
        return $this->object;
176
    }
177
178
    /**
179
     * Returns the {@link Activity} {@link Result}.
180
     */
181
    public function getResult(): ?Result
182
    {
183
        return $this->result;
184 1
    }
185
186 1
    /**
187 1
     * Returns the Authority that asserted the Statement true.
188
     */
189 1
    public function getAuthority(): ?Actor
190 1
    {
191
        return $this->authority;
192
    }
193
194
    /**
195
     * Returns the timestamp of when the events described in this statement
196
     * occurred.
197
     */
198
    public function getCreated(): ?\DateTime
199
    {
200
        return $this->created;
201
    }
202 2
203
    /**
204 2
     * Returns the timestamp of when this statement was recorded by the LRS.
205
     */
206
    public function getStored(): ?\DateTime
207
    {
208
        return $this->stored;
209
    }
210
211
    /**
212
     * Returns the context that gives the statement more meaning.
213
     */
214
    public function getContext(): ?Context
215
    {
216 2
        return $this->context;
217
    }
218 2
219
    /**
220
     * @return Attachment[]|null
221
     */
222 2
    public function getAttachments(): ?array
223
    {
224
        return $this->attachments;
225
    }
226 2
227
    public function getVersion(): ?string
228
    {
229
        return $this->version;
230 2
    }
231
232
    /**
233
     * Tests whether or not this Statement is a void Statement (i.e. it voids
234 2
     * another Statement).
235
     */
236
    public function isVoidStatement(): bool
237
    {
238 2
        return $this->verb->isVoidVerb();
239
    }
240
241
    /**
242 2
     * Returns a {@link StatementReference} for the Statement.
243
     */
244
    public function getStatementReference(): StatementReference
245
    {
246 2
        return new StatementReference($this->id);
0 ignored issues
show
Bug introduced by
It seems like $this->id can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
247 1
    }
248
249
    /**
250 1
     * Returns a Statement that voids the current Statement.
251
     */
252
    public function getVoidStatement(Actor $actor): self
253
    {
254 1
        return new Statement(
255 1
            null,
256
            $actor,
257
            Verb::createVoidVerb(),
258
            $this->getStatementReference()
259
        );
260
    }
261
262
    /**
263
     * Checks if another statement is equal.
264
     *
265
     * Two statements are equal if and only if all of their properties are equal.
266
     */
267
    public function equals(Statement $statement): bool
268
    {
269
        if (null !== $this->id xor null !== $statement->id) {
270
            return false;
271
        }
272
273
        if (null !== $this->id && null !== $statement->id && !$this->id->equals($statement->id)) {
274
            return false;
275
        }
276
277
        if (!$this->actor->equals($statement->actor)) {
278
            return false;
279
        }
280
281
        if (!$this->verb->equals($statement->verb)) {
282
            return false;
283
        }
284
285
        if (!$this->object->equals($statement->object)) {
286
            return false;
287
        }
288
289
        if (null === $this->result && null !== $statement->result) {
290
            return false;
291
        }
292
293
        if (null !== $this->result && null === $statement->result) {
294
            return false;
295
        }
296
297
        if (null !== $this->result && !$this->result->equals($statement->result)) {
0 ignored issues
show
Bug introduced by
It seems like $statement->result can be null; however, equals() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
298
            return false;
299
        }
300
301
        if (null === $this->authority && null !== $statement->authority) {
302
            return false;
303
        }
304
305
        if (null !== $this->authority && null === $statement->authority) {
306
            return false;
307
        }
308
309
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
0 ignored issues
show
Bug introduced by
It seems like $statement->authority can be null; however, equals() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
310
            return false;
311
        }
312
313
        if ($this->created != $statement->created) {
314
            return false;
315
        }
316
317
        if (null !== $this->context xor null !== $statement->context) {
318
            return false;
319
        }
320
321
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
322
            return false;
323
        }
324
325
        if (null !== $this->attachments xor null !== $statement->attachments) {
326
            return false;
327
        }
328
329
        if (null !== $this->attachments && null !== $statement->attachments) {
330
            if (count($this->attachments) !== count($statement->attachments)) {
331
                return false;
332
            }
333
334
            foreach ($this->attachments as $key => $attachment) {
335
                if (!$attachment->equals($statement->attachments[$key])) {
336
                    return false;
337
                }
338
            }
339
        }
340
341
        return true;
342
    }
343
}
344