NarrativeString::setValueToElement()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 12
nc 4
nop 2
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\DataType\TextAndMultimedia;
26
27
use PHPHealth\CDA\DataType\TextAndMultimedia\CharacterString;
28
use PHPHealth\CDA\Elements\Paragraph;
29
use PHPHealth\CDA\Elements\Table;
30
31
/**
32
 * A special character string which may contains narrative elements
33
 *
34
 * @author Julien Fastré <[email protected]>
35
 */
36
class NarrativeString extends CharacterString
37
{
38
    /**
39
     * All the elements collected with the differents `add*` functions.
40
     * This array is ordered.
41
     * 
42
     * @var \DOMElement[]
43
     */
44
    private $elements = array();
45
    
46
    public function __construct()
47
    {
48
        parent::__construct('');
49
    }
50
    
51
    
52
    private function addElement($type, $content)
53
    {
54
        $this->elements[] = [$type, $content];
55
    }
56
    
57
    /**
58
     * 
59
     * @param string $content
60
     * @return $this
61
     */
62
    public function addParagraph($content)
63
    {
64
        $this->addElement('paragraph', $content);
65
        
66
        return $this;
67
    }
68
    
69
    /**
70
     * 
71
     * @return Table
72
     */
73
    public function createTable()
74
    {
75
        $table = new Table($this);
76
        
77
        $this->addElement('table', $table);
78
        
79
        return $table;
80
    }
81
    
82
    public function setValueToElement(\DOMElement &$el, \DOMDocument $doc = null)
83
    {
84
        foreach ($this->elements as list($type, $element)) {
85
            switch ($type)
86
            {
87
                case 'table': 
88
                    $el->appendChild($element->toDOMElement($doc));
89
                    break;
90
                case 'paragraph':
91
                    $el->appendChild((new Paragraph($element))
92
                            ->toDOMElement($doc));
0 ignored issues
show
Bug introduced by
It seems like $doc defined by parameter $doc on line 82 can be null; however, PHPHealth\CDA\Elements\Paragraph::toDOMElement() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
93
                    break;
94
                default:
95
                    // this should not happen
96
                    throw new \LogicException();
97
            }
98
            
99
        }
100
    }
101
102
}
103