Completed
Push — master ( bf090e...257df1 )
by Julien
01:51
created

PatientRole::setPatient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Julien Fastré <[email protected]>.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace PHPHealth\CDA\RIM\Role;
28
29
use PHPHealth\CDA\Elements\AbstractElement;
30
use PHPHealth\CDA\DataType\Identifier\InstanceIdentifier;
31
use PHPHealth\CDA\DataType\Collection\Set;
32
use PHPHealth\CDA\RIM\Entity\Patient;
33
use PHPHealth\CDA\ClinicalDocument as CDA;
34
35
/**
36
 *
37
 *
38
 * @author Julien Fastré <[email protected]>
39
 */
40
class PatientRole extends AbstractElement
41
{
42
    /**
43
     *
44
     * @var Set
45
     */
46
    protected $patientIds;
47
    
48
    /**
49
     *
50
     * @var Patient
51
     */
52
    protected $patient;
53
    
54
    public function __construct(
55
        Set $ids,
56
        Patient $patient
57
    ) {
58
        $this->setPatientIds($ids);
59
        $this->setPatient($patient);
60
    }
61
    
62
    /**
63
     *
64
     * @return Set
65
     */
66
    public function getPatientIds()
67
    {
68
        return $this->patientIds;
69
    }
70
71
    /**
72
     *
73
     * @param Set $patientIds
74
     * @return $this
75
     */
76
    public function setPatientIds(Set $patientIds)
77
    {
78
        if ($patientIds->getElementName() !== InstanceIdentifier::class) {
79
            throw new \UnexpectedValueException("The values of patientsIds shoud"
80
                    . " be instances of ".InstanceIdentifier::class.", "
81
                    . "".$patientIds->getElementName()." given");
82
        }
83
        
84
        $this->patientIds = $patientIds;
85
        
86
        return $this;
87
    }
88
    
89
    public function addPatientId(InstanceIdentifier $ii)
90
    {
91
        $this->patientIds[] = $ii;
92
    }
93
94
    public function getPatient()
95
    {
96
        return $this->patient;
97
    }
98
99
    public function setPatient(Patient $patient)
100
    {
101
        $this->patient = $patient;
102
        return $this;
103
    }
104
105
            
106
    protected function getElementTag()
107
    {
108
        return 'patientRole';
109
    }
110
111
    public function toDOMElement(\DOMDocument $doc)
112
    {
113
        $el = $this->createElement($doc);
114
        
115
        foreach ($this->patientIds->get() as $ii) {
116
            $id = $doc->createElement(CDA::NS_CDA.'id');
117
            $ii->setValueToElement($id, $doc);
118
            $el->appendChild($id);
119
        }
120
        
121
        $el->appendChild($this->getPatient()->toDOMElement($doc));
122
        
123
        return $el;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $el; (PHPHealth\CDA\Elements\DOMElement) is incompatible with the return type declared by the interface PHPHealth\CDA\ElementInterface::toDOMElement of type DOMElement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
124
    }
125
}
126