Completed
Push — master ( 2bedd9...b42c0c )
by Christian
02:23
created

MatchingInteractionDefinition::equals()   C

Complexity

Conditions 15
Paths 17

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 5.9385
c 0
b 0
f 0
cc 15
eloc 26
nc 17
nop 1

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\Interaction;
13
14
use Xabbuh\XApi\Model\Definition;
15
use Xabbuh\XApi\Model\LanguageMap;
16
17
/**
18
 * An interaction where the learner is asked to match items in one set
19
 * (the source set) to items in another set (the target set).
20
 *
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
final class MatchingInteractionDefinition extends InteractionDefinition
24
{
25
    private $source;
26
    private $target;
27
28
    /**
29
     * @param LanguageMap|null            $name
30
     * @param LanguageMap|null            $description
31
     * @param string|null                 $type
32
     * @param string|null                 $moreInfo
33
     * @param string[]|null               $correctResponsesPattern
34
     * @param InteractionComponent[]|null $source
35
     * @param InteractionComponent[]|null $target
36
     */
37
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, $type = null, $moreInfo = null, array $correctResponsesPattern = null, array $source = null, array $target = null)
38
    {
39
        parent::__construct($name, $description, $type, $moreInfo, $correctResponsesPattern);
40
41
        $this->source = $source;
42
        $this->target = $target;
43
    }
44
45
    /**
46
     * @param InteractionComponent[]|null $source
47
     *
48
     * @return static
49
     */
50
    public function withSource(array $source = null)
51
    {
52
        $interaction = clone $this;
53
        $interaction->source = $source;
54
55
        return $interaction;
56
    }
57
58
    /**
59
     * @param InteractionComponent[]|null $target
60
     *
61
     * @return static
62
     */
63
    public function withTarget(array $target = null)
64
    {
65
        $interaction = clone $this;
66
        $interaction->target = $target;
67
68
        return $interaction;
69
    }
70
71
    public function getSource()
72
    {
73
        return $this->source;
74
    }
75
76
    public function getTarget()
77
    {
78
        return $this->target;
79
    }
80
81
    public function equals(Definition $definition)
82
    {
83
        if (!parent::equals($definition)) {
84
            return false;
85
        }
86
87
        if (!$definition instanceof MatchingInteractionDefinition) {
88
            return false;
89
        }
90
91
        if (null !== $this->source xor null !== $definition->source) {
92
            return false;
93
        }
94
95
        if (null !== $this->target xor null !== $definition->target) {
96
            return false;
97
        }
98
99
        if (null !== $this->source) {
100
            if (count($this->source) !== count($definition->source)) {
101
                return false;
102
            }
103
104
            foreach ($this->source as $key => $source) {
105
                if (!isset($definition->source[$key])) {
106
                    return false;
107
                }
108
109
                if (!$source->equals($definition->source[$key])) {
110
                    return false;
111
                }
112
            }
113
        }
114
115
        if (null !== $this->target) {
116
            if (count($this->target) !== count($definition->target)) {
117
                return false;
118
            }
119
120
            foreach ($this->target as $key => $target) {
121
                if (!isset($definition->target[$key])) {
122
                    return false;
123
                }
124
125
                if (!$target->equals($definition->target[$key])) {
126
                    return false;
127
                }
128
            }
129
        }
130
131
        return true;
132
    }
133
}
134