Result::equals()   C
last analyzed

Complexity

Conditions 12
Paths 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 6.9666
c 0
b 0
f 0
ccs 0
cts 6
cp 0
cc 12
nc 9
nop 1
crap 156

How to fix   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 {@link Actor Actor's} outcome related to the {@link Statement} in which
16
 * it is included.
17
 *
18
 * @author Christian Flothmann <[email protected]>
19
 */
20
final class Result
21
{
22
    private $score;
23
    private $success;
24
    private $completion;
25
    private $response;
26
    private $duration;
27
    private $extensions;
28
29
    public function __construct(Score $score = null, bool $success = null, bool $completion = null, string $response = null, string $duration = null, Extensions $extensions = null)
30
    {
31
        $this->score = $score;
32
        $this->success = $success;
33
        $this->completion = $completion;
34
        $this->response = $response;
35
        $this->duration = $duration;
36
        $this->extensions = $extensions;
37
    }
38
39
    public function withScore(Score $score = null): self
40
    {
41
        $result = clone $this;
42
        $result->score = $score;
43
44
        return $result;
45
    }
46
47
    public function withSuccess(bool $success = null): self
48
    {
49
        $result = clone $this;
50
        $result->success = $success;
51
52
        return $result;
53
    }
54
55
    public function withCompletion(bool $completion = null): self
56
    {
57
        $result = clone $this;
58
        $result->completion = $completion;
59
60
        return $result;
61
    }
62
63
    public function withResponse(string $response = null): self
64
    {
65
        $result = clone $this;
66
        $result->response = $response;
67
68
        return $result;
69
    }
70
71
    public function withDuration(string $duration = null): self
72
    {
73
        $result = clone $this;
74
        $result->duration = $duration;
75
76
        return $result;
77
    }
78
79
    public function withExtensions(Extensions $extensions = null): self
80
    {
81
        $result = clone $this;
82
        $result->extensions = $extensions;
83
84
        return $result;
85
    }
86
87
    /**
88
     * Returns the user's score.
89
     */
90
    public function getScore(): ?Score
91
    {
92
        return $this->score;
93
    }
94
95
    /**
96
     * Returns whether or not the user finished a task successfully.
97
     */
98
    public function getSuccess(): ?bool
99
    {
100
        return $this->success;
101
    }
102
103
    /**
104
     * Returns the completion status.
105
     */
106
    public function getCompletion(): ?bool
107
    {
108
        return $this->completion;
109
    }
110
111
    /**
112
     * Returns the response.
113
     */
114
    public function getResponse(): ?string
115
    {
116
        return $this->response;
117
    }
118
119
    /**
120
     * Returns the period of time over which the Activity was performed.
121
     */
122
    public function getDuration(): ?string
123
    {
124
        return $this->duration;
125
    }
126
127
    /**
128
     * Returns the extensions associated with the result.
129
     */
130
    public function getExtensions(): ?Extensions
131
    {
132
        return $this->extensions;
133
    }
134
135
    /**
136
     * Checks if another result is equal.
137
     *
138
     * Two results are equal if and only if all of their properties are equal.
139
     */
140
    public function equals(Result $result): bool
141
    {
142
        if (null !== $this->score xor null !== $result->score) {
143
            return false;
144
        }
145
146
        if (null !== $this->score && !$this->score->equals($result->score)) {
0 ignored issues
show
Bug introduced by
It seems like $result->score 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...
147
            return false;
148
        }
149
150
        if ($this->success !== $result->success) {
151
            return false;
152
        }
153
154
        if ($this->completion !== $result->completion) {
155
            return false;
156
        }
157
158
        if ($this->response !== $result->response) {
159
            return false;
160
        }
161
162
        if ($this->duration !== $result->duration) {
163
            return false;
164
        }
165
166
        if (null !== $this->extensions xor null !== $result->extensions) {
167
            return false;
168
        }
169
170
        if (null !== $this->extensions && null !== $result->extensions && !$this->extensions->equals($result->extensions)) {
171
            return false;
172
        }
173
174
        return true;
175
    }
176
}
177