Skip to content

Commit d3f7d6f

Browse files
committed
Typehinting, Micro optimizations, cleanup and update phpdoc blocks.
1 parent 2429689 commit d3f7d6f

15 files changed

+236
-234
lines changed

.travis.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
language: php
22

33
php:
4-
- 5.6
5-
- 7.0
6-
- 7.1
4+
- 7.2
5+
- 7.3
76

87
env:
9-
matrix:
10-
- COMPOSER_FLAGS="--prefer-lowest"
11-
- COMPOSER_FLAGS=""
8+
matrix:
9+
- COMPOSER_FLAGS="--prefer-lowest"
10+
- COMPOSER_FLAGS=""
1211

1312
before_script:
14-
- travis_retry composer self-update
15-
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
13+
- travis_retry composer self-update
14+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
1615

1716
script:
18-
- phpunit --coverage-text --coverage-clover=coverage.clover
17+
- phpunit --coverage-text --coverage-clover=coverage.clover
1918

2019
after_script:
21-
- wget https://scrutinizer-ci.com/ocular.phar
22-
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
20+
- wget https://scrutinizer-ci.com/ocular.phar
21+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

src/Components/Button.php

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
namespace NotificationChannels\Facebook\Components;
44

5+
use JsonSerializable;
6+
use Illuminate\Support\Str;
57
use NotificationChannels\Facebook\Enums\ButtonType;
68
use NotificationChannels\Facebook\Exceptions\CouldNotCreateButton;
79

8-
class Button implements \JsonSerializable
10+
/**
11+
* Class Button
12+
*/
13+
class Button implements JsonSerializable
914
{
1015
/** @var string Button Title */
1116
protected $title;
@@ -22,25 +27,25 @@ class Button implements \JsonSerializable
2227
/**
2328
* Create a button.
2429
*
25-
* @param string $title
26-
* @param string|array $data
27-
* @param string $type
30+
* @param string $title
31+
* @param string|array $data
32+
* @param string $type
2833
*
2934
* @return static
3035
*/
31-
public static function create($title = '', $data = null, $type = ButtonType::WEB_URL)
36+
public static function create(string $title = '', $data = null, string $type = ButtonType::WEB_URL): Button
3237
{
3338
return new static($title, $data, $type);
3439
}
3540

3641
/**
3742
* Button Constructor.
3843
*
39-
* @param string $title
40-
* @param string|array $data
41-
* @param string $type
44+
* @param string $title
45+
* @param string|array $data
46+
* @param string $type
4247
*/
43-
public function __construct($title = '', $data = null, $type = ButtonType::WEB_URL)
48+
public function __construct(string $title = '', $data = null, string $type = ButtonType::WEB_URL)
4449
{
4550
$this->title = $title;
4651
$this->data = $data;
@@ -50,16 +55,18 @@ public function __construct($title = '', $data = null, $type = ButtonType::WEB_U
5055
/**
5156
* Set Button Title.
5257
*
53-
* @param $title
58+
* @param string $title
5459
*
55-
* @return $this
5660
* @throws CouldNotCreateButton
61+
* @return $this
5762
*/
58-
public function title($title)
63+
public function title(string $title): self
5964
{
6065
if ($this->isNotSetOrEmpty($title)) {
6166
throw CouldNotCreateButton::titleNotProvided();
62-
} elseif (mb_strlen($title) > 20) {
67+
}
68+
69+
if (mb_strlen($title) > 20) {
6370
throw CouldNotCreateButton::titleLimitExceeded($title);
6471
}
6572

@@ -71,16 +78,18 @@ public function title($title)
7178
/**
7279
* Set a URL for the button.
7380
*
74-
* @param $url
81+
* @param string $url
7582
*
76-
* @return $this
7783
* @throws CouldNotCreateButton
84+
* @return $this
7885
*/
79-
public function url($url)
86+
public function url(string $url): self
8087
{
8188
if ($this->isNotSetOrEmpty($url)) {
8289
throw CouldNotCreateButton::urlNotProvided();
83-
} elseif (! filter_var($url, FILTER_VALIDATE_URL)) {
90+
}
91+
92+
if (!filter_var($url, FILTER_VALIDATE_URL)) {
8493
throw CouldNotCreateButton::invalidUrlProvided($url);
8594
}
8695

@@ -91,16 +100,18 @@ public function url($url)
91100
}
92101

93102
/**
94-
* @param $phone
103+
* @param string $phone
95104
*
96-
* @return $this
97105
* @throws CouldNotCreateButton
106+
* @return $this
98107
*/
99-
public function phone($phone)
108+
public function phone(string $phone): self
100109
{
101110
if ($this->isNotSetOrEmpty($phone)) {
102111
throw CouldNotCreateButton::phoneNumberNotProvided();
103-
} elseif (is_string($phone) && ! starts_with($phone, '+')) {
112+
}
113+
114+
if (is_string($phone) && !Str::startsWith($phone, '+')) {
104115
throw CouldNotCreateButton::invalidPhoneNumberProvided($phone);
105116
}
106117

@@ -111,17 +122,15 @@ public function phone($phone)
111122
}
112123

113124
/**
114-
* @param $postback
125+
* @param array $postback
115126
*
116-
* @return $this
117127
* @throws CouldNotCreateButton
128+
* @return $this
118129
*/
119-
public function postback($postback)
130+
public function postback(array $postback): self
120131
{
121132
if ($this->isNotSetOrEmpty($postback)) {
122133
throw CouldNotCreateButton::postbackNotProvided();
123-
} elseif (! is_array($postback)) {
124-
throw CouldNotCreateButton::invalidPostbackProvided($postback);
125134
}
126135

127136
$this->payload['payload'] = json_encode($postback);
@@ -133,11 +142,11 @@ public function postback($postback)
133142
/**
134143
* Set Button Type.
135144
*
136-
* @param $type Possible Values: "web_url", "postback" or "phone_number". Default: "web_url"
145+
* @param string $type Possible Values: "web_url", "postback" or "phone_number". Default: "web_url"
137146
*
138147
* @return $this
139148
*/
140-
public function type($type)
149+
public function type(string $type): self
141150
{
142151
$this->payload['type'] = $type;
143152

@@ -149,7 +158,7 @@ public function type($type)
149158
*
150159
* @return $this
151160
*/
152-
public function isTypeWebUrl()
161+
public function isTypeWebUrl(): self
153162
{
154163
$this->payload['type'] = ButtonType::WEB_URL;
155164

@@ -161,7 +170,7 @@ public function isTypeWebUrl()
161170
*
162171
* @return $this
163172
*/
164-
public function isTypePostback()
173+
public function isTypePostback(): self
165174
{
166175
$this->payload['type'] = ButtonType::POSTBACK;
167176

@@ -173,7 +182,7 @@ public function isTypePostback()
173182
*
174183
* @return $this
175184
*/
176-
public function isTypePhoneNumber()
185+
public function isTypePhoneNumber(): self
177186
{
178187
$this->payload['type'] = ButtonType::PHONE_NUMBER;
179188

@@ -183,24 +192,24 @@ public function isTypePhoneNumber()
183192
/**
184193
* Determine Button Type.
185194
*
186-
* @param $type
195+
* @param string $type
187196
*
188197
* @return bool
189198
*/
190-
protected function isType($type)
199+
protected function isType(string $type): bool
191200
{
192201
return isset($this->payload['type']) && $type === $this->payload['type'];
193202
}
194203

195204
/**
196205
* Make payload by data and type.
197206
*
198-
* @param mixed $data
207+
* @param mixed $data
199208
*
200-
* @return $this
201209
* @throws CouldNotCreateButton
210+
* @return $this
202211
*/
203-
protected function makePayload($data)
212+
protected function makePayload($data): self
204213
{
205214
if ($this->isNotSetOrEmpty($data)) {
206215
return $this;
@@ -228,10 +237,10 @@ protected function makePayload($data)
228237
/**
229238
* Builds payload and returns an array.
230239
*
231-
* @return array
232240
* @throws CouldNotCreateButton
241+
* @return array
233242
*/
234-
public function toArray()
243+
public function toArray(): array
235244
{
236245
$this->title($this->title);
237246
$this->makePayload($this->data);
@@ -242,7 +251,8 @@ public function toArray()
242251
/**
243252
* Convert the object into something JSON serializable.
244253
*
245-
* @return array
254+
* @throws CouldNotCreateButton
255+
* @return mixed
246256
*/
247257
public function jsonSerialize()
248258
{
@@ -256,8 +266,8 @@ public function jsonSerialize()
256266
*
257267
* @return bool
258268
*/
259-
protected function isNotSetOrEmpty($var)
269+
protected function isNotSetOrEmpty($var): bool
260270
{
261-
return ! isset($var) || empty($var);
271+
return !isset($var) || empty($var);
262272
}
263273
}

0 commit comments

Comments
 (0)