Section   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 214
Duplicated Lines 9.35 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 1
cbo 7
dl 20
loc 214
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getElementTag() 0 4 1
A getClassCode() 0 4 1
A getId() 0 4 1
A setId() 0 6 1
A getCode() 0 4 1
A setCode() 0 5 1
A getText() 0 4 1
A setText() 0 5 1
A setTemplateIds() 20 20 3
A addTemplateId() 0 6 1
A getTemplateIds() 0 4 1
A getTitle() 0 4 1
A setTitle() 0 6 1
A createEntry() 0 8 1
A getEntries() 0 4 1
A addEntry() 0 4 1
C toDOMElement() 0 42 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * The MIT License
4
 *
5
 * Copyright 2017 Julien Fastré <[email protected]>.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
namespace PHPHealth\CDA\Component\SingleComponent;
26
27
use PHPHealth\CDA\Elements\AbstractElement;
28
use PHPHealth\CDA\HasClassCode;
29
use PHPHealth\CDA\Elements\TemplateId;
30
use PHPHealth\CDA\DataType\Identifier\InstanceIdentifier;
31
use PHPHealth\CDA\Elements\Id;
32
use PHPHealth\CDA\Elements\Title;
33
use PHPHealth\CDA\DataType\Code\CodedWithEquivalents;
34
use PHPHealth\CDA\Elements\Code;
35
use PHPHealth\CDA\DataType\TextAndMultimedia\CharacterString;
36
use PHPHealth\CDA\Elements\Text;
37
use PHPHealth\CDA\Elements\Entry;
38
39
/**
40
 * 
41
 * Single section which will be included in component
42
 *
43
 * @author Julien Fastré <[email protected]>
44
 */
45
class Section extends AbstractElement implements HasClassCode
46
{
47
    /**
48
     *
49
     * @var InstanceIdentifier
50
     */
51
    private $id;
52
    
53
    /**
54
     *
55
     * @var CodedWithEquivalents
56
     */
57
    private $code;
58
    
59
    /**
60
     * 
61
     * @var CharacterString
62
     */
63
    private $text;
64
    
65
    /**
66
     *
67
     * @var InstanceIdentifier[]
68
     */
69
    private $templateIds = array();
70
    
71
    /**
72
     *
73
     * @var CharacterString
74
     */
75
    private $title;
76
    
77
    /**
78
     *
79
     * @var Entry[]
80
     */
81
    private $entries = array();
82
    
83
    protected function getElementTag(): string
84
    {
85
        return 'section';
86
    }
87
88
    public function getClassCode(): string
89
    {
90
        return 'DOCSECT';
91
    }
92
    
93
    /**
94
     * 
95
     * @return InstanceIdentifier
96
     */
97
    public function getId()
98
    {
99
        return $this->id;
100
    }
101
102
    public function setId(InstanceIdentifier $id)
103
    {
104
        $this->id = $id;
105
        
106
        return $this;
107
    }
108
109
    public function getCode(): CodedWithEquivalents
110
    {
111
        return $this->code;
112
    }
113
114
    public function setCode(CodedWithEquivalents $code)
115
    {
116
        $this->code = $code;
117
        return $this;
118
    }
119
    
120
    public function getText(): CharacterString
121
    {
122
        return $this->text;
123
    }
124
125
    public function setText(CharacterString $text)
126
    {
127
        $this->text = $text;
128
        return $this;
129
    }
130
    
131 View Code Duplication
    public function setTemplateIds(array $templateIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $validation = \array_reduce($templateIds, 
134
            function ($carry, $item) {
135
                if ($carry === false) {
136
                    return false;
137
                }
138
                
139
                return $item instanceof InstanceIdentifier;
140
            });
141
        
142
        if ($validation === false) {
143
            throw new \UnexpectedValueException(sprintf("The values of templateIds"
144
                . " must contains only %s", InstanceIdentifier::class));
145
        }
146
        
147
        $this->templateIds = $templateIds;
148
        
149
        return $this;
150
    }
151
    
152
    public function addTemplateId(InstanceIdentifier $templateId)
153
    {
154
        $this->templateIds[] = $templateId;
155
        
156
        return $this;
157
    }
158
159
            
160
    /**
161
     * the code for the current section
162
     * 
163
     * @return InstanceIdentifier[]
164
     */
165
    public function getTemplateIds()
166
    {
167
        return $this->templateIds;
168
    }
169
    
170
    /**
171
     * The title for the section
172
     * 
173
     * @return CharacterString
174
     */
175
    public function getTitle()
176
    {
177
        return $this->title;
178
    }
179
    
180
    public function setTitle(CharacterString $title)
181
    {
182
        $this->title = $title;
183
        
184
        return $this;
185
    }
186
    
187
    /**
188
     * create an entry, which is already bound to the current section
189
     * 
190
     * @return Entry
191
     */
192
    public function createEntry(): Entry
193
    {
194
        $entry = new Entry();
195
        
196
        $this->addEntry($entry);
197
        
198
        return $entry;
199
    }
200
    
201
    function getEntries(): array
202
    {
203
        return $this->entries;
204
    }
205
    
206
    public function addEntry(Entry $entry)
207
    {
208
        $this->entries[] = $entry;
209
    }
210
211
    
212
    /**
213
     * 
214
     * @param \DOMDocument $doc
215
     */
216
    public function toDOMElement(\DOMDocument $doc): \DOMElement
217
    {
218
        $el = $this->createElement($doc);
219
        // append templateId
220
        if ($this->getTemplateIds() !== NULL) {
221
            foreach ($this->getTemplateIds() as $id) {
222
                $el->appendChild(
223
                    (new TemplateId($id))->toDOMElement($doc)
224
                    );
225
            }
226
        }
227
        // append id
228
        if ($this->getId() !== NULL) {
229
            $el->appendChild(
230
                (new Id($this->getId()))->toDOMElement($doc)
231
                );
232
        }
233
        // append code
234
        if ($this->getCode() !== NULL) {
235
            $el->appendChild(
236
                (new Code($this->getCode()))->toDOMElement($doc)
0 ignored issues
show
Compatibility introduced by
$this->getCode() of type object<PHPHealth\CDA\Dat...e\CodedWithEquivalents> is not a sub-type of object<PHPHealth\CDA\DataType\Code\CodedValue>. It seems like you assume a child class of the class PHPHealth\CDA\DataType\Code\CodedWithEquivalents to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
237
                );
238
        }
239
        // append title
240
        if (! empty($this->getTitle())) {
241
            $el->appendChild(
242
                (new Title($this->getTitle()))->toDOMElement($doc)
243
                );
244
        }
245
        // append text
246
        if (! empty($this->getText())) {
247
            $el->appendChild(
248
                (new Text($this->getText()))->toDOMElement($doc)
249
                );
250
        }
251
        
252
        foreach ($this->getEntries() as $entry) {
253
            $el->appendChild($entry->toDOMElement($doc));
254
        }
255
        
256
        return $el;
257
    }
258
}
259