Completed
Pull Request — master (#27)
by Christian
02:36
created

InteractionComponent::equals()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
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\Interaction;
13
14
use Xabbuh\XApi\Model\LanguageMap;
15
16
/**
17
 * An XAPI activity interaction component.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class InteractionComponent
22
{
23
    private $id;
24
    private $description;
25
26
    /**
27
     * @param string           $id
28
     * @param LanguageMap|null $description
29
     */
30
    public function __construct($id, LanguageMap $description = null)
31
    {
32
        $this->id = $id;
33
        $this->description = $description;
34
    }
35
36
    public function getId()
37
    {
38
        return $this->id;
39
    }
40
41
    public function getDescription()
42
    {
43
        return $this->description;
44
    }
45
46
    public function equals(InteractionComponent $interactionComponent)
47
    {
48
        if ($this->id !== $interactionComponent->id) {
49
            return false;
50
        }
51
52
        if (null !== $this->description xor null !== $interactionComponent->description) {
53
            return false;
54
        }
55
56
        if (null !== $this->description && !$this->description->equals($interactionComponent->description)) {
0 ignored issues
show
Bug introduced by
It seems like $interactionComponent->description 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...
57
            return false;
58
        }
59
60
        return true;
61
    }
62
}
63