Attendees::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal\Property\Event;
13
14
use Eluceo\iCal\Property;
15
16
class Attendees extends Property
17
{
18
    /**
19
     * @var Property[]
20
     */
21
    protected $attendees = [];
22
23 33
    public function __construct()
24
    {
25 33
        $this->name = 'ATTENDEES';
26
        // prevent super constructor to be called
27 33
    }
28
29
    /**
30
     * @param       $value
31
     * @param array $params
32
     *
33
     * @return $this
34
     */
35 1
    public function add($value, $params = [])
36
    {
37 1
        $this->attendees[] = new Property('ATTENDEE', $value, $params);
38
39 1
        return $this;
40
    }
41
42
    /**
43
     * @param Property[] $value
44
     *
45
     * @return $this
46
     */
47 1
    public function setValue($value)
48
    {
49 1
        $this->attendees = $value;
50
51 1
        return $this;
52
    }
53
54
    /**
55
     * @return Property[]
56
     */
57 2
    public function getValue()
58
    {
59 2
        return $this->attendees;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->attendees; (Eluceo\iCal\Property[]) is incompatible with the return type of the parent method Eluceo\iCal\Property::getValue of type Eluceo\iCal\Property\ValueInterface.

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...
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 8
    public function toLines()
66
    {
67 8
        $lines = [];
68 8
        foreach ($this->attendees as $attendee) {
69 1
            $lines[] = $attendee->toLine();
70
        }
71
72 8
        return $lines;
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param mixed  $value
78
     *
79
     * @throws \BadMethodCallException
80
     */
81 1
    public function setParam($name, $value)
82
    {
83 1
        throw new \BadMethodCallException('Cannot call setParam on Attendees Property');
84
    }
85
86
    /**
87
     * @param $name
88
     *
89
     * @throws \BadMethodCallException
90
     */
91 1
    public function getParam($name)
92
    {
93 1
        throw new \BadMethodCallException('Cannot call getParam on Attendees Property');
94
    }
95
}
96