Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
13 | class UptimeCheckSucceeded extends BaseNotification |
||
14 | { |
||
15 | public MonitorSucceededEvent $event; |
||
|
|||
16 | |||
17 | /** |
||
18 | * Get the mail representation of the notification. |
||
19 | * |
||
20 | * @param mixed $notifiable |
||
21 | * @return \Illuminate\Notifications\Messages\MailMessage |
||
22 | */ |
||
23 | public function toMail($notifiable) |
||
24 | { |
||
25 | $mailMessage = (new MailMessage) |
||
26 | ->subject($this->getMessageText()) |
||
27 | ->line($this->getMessageText()) |
||
28 | ->line($this->getLocationDescription()); |
||
29 | |||
30 | foreach ($this->getMonitorProperties() as $name => $value) { |
||
31 | $mailMessage->line($name.': '.$value); |
||
32 | } |
||
33 | |||
34 | return $mailMessage; |
||
35 | } |
||
36 | |||
37 | public function toSlack($notifiable) |
||
38 | { |
||
39 | return (new SlackMessage) |
||
40 | ->attachment(function (SlackAttachment $attachment) { |
||
41 | $attachment |
||
42 | ->title($this->getMessageText()) |
||
43 | ->fallback($this->getMessageText()) |
||
44 | ->footer($this->getLocationDescription()) |
||
45 | ->timestamp(Carbon::now()); |
||
46 | }); |
||
47 | } |
||
48 | |||
49 | public function isStillRelevant(): bool |
||
50 | { |
||
51 | return $this->getMonitor()->uptime_status != UptimeStatus::DOWN; |
||
52 | } |
||
53 | |||
54 | public function setEvent(MonitorSucceededEvent $event): self |
||
55 | { |
||
56 | $this->event = $event; |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | public function getMessageText(): string |
||
62 | { |
||
63 | return "{$this->getMonitor()->url} is up"; |
||
64 | } |
||
65 | } |
||
66 |