Skip to content

Commit 9e64095

Browse files
committed
Stricter typing and minor comment updates
1 parent 1d9e976 commit 9e64095

12 files changed

+29
-36
lines changed

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ the [Telegram Bot API](https://core.telegram.org/bots/api).
7070
You can create a keyboard by calling the static `make()` method on its class.
7171

7272
After that you can chain methods to set additional fields that are available in the Bot API. This is done by calling the
73-
field name in camelCase. So instead of input_field_placeholder, you need to call `inputFieldPlaceholder()`.
73+
field name in camelCase. So instead of `input_field_placeholder`, you need to call `inputFieldPlaceholder()`.
7474

7575
```php
7676
ReplyKeyboardMarkup::make()
@@ -85,14 +85,14 @@ The Buttons are created in the same way:
8585

8686
```php
8787
KeyboardButton::make()
88-
->text('Text of Button')
88+
->text('Send my Contact')
8989
->requestContact();
9090
```
9191

9292
As a shortcut, you can pass the mandatory `text` field as an argument to the static method `make()` like this:
9393

9494
```php
95-
KeyboardButton::make('Text of Button')
95+
KeyboardButton::make('Send my Location')
9696
->requestLocation();
9797
```
9898

@@ -169,9 +169,7 @@ If you want to add a bunch of buttons that have each a row for themselves you ca
169169
```php
170170
InlineKeyboardMarkup::make()
171171
->stack([
172-
InlineKeyboardButton::make('Login')->loginUrl([
173-
'url' => 'https://example.com/login'
174-
]),
172+
InlineKeyboardButton::make('Login')->loginUrl('https://example.com/login'),
175173
InlineKeyboardButton::make('Visit Homepage')->url('https://example.com')
176174
]);
177175
```

composer.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
"ext-json": "*"
2828
},
2929
"scripts": {
30-
"test": [
31-
"vendor/bin/pest"
32-
]
30+
"test": "vendor/bin/pest"
3331
},
3432
"config": {
3533
"allow-plugins": {

src/FluentEntity.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
abstract class FluentEntity implements \JsonSerializable
66
{
7-
/**
8-
* @var array $data
9-
*/
10-
protected $data = [];
117

12-
protected $defaults = [];
8+
protected array $data = [];
9+
10+
protected array $defaults = [];
1311

1412
public function __construct(array $data = [])
1513
{
@@ -30,7 +28,7 @@ public function __call($name, $arguments): self
3028
return $this;
3129
}
3230

33-
public function jsonSerialize(): mixed
31+
public function jsonSerialize(): object
3432
{
3533
return (object) $this->data;
3634
}

src/ForceReply.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
class ForceReply extends FluentEntity
1010
{
1111

12-
protected $data = [
12+
protected array $data = [
1313
'force_reply' => true,
1414
];
1515

16-
protected $defaults = [
16+
protected array $defaults = [
1717
'selective' => true,
1818
];
1919

src/InlineKeyboard/InlineKeyboardButton.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
class InlineKeyboardButton extends Button
1717
{
1818

19-
protected $defaults = [
19+
protected array $defaults = [
2020
'pay' => true,
2121
];
2222

2323
public static function make(string $text = null): static
2424
{
2525
$data = [];
2626

27-
if ($text) {
27+
if ($text !== null) {
2828
$data['text'] = $text;
2929
}
3030

@@ -33,7 +33,7 @@ public static function make(string $text = null): static
3333

3434
public function loginUrl(array|string $login_url): self
3535
{
36-
if (! is_array($login_url)) {
36+
if (is_string($login_url)) {
3737
$login_url = [
3838
'url' => $login_url
3939
];

src/InlineKeyboard/InlineKeyboardMarkup.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
class InlineKeyboardMarkup extends KeyboardMarkup
99
{
1010

11-
protected static $keyboardFieldName = 'inline_keyboard';
11+
protected static string $keyboardFieldName = 'inline_keyboard';
1212

1313
}

src/KeyboardMarkup.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ abstract class KeyboardMarkup extends FluentEntity
1010
*
1111
* @var string
1212
*/
13-
protected static $keyboardFieldName = 'keyboard';
13+
protected static string $keyboardFieldName = 'keyboard';
1414

15-
protected $currentRowIndex = 0;
15+
protected int $currentRowIndex = 0;
1616

1717
public function __construct(array $data = [])
1818
{
@@ -32,13 +32,13 @@ public function row(array $buttons = []): self
3232
{
3333
$keyboard = &$this->data[static::$keyboardFieldName];
3434

35-
// Last row is not empty
35+
// Last row is not empty, add new row
3636
if (! empty($keyboard[$this->currentRowIndex])) {
3737
$keyboard[] = [];
3838
$this->currentRowIndex++;
3939
}
4040

41-
// argument is not empty
41+
// Buttons have been passed, add them
4242
if (! empty($buttons)) {
4343
$keyboard[$this->currentRowIndex] = $buttons;
4444
$this->currentRowIndex++;
@@ -48,14 +48,13 @@ public function row(array $buttons = []): self
4848
}
4949

5050
/**
51-
* Adds buttons one per row to the keyboard.
51+
* Adds buttons to the keyboard, one per row.
5252
*
5353
* @param Button[] $buttons
5454
* @return $this
5555
*/
5656
public function stack(array $buttons): self
5757
{
58-
// Every button gets its own row
5958
foreach ($buttons as $button) {
6059
$this->row([$button]);
6160
}

src/ReplyKeyboard/KeyboardButton.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
*/
1313
class KeyboardButton extends Button
1414
{
15-
protected $defaults = [
15+
16+
protected array $defaults = [
1617
'request_contact' => true,
1718
'request_location' => true,
1819
'request_poll' => [],
@@ -22,7 +23,7 @@ public static function make(string $text = null): static
2223
{
2324
$data = [];
2425

25-
if ($text) {
26+
if ($text !== null) {
2627
$data['text'] = $text;
2728
}
2829

src/ReplyKeyboard/KeyboardButtonPollType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class KeyboardButtonPollType extends Button
1111
{
1212

13-
protected $data = [];
13+
protected array $data = [];
1414

1515
public static function any(): static
1616
{

src/ReplyKeyboard/ReplyKeyboardMarkup.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
class ReplyKeyboardMarkup extends KeyboardMarkup
1515
{
1616

17-
protected static $keyboardFieldName = 'keyboard';
17+
protected static string $keyboardFieldName = 'keyboard';
1818

19-
protected $defaults = [
19+
protected array $defaults = [
2020
'resize_keyboard' => true,
2121
'one_time_keyboard' => true,
2222
'selective' => true,
2323
];
2424

25-
2625
}

src/ReplyKeyboardRemove.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
class ReplyKeyboardRemove extends FluentEntity
99
{
1010

11-
protected $data = [
11+
protected array $data = [
1212
'remove_keyboard' => true,
1313
];
1414

15-
protected $defaults = [
15+
protected array $defaults = [
1616
'selective' => true,
1717
];
1818

src/helpers.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PhpTelegramBot\FluentKeyboard;
44

55
if (! function_exists('snake_case')) {
6-
function snake_case($value)
6+
function snake_case($value): string
77
{
88
return strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1_', $value));
99
}

0 commit comments

Comments
 (0)