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

RecordTarget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\Participation;
28
29
use PHPHealth\CDA\Elements\AbstractElement;
30
use PHPHealth\CDA\RIM\Role\PatientRole;
31
32
/**
33
 *
34
 *
35
 * @author Julien Fastré <[email protected]>
36
 */
37
class RecordTarget extends AbstractElement
38
{
39
    /**
40
     *
41
     * @var PatientRole
42
     */
43
    protected $patientRole;
44
    
45
    public function __construct(PatientRole $patientRole)
46
    {
47
        $this->setPatientRole($patientRole);
48
    }
49
    
50
    public function getPatientRole()
51
    {
52
        return $this->patientRole;
53
    }
54
55
    public function setPatientRole(PatientRole $patientRole)
56
    {
57
        $this->patientRole = $patientRole;
58
        return $this;
59
    }
60
61
        
62
    protected function getElementTag()
63
    {
64
        return 'recordTarget';
65
    }
66
67
    public function toDOMElement(\DOMDocument $doc)
68
    {
69
        $el = $this->createElement($doc);
70
        
71
        $el->appendChild($this->patientRole->toDOMElement($doc));
72
        
73
        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...
74
    }
75
}
76