Completed
Pull Request — master (#95)
by
unknown
01:04
created

Link::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Spatie\CalendarLinks;
4
5
use DateTimeInterface;
6
use Spatie\CalendarLinks\Exceptions\InvalidLink;
7
use Spatie\CalendarLinks\Generators\Google;
8
use Spatie\CalendarLinks\Generators\Ics;
9
use Spatie\CalendarLinks\Generators\WebOutlook;
10
use Spatie\CalendarLinks\Generators\Office365;
11
use Spatie\CalendarLinks\Generators\Yahoo;
12
13
/**
14
 * @property-read string $title
15
 * @property-read DateTimeInterface $from
16
 * @property-read DateTimeInterface $to
17
 * @property-read string $description
18
 * @property-read string $address
19
 * @property-read bool $allDay
20
 */
21
class Link
22
{
23
    /** @var string */
24
    protected $title;
25
26
    /** @var \DateTime */
27
    protected $from;
28
29
    /** @var \DateTime */
30
    protected $to;
31
32
    /** @var string */
33
    protected $description;
34
35
    /** @var bool */
36
    protected $allDay;
37
38
    /** @var string */
39
    protected $address;
40
41
    public function __construct(string $title, DateTimeInterface $from, DateTimeInterface $to, bool $allDay = false)
42
    {
43
        $this->title = $title;
44
        $this->allDay = $allDay;
45
46
        if ($to < $from) {
47
            throw InvalidLink::invalidDateRange($from, $to);
48
        }
49
50
        $this->from = clone $from;
0 ignored issues
show
Documentation Bug introduced by
clone $from is of type object<DateTimeInterface>, but the property $from was declared to be of type object<DateTime>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
51
        $this->to = clone $to;
0 ignored issues
show
Documentation Bug introduced by
clone $to is of type object<DateTimeInterface>, but the property $to was declared to be of type object<DateTime>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
52
    }
53
54
    /**
55
     * @param string $title
56
     * @param \DateTimeInterface $from
57
     * @param \DateTimeInterface $to
58
     * @param bool $allDay
59
     *
60
     * @return static
61
     * @throws InvalidLink
62
     */
63
    public static function create(string $title, DateTimeInterface $from, DateTimeInterface $to, bool $allDay = false)
64
    {
65
        return new static($title, $from, $to, $allDay);
66
    }
67
68
    /**
69
     * @param string $title
70
     * @param DateTimeInterface $fromDate
71
     * @param int $numberOfDays
72
     *
73
     * @return Link
74
     * @throws InvalidLink
75
     */
76
    public static function createAllDay(string $title, DateTimeInterface $fromDate, int $numberOfDays = 1): self
77
    {
78
        $from = (clone $fromDate)->modify('midnight');
79
        $to = (clone $from)->modify("+$numberOfDays days");
80
81
        return new self($title, $from, $to, true);
82
    }
83
84
    /**
85
     * @param string $description
86
     *
87
     * @return $this
88
     */
89
    public function description(string $description)
90
    {
91
        $this->description = $description;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $address
98
     *
99
     * @return $this
100
     */
101
    public function address(string $address)
102
    {
103
        $this->address = $address;
104
105
        return $this;
106
    }
107
108
    public function formatWith(Generator $generator): string
109
    {
110
        return $generator->generate($this);
111
    }
112
113
    public function google(): string
114
    {
115
        return $this->formatWith(new Google());
116
    }
117
118
    public function ics(array $options = []): string
119
    {
120
        return $this->formatWith(new Ics($options));
121
    }
122
123
    public function yahoo(): string
124
    {
125
        return $this->formatWith(new Yahoo());
126
    }
127
128
    public function webOutlook(): string
129
    {
130
        return $this->formatWith(new WebOutlook());
131
    }
132
133
    public function office365(): string
134
    {
135
        return $this->formatWith(new Office365());
136
    }
137
138
    public function __get($property)
139
    {
140
        return $this->$property;
141
    }
142
}
143