EffectiveTime   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 2
cbo 1
dl 0
loc 64
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 4 1
A setValue() 0 17 4
A setOperatorAppend() 0 4 1
A toDOMElement() 0 10 2
A getElementTag() 0 4 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\Elements;
28
29
use PHPHealth\CDA\DataType\Quantity\DateAndTime\TimeStamp;
30
use PHPHealth\CDA\DataType\Collection\Interval\PeriodicIntervalOfTime;
31
use PHPHealth\CDA\DataType\Collection\Interval\IntervalOfTime;
32
use PHPHealth\CDA\ClinicalDocument as CDA;
33
34
/**
35
 *
36
 *
37
 * @author Julien Fastré <[email protected]>
38
 */
39
class EffectiveTime extends AbstractElement
40
{
41
    /**
42
     *
43
     * @var TimeStamp|PeriodicIntervalOfTime
44
     */
45
    protected $value;
46
    
47
    /**
48
     *
49
     * @var string
50
     */
51
    protected $operator = '';
52
    
53
    public function __construct($value)
54
    {
55
        $this->setValue($value);
56
    }
57
    
58
    
59
    public function getValue()
60
    {
61
        return $this->value;
62
    }
63
64
    public function setValue($value)
65
    {
66
        if ($value instanceof PeriodicIntervalOfTime 
67
            ||
68
            $value instanceof TimeStamp
69
            ||
70
            $value instanceof IntervalOfTime
71
            ) {
72
            $this->value = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value can also be of type object<PHPHealth\CDA\Dat...nterval\IntervalOfTime>. However, the property $value is declared as type object<PHPHealth\CDA\Dat...PeriodicIntervalOfTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
73
        } else {
74
            throw new \UnexpectedValueException(sprintf("The timestamp must "
75
                . "implements %s, %s or %s", PeriodicIntervalOfTime::class, 
76
                TimeStamp::class, IntervalOfTime::class));
77
        }
78
        
79
        return $this;
80
    }
81
    
82
    public function setOperatorAppend()
83
    {
84
        $this->operator = 'A';
85
    }
86
87
    public function toDOMElement(\DOMDocument $doc)
88
    {
89
        $el = $this->createElement($doc, ['value']);
90
        
91
        if ($this->operator === 'A') {
92
            $el->setAttribute(CDA::NS_CDA.'operator', 'A');
93
        }
94
        
95
        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...
96
    }
97
98
    protected function getElementTag()
99
    {
100
        return 'effectiveTime';
101
    }
102
}
103