GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( df2670...912221 )
by Gallice
03:05
created

WebUrl::setMessengerExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tgallice\FBMessenger\Model\Button;
4
5
use Tgallice\FBMessenger\Model\Button;
6
7
class WebUrl extends Button
8
{
9
    const HEIGHT_RATIO_FULL = 'full';
10
    const HEIGHT_RATIO_COMPACT = 'compact';
11
    const HEIGHT_RATIO_TALL = 'tall';
12
13
    /**
14
     * @var string
15
     */
16
    private $title;
17
18
    /**
19
     * @var string
20
     */
21
    private $url;
22
23
    /**
24
     * @var string
25
     */
26
    private $webviewHeightRatio = self::HEIGHT_RATIO_FULL;
27
28
    /**
29
     * @var bool
30
     */
31
    private $messengerExtensions = false;
32
33
    /**
34
     * @var string|null
35
     */
36
    public $fallbackUrl;
37
38
    /**
39
     * @param string $title
40
     * @param string $url
41
     */
42 15
    public function __construct($title, $url)
43
    {
44 15
        parent::__construct(Button::TYPE_WEB_URL);
45
46 15
        self::validateTitleSize($title);
47 14
        $this->title = $title;
48 14
        $this->url = $url;
49 14
    }
50
51
    /**
52
     * @return string
53
     */
54 1
    public function getTitle()
55
    {
56 1
        return $this->title;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 1
    public function getUrl()
63
    {
64 1
        return $this->url;
65
    }
66
67
    /**
68
     * @param string $ratio
69
     */
70 2
    public function setWebviewHeightRatio($ratio)
71
    {
72 2
        if (!in_array($ratio, $this->getAllowedHeights())) {
73 1
            throw new \InvalidArgumentException(sprintf('Webview height ratio must be one of this values: [%s]', implode(', ', $this->getAllowedHeights())));
74
        }
75
76 1
        $this->webviewHeightRatio = $ratio;
77 1
    }
78
79
    /**
80
     * @param bool $messengerExtensions
81
     */
82 2
    public function setMessengerExtensions($messengerExtensions)
83
    {
84 2
        $this->messengerExtensions = $messengerExtensions;
85 2
    }
86
87
    /**
88
     * @param string $fallbackUrl
89
     */
90 2
    public function setFallbackUrl($fallbackUrl)
91
    {
92 2
        $this->fallbackUrl = $fallbackUrl;
93 2
    }
94
95
    /**
96
     * @return string|null
97
     */
98 2
    public function getWebviewHeightRatio()
99
    {
100 2
        return $this->webviewHeightRatio;
101
    }
102
103
    /**
104
     * @return string|null
105
     */
106 2
    public function getFallbackUrl()
107
    {
108 2
        return $this->fallbackUrl;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114 2
    public function useMessengerExtensions()
115
    {
116 2
        return $this->messengerExtensions;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122 2
    public function jsonSerialize()
123
    {
124 2
        $json = parent::jsonSerialize();
125
126 2
        $json['title'] = $this->title;
127 2
        $json['url'] = $this->url;
128
129 2
        if (!empty($this->webviewHeightRatio)) {
130 2
            $json['webview_height_ratio'] = $this->webviewHeightRatio;
131 2
        }
132
133 2
        if ($this->messengerExtensions) {
134 1
            $json['messenger_extensions'] = $this->messengerExtensions;
135 1
        }
136
137 2
        if (!empty($this->fallbackUrl)) {
138 1
            $json['fallback_url'] = $this->fallbackUrl;
139 1
        }
140
141 2
        return $json;
0 ignored issues
show
Best Practice introduced by
The expression return $json; seems to be an array, but some of its elements' types (boolean) are incompatible with the return type of the parent method Tgallice\FBMessenger\Model\Button::jsonSerialize of type array<string,string>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
142
    }
143
144
    /**
145
     * @return string[]
146
     */
147 2
    private function getAllowedHeights()
148
    {
149
        return [
150 2
            self::HEIGHT_RATIO_FULL,
151 2
            self::HEIGHT_RATIO_COMPACT,
152
            self::HEIGHT_RATIO_TALL
153 2
        ];
154
    }
155
}
156