Completed
Pull Request — master (#8)
by Christian
02:48
created

Definition::equals()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 40
rs 5.2653
ccs 0
cts 10
cp 0
cc 11
eloc 20
nc 14
nop 1
crap 132

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
 * Definition of an {@link Activity}.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class Definition
20
{
21
    /**
22
     * The human readable activity name
23
     * @var array
24
     */
25
    private $name;
26
27
    /**
28
     * The human readable activity description
29
     * @var array
30
     */
31
    private $description;
32
33
    /**
34
     * The type of the {@link Activity}
35
     * @var string
36
     */
37
    private $type;
38
39
    /**
40
     * An IRL where human-readable information describing the {@link Activity} can be found.
41
     *
42
     * @var string|null
43
     */
44
    private $moreInfo;
45
46
    /**
47
     * @param array|null  $name
48
     * @param array|null  $description
49
     * @param string|null $type
50
     * @param string|null $moreInfo
51
     */
52
    public function __construct(array $name = null, array $description = null, $type = null, $moreInfo = null)
53
    {
54
        $this->name = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name can be null. However, the property $name is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
55
        $this->description = $description;
0 ignored issues
show
Documentation Bug introduced by
It seems like $description can be null. However, the property $description is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
56
        $this->type = $type;
57
        $this->moreInfo = $moreInfo;
58
    }
59
60
    /**
61
     * Returns the human readable names.
62
     *
63
     * @return array The name language map
64
     */
65
    public function getName()
66
    {
67
        return $this->name;
68
    }
69
70
    /**
71
     * Returns the human readable descriptions.
72
     *
73
     * @return array The description language map
74
     */
75
    public function getDescription()
76
    {
77
        return $this->description;
78
    }
79
80
    /**
81
     * Returns the {@link Activity} type.
82
     *
83
     * @return string The type
84
     */
85
    public function getType()
86
    {
87
        return $this->type;
88
    }
89
90
    /**
91
     * Returns an IRL where human-readable information about the activity can be found.
92
     *
93
     * @return string|null
94
     */
95
    public function getMoreInfo()
96
    {
97
        return $this->moreInfo;
98
    }
99
100
    /**
101
     * Checks if another definition is equal.
102
     *
103
     * Two definitions are equal if and only if all of their properties are equal.
104
     *
105
     * @param Definition $definition The definition to compare with
106
     *
107
     * @return bool True if the definitions are equal, false otherwise
108
     */
109
    public function equals(Definition $definition)
110
    {
111
        if ($this->type !== $definition->type) {
112
            return false;
113
        }
114
115
        if ($this->moreInfo !== $definition->moreInfo) {
116
            return false;
117
        }
118
119
        if (count($this->name) !== count($definition->name)) {
120
            return false;
121
        }
122
123
        if (count($this->description) !== count($definition->description)) {
124
            return false;
125
        }
126
127
        foreach ($this->name as $language => $value) {
128
            if (!isset($definition->name[$language])) {
129
                return false;
130
            }
131
132
            if ($value !== $definition->name[$language]) {
133
                return false;
134
            }
135
        }
136
137
        foreach ($this->description as $language => $value) {
138
            if (!isset($definition->description[$language])) {
139
                return false;
140
            }
141
142
            if ($value !== $definition->description[$language]) {
143
                return false;
144
            }
145
        }
146
147
        return true;
148
    }
149
}
150