From e76a0234e37a72486d431476a04815f08dbad320 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 28 Dec 2018 15:59:44 +0800 Subject: [PATCH] A few type signatures to make PHPStan happier --- src/common.php | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/common.php b/src/common.php index 02bbbcf..668a11b 100644 --- a/src/common.php +++ b/src/common.php @@ -3,14 +3,15 @@ require_once(__DIR__ . '/config.php'); require_once(AUTOLOAD_PATH); -use Telegram\Bot\Api; +use Telegram\Bot\Api as TelegramAPI; +use Telegram\Bot\Objects\Update as TelegramUpdate; -function getTelegram(): \Telegram\Bot\Api { +function getTelegram(): TelegramAPI { STATIC $tg; - return $tg = $tg ?? new \Telegram\Bot\Api(BOT_TOKEN); + return $tg = $tg ?? new TelegramAPI(BOT_TOKEN); } -function splitBill($amount) { +function splitBill(float $amount) : float { return floor($amount/2); } @@ -19,44 +20,37 @@ function identity($x) { } const notEmpty = 'notEmpty'; -function notEmpty($value) { +function notEmpty($value) : bool { return !empty($value); } -function getMessageSender($update) { +function getMessageSender(TelegramUpdate $update) : string { return PARTICIPANT_IDS[getMessageSenderId($update)]; } -function getMessageSenderId($update) { +function getMessageSenderId(TelegramUpdate $update) : int { return $update->get('message')->get('from')->get('id'); } -function getMessageSenderDisplayName($update) { +function getMessageSenderDisplayName(TelegramUpdate $update) : int { return $update->get('message')->get('from')->get('first_name'); } -function canChatWith($update) { +function canChatWith(TelegramUpdate $update) : bool { return in_array($update->get('message')->get('from')->get('id'), array_keys(PARTICIPANT_IDS)); } -function debug($whatever) { - echo '
';
-    print_r($whatever);
-    echo '
'; -} - -function partition(int $numPartitions, $array) { +function partition(int $numPartitions, array $array) : array { $partitionSize = (int)ceil(count($array) / $numPartitions); - return - array_values(filter(notEmpty)( - map(function($p) use ($array, $partitionSize) { - return array_slice($array, $p*$partitionSize, $partitionSize); - })(range(0, $numPartitions-1)) - )); + return array_values(filter(notEmpty)( + map(function($p) use ($array, $partitionSize) { + return array_slice($array, $p*$partitionSize, $partitionSize); + })(range(0, $numPartitions-1)) + )); } -function getInbox($inbox) { +function getInbox(string $inbox) { STATIC $inboxes; if (!isset($inboxes[$inbox])) { @@ -230,7 +224,7 @@ function ∪($a, $b) { } function getSeason(int $monthNum) { - return ['summer', 'autumn', 'winter', 'spring'][floor(($monthNum%12)/3)]; + return ['summer', 'autumn', 'winter', 'spring'][(int)floor(($monthNum%12)/3)]; } function getMonthName($monthNum) { -- 2.11.0