Completed
Push — master ( ccd20f...6ee4e9 )
by dan
02:09
created

SlackWebhookFormatter::format()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Formatter;
4
5
use IrishDan\NotificationBundle\Message\Message;
6
use IrishDan\NotificationBundle\Notification\NotificationInterface;
7
use IrishDan\NotificationBundle\Slackable;
8
use IrishDan\NotificationBundle\Textable;
9
10
class SlackWebhookFormatter implements MessageFormatterInterface
11
{
12
    public function format(NotificationInterface $notification)
13
    {
14
        $message = new Message();
15
        // $notificationData = $notification->getDataArray();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
17
        /** @var Slackable $notifiable */
18
        $notifiable = $notification->getNotifiable();
19
        if (!$notifiable instanceof Slackable) {
20
            throw new \RuntimeException('Notifiable mustimplement Slackable interface in order to send SMS');
21
        }
22
23
        // Build the dispatch data array.
24
        $dispatchData = [
25
            'webhook' => $notifiable->getSlackWebhook(),
26
        ];
27
28
        // Build the message data array.
29
        $messageData         = [];
30
        $messageData['body'] = 'A Hoi hoi! Mullin!';
31
32
        $message->setDispatchData($dispatchData);
33
        $message->setMessageData($messageData);
34
35
        return $message;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $message; (IrishDan\NotificationBundle\Message\Message) is incompatible with the return type declared by the interface IrishDan\NotificationBun...matterInterface::format of type Nexmo\Message\MessageInterface.

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...
36
    }
37
}