SubStatement::equals()   D
last analyzed

Complexity

Conditions 22
Paths 15

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 506

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 4.1666
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 22
nc 15
nop 1
crap 506

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
 * 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
    private $verb;
22
    private $actor;
23
    private $object;
24
    private $result;
25
    private $created;
26
    private $context;
27
    private $attachments;
28
29
    /**
30
     * @param Attachment[]|null $attachments
31
     */
32
    public function __construct(Actor $actor, Verb $verb, StatementObject $object, Result $result = null, Context $context = null, \DateTime $created = null, array $attachments = null)
33
    {
34
        if ($object instanceof SubStatement) {
35
            throw new \InvalidArgumentException('Nesting sub statements is forbidden by the xAPI spec.');
36
        }
37
38
        $this->actor = $actor;
39
        $this->verb = $verb;
40
        $this->object = $object;
41
        $this->result = $result;
42
        $this->created = $created;
43
        $this->context = $context;
44
        $this->attachments = null !== $attachments ? array_values($attachments) : null;
45
    }
46
47
    public function withActor(Actor $actor): self
48
    {
49
        $subStatement = clone $this;
50
        $subStatement->actor = $actor;
51
52
        return $subStatement;
53
    }
54
55
    public function withVerb(Verb $verb): self
56
    {
57
        $subStatement = clone $this;
58
        $subStatement->verb = $verb;
59
60
        return $subStatement;
61
    }
62
63
    public function withObject(StatementObject $object): self
64
    {
65
        $subStatement = clone $this;
66
        $subStatement->object = $object;
67
68
        return $subStatement;
69
    }
70
71
    public function withResult(Result $result): self
72
    {
73
        $subStatement = clone $this;
74
        $subStatement->result = $result;
75
76
        return $subStatement;
77
    }
78
79
    public function withCreated(\DateTime $created = null): self
80
    {
81
        $statement = clone $this;
82
        $statement->created = $created;
83
84
        return $statement;
85
    }
86
87
    public function withContext(Context $context): self
88
    {
89
        $subStatement = clone $this;
90
        $subStatement->context = $context;
91
92
        return $subStatement;
93
    }
94
95
    /**
96
     * @param Attachment[]|null $attachments
97
     */
98
    public function withAttachments(array $attachments = null): self
99
    {
100
        $statement = clone $this;
101
        $statement->attachments = null !== $attachments ? array_values($attachments) : null;
102
103
        return $statement;
104
    }
105
106
    /**
107
     * Returns the Statement's {@link Verb}.
108
     */
109
    public function getVerb(): Verb
110
    {
111
        return $this->verb;
112
    }
113
114
    /**
115
     * Returns the Statement's {@link Actor}.
116
     */
117
    public function getActor(): Actor
118
    {
119
        return $this->actor;
120
    }
121
122
    /**
123
     * Returns the Statement's {@link StatementObject}.
124
     */
125
    public function getObject(): StatementObject
126
    {
127
        return $this->object;
128
    }
129
130
    /**
131
     * Returns the {@link Activity} {@link Result}.
132
     */
133
    public function getResult(): ?Result
134
    {
135
        return $this->result;
136
    }
137
138
    /**
139
     * Returns the timestamp of when the events described in this statement
140
     * occurred.
141
     */
142
    public function getCreated(): ?\DateTime
143
    {
144
        return $this->created;
145
    }
146
147
    /**
148
     * Returns the {@link Statement} {@link Context}.
149
     */
150
    public function getContext(): ?Context
151
    {
152
        return $this->context;
153
    }
154
155
    /**
156
     * @return Attachment[]|null
157
     */
158
    public function getAttachments(): ?array
159
    {
160
        return $this->attachments;
161
    }
162
163
    /**
164
     * Tests whether or not this Statement is a void Statement (i.e. it voids
165
     * another Statement).
166
     */
167
    public function isVoidStatement(): bool
168
    {
169
        return $this->verb->isVoidVerb();
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function equals(StatementObject $statement): bool
176
    {
177
        if (!$statement instanceof SubStatement) {
178
            return false;
179
        }
180
181
        if (!$this->actor->equals($statement->actor)) {
182
            return false;
183
        }
184
185
        if (!$this->verb->equals($statement->verb)) {
186
            return false;
187
        }
188
189
        if (!$this->object->equals($statement->object)) {
190
            return false;
191
        }
192
193
        if (null === $this->result && null !== $statement->result) {
194
            return false;
195
        }
196
197
        if (null !== $this->result && null === $statement->result) {
198
            return false;
199
        }
200
201
        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...
202
            return false;
203
        }
204
205
        if ($this->created != $statement->created) {
206
            return false;
207
        }
208
209
        if (null !== $this->context xor null !== $statement->context) {
210
            return false;
211
        }
212
213
        if (null !== $this->context && null !== $statement->context && !$this->context->equals($statement->context)) {
214
            return false;
215
        }
216
217
        if (null !== $this->attachments xor null !== $statement->attachments) {
218
            return false;
219
        }
220
221
        if (null !== $this->attachments && null !== $statement->attachments) {
222
            if (count($this->attachments) !== count($statement->attachments)) {
223
                return false;
224
            }
225
226
            foreach ($this->attachments as $key => $attachment) {
227
                if (!$attachment->equals($statement->attachments[$key])) {
228
                    return false;
229
                }
230
            }
231
        }
232
233
        return true;
234
    }
235
}
236