A few type signatures to make PHPStan happier
authorCameron Ball <cameron@cameron1729.xyz>
Fri, 28 Dec 2018 07:59:44 +0000 (15:59 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Fri, 4 Jan 2019 20:47:06 +0000 (04:47 +0800)
src/common.php

index 02bbbcf..668a11b 100644 (file)
@@ -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 '<pre>';
-    print_r($whatever);
-    echo '</pre>';
-}
-
-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) {