Completed
Push — master ( 21b23e...04c0e6 )
by Christian
04:03
created

Statement::equals()   C

Complexity

Conditions 21
Paths 13

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 462

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 52
rs 5.8531
c 1
b 0
f 0
ccs 0
cts 0
cp 0
cc 21
eloc 26
nc 13
nop 1
crap 462

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
22
     * @var StatementId|null The unique identifier
23
     */
24
    private $id;
25
26
    /**
27
     * @var Verb $verb The {@link Verb}
28
     */
29
    private $verb;
30
31
    /**
32
     * @var Actor The {@link Actor}
33
     */
34
    private $actor;
35
36
    /**
37
     * @var Object The {@link Object}
38
     */
39
    private $object;
40
41
    /**
42
     * @var Result The {@link Activity} {@link Result}
43
     */
44
    private $result;
45
46
    /**
47
     * @var Actor The Authority that asserted the Statement true
48
     */
49
    private $authority;
50
51
    /**
52
     * @var \DateTime The timestamp of when the events described in this statement occurred
53
     */
54
    private $created;
55
56
    /**
57
     * @var \DateTime The timestamp of when this statement was recorded by the LRS
58
     */
59
    private $stored;
60
61 4
    /**
62
     * @var Context|null A context giving the statement more meaning
63 4
     */
64 4
    private $context;
65 4
66 4
    public function __construct(StatementId $id = null, Actor $actor, Verb $verb, Object $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null)
67 4
    {
68 4
        $this->id = $id;
69 4
        $this->actor = $actor;
70 4
        $this->verb = $verb;
71 4
        $this->object = $object;
72
        $this->result = $result;
73
        $this->authority = $authority;
74
        $this->created = $created;
75
        $this->stored = $stored;
76
        $this->context = $context;
77
    }
78 2
79
    public function withId(StatementId $id = null)
80 2
    {
81
        $statement = clone $this;
82
        $statement->id = $id;
83
84
        return $statement;
85
    }
86
87
    public function withActor(Actor $actor)
88 3
    {
89
        $statement = clone $this;
90 3
        $statement->actor = $actor;
91
92
        return $statement;
93
    }
94
95
    public function withVerb(Verb $verb)
96
    {
97
        $statement = clone $this;
98 3
        $statement->verb = $verb;
99
100 3
        return $statement;
101
    }
102
103
    public function withObject(Object $object)
104
    {
105
        $statement = clone $this;
106
        $statement->object = $object;
107
108 3
        return $statement;
109
    }
110 3
111
    public function withResult(Result $result)
112
    {
113
        $statement = clone $this;
114
        $statement->result = $result;
115
116
        return $statement;
117
    }
118
119
    /**
120
     * Creates a new Statement based on the current one containing an Authority
121
     * that asserts the Statement true.
122
     *
123
     * @param Actor $authority The Authority asserting the Statement true
124
     *
125
     * @return Statement The new Statement
126
     */
127
    public function withAuthority(Actor $authority)
128 2
    {
129
        $statement = clone $this;
130 2
        $statement->authority = $authority;
131
132
        return $statement;
133
    }
134
135
    public function withTimestamp(\DateTime $timestamp)
136
    {
137
        $statement = clone $this;
138
        $statement->created = $timestamp;
139
140
        return $statement;
141
    }
142
143
    public function withStored(\DateTime $stored)
144
    {
145
        $statement = clone $this;
146
        $statement->stored = $stored;
147
148
        return $statement;
149
    }
150
151
    public function withContext(Context $context)
152
    {
153
        $statement = clone $this;
154
        $statement->context = $context;
155
156
        return $statement;
157
    }
158
159
    /**
160
     * Returns the Statement's unique identifier.
161
     *
162
     * @return StatementId|null The identifier
163
     */
164
    public function getId()
165
    {
166
        return $this->id;
167
    }
168
169
    /**
170 2
     * Returns the Statement's {@link Verb}.
171
     *
172 2
     * @return Verb The Verb
173
     */
174 2
    public function getVerb()
175
    {
176
        return $this->verb;
177
    }
178
179
    /**
180
     * Returns the Statement's {@link Actor}.
181
     *
182
     * @return Actor The Actor
183
     */
184 1
    public function getActor()
185
    {
186 1
        return $this->actor;
187 1
    }
188
189 1
    /**
190 1
     * Returns the Statement's {@link Object}.
191
     *
192
     * @return \Xabbuh\XApi\Model\Object The Object
193
     */
194
    public function getObject()
195
    {
196
        return $this->object;
197
    }
198
199
    /**
200
     * Returns the {@link Activity} {@link Result}.
201
     *
202 2
     * @return Result The Result
203
     */
204 2
    public function getResult()
205
    {
206
        return $this->result;
207
    }
208
209
    /**
210
     * Returns the Authority that asserted the Statement true.
211
     *
212
     * @return Actor The Authority
213
     */
214
    public function getAuthority()
215
    {
216 2
        return $this->authority;
217
    }
218 2
219
    /**
220
     * Returns the timestamp of when the events described in this statement
221
     * occurred.
222 2
     *
223
     * @return \DateTime The timestamp
224
     */
225
    public function getTimestamp()
226 2
    {
227
        return $this->created;
228
    }
229
230 2
    /**
231
     * Returns the timestamp of when the events described in this statement
232
     * occurred.
233
     *
234 2
     * @return \DateTime The timestamp
235
     */
236
    public function getCreated()
237
    {
238 2
        return $this->created;
239
    }
240
241
    /**
242 2
     * Returns the timestamp of when this statement was recorded by the LRS.
243
     *
244
     * @return \DateTime The timestamp
245
     */
246 2
    public function getStored()
247 1
    {
248
        return $this->stored;
249
    }
250 1
251
    /**
252
     * Returns the context that gives the statement more meaning.
253
     *
254 1
     * @return Context|null
255 1
     */
256
    public function getContext()
257
    {
258
        return $this->context;
259
    }
260
261
    /**
262
     * Tests whether or not this Statement is a void Statement (i.e. it voids
263
     * another Statement).
264
     *
265
     * @return bool True if the Statement voids another Statement, false otherwise
266
     */
267
    public function isVoidStatement()
268
    {
269
        return $this->verb->isVoidVerb();
270
    }
271
272
    /**
273
     * Returns a {@link StatementReference} for the Statement.
274
     *
275
     * @return StatementReference The reference
276
     */
277
    public function getStatementReference()
278
    {
279
        $reference = 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...
280
281
        return $reference;
282
    }
283
284
    /**
285
     * Returns a Statement that voids the current Statement.
286
     *
287
     * @param Actor $actor The Actor voiding this Statement
288
     *
289
     * @return Statement The voiding Statement
290
     */
291
    public function getVoidStatement(Actor $actor)
292
    {
293
        return new Statement(
294
            null,
295
            $actor,
296
            Verb::createVoidVerb(),
297
            $this->getStatementReference()
298
        );
299
    }
300
301
    /**
302
     * Checks if another statement is equal.
303
     *
304
     * Two statements are equal if and only if all of their properties are equal.
305
     *
306
     * @param Statement $statement The statement to compare with
307
     *
308
     * @return bool True if the statements are equal, false otherwise
309
     */
310
    public function equals(Statement $statement)
311
    {
312
        if (null === $this->id && null !== $statement->id) {
313
            return false;
314
        }
315
316
        if (null !== $this->id && !$this->id->equals($statement->id)) {
0 ignored issues
show
Bug introduced by
It seems like $statement->id 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...
317
            return false;
318
        }
319
320
        if (!$this->actor->equals($statement->actor)) {
321
            return false;
322
        }
323
324
        if (!$this->verb->equals($statement->verb)) {
325
            return false;
326
        }
327
328
        if (!$this->object->equals($statement->object)) {
329
            return false;
330
        }
331
332
        if (null === $this->result && null !== $statement->result) {
333
            return false;
334
        }
335
336
        if (null !== $this->result && null === $statement->result) {
337
            return false;
338
        }
339
340
        if (null !== $this->result && !$this->result->equals($statement->result)) {
341
            return false;
342
        }
343
344
        if (null === $this->authority && null !== $statement->authority) {
345
            return false;
346
        }
347
348
        if (null !== $this->authority && null === $statement->authority) {
349
            return false;
350
        }
351
352
        if (null !== $this->authority && !$this->authority->equals($statement->authority)) {
353
            return false;
354
        }
355
356
        if ($this->created != $statement->created) {
357
            return false;
358
        }
359
360
        return true;
361
    }
362
}
363