Completed
Push — master ( 2b511a...1544c6 )
by Christian
10s
created

Activity::equals()   C

Complexity

Conditions 9
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 19.125

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 3
cts 6
cp 0.5
rs 5.3563
c 0
b 0
f 0
cc 9
eloc 12
nc 6
nop 1
crap 19.125
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 Activity in a {@link Statement}.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class Activity extends StatementObject
20
{
21
    private $id;
22
    private $definition;
23
24
    public function __construct(IRI $id, Definition $definition = null)
25
    {
26
        $this->id = $id;
27
        $this->definition = $definition;
28
    }
29
30
    /**
31
     * Returns the Activity's unique identifier.
32
     */
33
    public function getId(): IRI
34
    {
35
        return $this->id;
36 13
    }
37
38 13
    /**
39 13
     * Returns the Activity's {@link Definition}.
40 13
     */
41
    public function getDefinition(): ?Definition
42
    {
43
        return $this->definition;
44
    }
45
46
    /**
47 1
     * {@inheritdoc}
48
     */
49 1
    public function equals(StatementObject $object): bool
50
    {
51
        if (!$object instanceof Activity) {
52
            return false;
53
        }
54
55
        if (!$this->id->equals($object->id)) {
56
            return false;
57
        }
58
59
        if (null === $this->definition && null !== $object->definition) {
60
            return false;
61
        }
62
63
        if (null !== $this->definition && null === $object->definition) {
64
            return false;
65 2
        }
66
67 2
        if (null !== $this->definition && !$this->definition->equals($object->definition)) {
0 ignored issues
show
Bug introduced by
It seems like $object->definition 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...
68
            return false;
69
        }
70
71
        return true;
72
    }
73
}
74