From 285ecfa38b9e6e7101bad415fff439857c9c7eea Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Wed, 12 Feb 2020 19:15:58 -0500 Subject: [PATCH] refactor: count on null --- app/Console/Commands/GitUpdater.php | 2 +- app/Helpers/LanguageCensor.php | 2 +- app/Helpers/TorrentTools.php | 14 +++++++------- app/Http/Controllers/API/ChatController.php | 4 ++-- app/Http/Controllers/CommentController.php | 2 +- app/Http/Controllers/Staff/BackupController.php | 2 +- app/Repositories/ChatRepository.php | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/Console/Commands/GitUpdater.php b/app/Console/Commands/GitUpdater.php index fd4af088b8..8341bacec4 100644 --- a/app/Console/Commands/GitUpdater.php +++ b/app/Console/Commands/GitUpdater.php @@ -106,7 +106,7 @@ private function update() { $updating = $this->checkForUpdates(); - if (count($updating) > 0) { + if ((is_countable($updating) ? count($updating) : 0) > 0) { $this->alertDanger('Found Updates'); $this->cyan('Files that need updated:'); diff --git a/app/Helpers/LanguageCensor.php b/app/Helpers/LanguageCensor.php index 3d408df85a..38660ca782 100644 --- a/app/Helpers/LanguageCensor.php +++ b/app/Helpers/LanguageCensor.php @@ -63,7 +63,7 @@ public static function censor($source) $indexes = self::matchWordIndexes($source, $word); $ignore = 0; for ($i = 0; $i < $length; $i++) { - if (count($indexes) > 0 && $indexes[0] == $i) { + if ((is_countable($indexes) ? count($indexes) : 0) > 0 && $indexes[0] == $i) { $match = substr($source, $indexes[0], $word_length); $result .= "{$match}"; $ignore = $word_length - 1; diff --git a/app/Helpers/TorrentTools.php b/app/Helpers/TorrentTools.php index 83e702cec5..b186f628b2 100755 --- a/app/Helpers/TorrentTools.php +++ b/app/Helpers/TorrentTools.php @@ -72,8 +72,8 @@ public static function normalizeTorrent($torrentFile) public static function getFileCount($decodedTorrent) { // Multiple file torrent ? - if (array_key_exists('files', $decodedTorrent['info']) && count($decodedTorrent['info']['files'])) { - return count($decodedTorrent['info']['files']); + if (array_key_exists('files', $decodedTorrent['info']) && (is_countable($decodedTorrent['info']['files']) ? count($decodedTorrent['info']['files']) : 0)) { + return is_countable($decodedTorrent['info']['files']) ? count($decodedTorrent['info']['files']) : 0; } return 1; @@ -89,11 +89,11 @@ public static function getFileCount($decodedTorrent) public static function getTorrentSize($decodedTorrent) { $size = 0; - if (array_key_exists('files', $decodedTorrent['info']) && count($decodedTorrent['info']['files'])) { + if (array_key_exists('files', $decodedTorrent['info']) && (is_countable($decodedTorrent['info']['files']) ? count($decodedTorrent['info']['files']) : 0)) { foreach ($decodedTorrent['info']['files'] as $k => $file) { $dir = ''; $size += $file['length']; - $count = count($file['path']); + $count = is_countable($file['path']) ? count($file['path']) : 0; } } else { $size = $decodedTorrent['info']['length']; @@ -112,10 +112,10 @@ public static function getTorrentSize($decodedTorrent) */ public static function getTorrentFiles($decodedTorrent) { - if (array_key_exists('files', $decodedTorrent['info']) && count($decodedTorrent['info']['files'])) { + if (array_key_exists('files', $decodedTorrent['info']) && (is_countable($decodedTorrent['info']['files']) ? count($decodedTorrent['info']['files']) : 0)) { foreach ($decodedTorrent['info']['files'] as $k => $file) { $dir = ''; - $count = count($file['path']); + $count = is_countable($file['path']) ? count($file['path']) : 0; for ($i = 0; $i < $count; $i++) { if (($i + 1) == $count) { $fname = $dir.$file['path'][$i]; @@ -157,7 +157,7 @@ public static function getTorrentHash($decodedTorrent) public static function getTorrentFileCount($decodedTorrent) { if (array_key_exists('files', $decodedTorrent['info'])) { - return count($decodedTorrent['info']['files']); + return is_countable($decodedTorrent['info']['files']) ? count($decodedTorrent['info']['files']) : 0; } return 1; diff --git a/app/Http/Controllers/API/ChatController.php b/app/Http/Controllers/API/ChatController.php index 3ab380eae9..ac5920ee88 100644 --- a/app/Http/Controllers/API/ChatController.php +++ b/app/Http/Controllers/API/ChatController.php @@ -61,7 +61,7 @@ public function echoes() { $user = User::with(['echoes'])->findOrFail($this->auth->user()->id); - if (!$user->echoes || count($user->echoes->toArray()) < 1) { + if (!$user->echoes || (is_countable($user->echoes->toArray()) ? count($user->echoes->toArray()) : 0) < 1) { $echoes = new UserEcho(); $echoes->user_id = $this->auth->user()->id; $echoes->room_id = 1; @@ -76,7 +76,7 @@ public function audibles() { $user = User::with(['audibles'])->findOrFail($this->auth->user()->id); - if (!$user->audibles || count($user->audibles->toArray()) < 1) { + if (!$user->audibles || (is_countable($user->audibles->toArray()) ? count($user->audibles->toArray()) : 0) < 1) { $audibles = new UserAudible(); $audibles->user_id = $this->auth->user()->id; $audibles->room_id = 1; diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 215926cc1a..415adc8b1d 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -485,7 +485,7 @@ public function quickthanks(Request $request, $id) ]; } - $selected = mt_rand(0, count($thankArray) - 1); + $selected = mt_rand(0, (is_countable($thankArray) ? count($thankArray) : 0) - 1); $comment->content = $thankArray[$selected]; $comment->user_id = $user->id; $comment->torrent_id = $torrent->id; diff --git a/app/Http/Controllers/Staff/BackupController.php b/app/Http/Controllers/Staff/BackupController.php index b08a1f29c0..c6890bdc53 100644 --- a/app/Http/Controllers/Staff/BackupController.php +++ b/app/Http/Controllers/Staff/BackupController.php @@ -34,7 +34,7 @@ public function index(Request $request) $user = $request->user(); abort_unless($user->group->is_owner, 403); - if (!count(config('backup.backup.destination.disks'))) { + if (!(is_countable(config('backup.backup.destination.disks')) ? count(config('backup.backup.destination.disks')) : 0)) { dd(trans('backup.no_disks_configured')); } diff --git a/app/Repositories/ChatRepository.php b/app/Repositories/ChatRepository.php index d2b17d3ad8..1194f1bca8 100644 --- a/app/Repositories/ChatRepository.php +++ b/app/Repositories/ChatRepository.php @@ -287,7 +287,7 @@ public function checkMessageLimits($room_id) { $messages = $this->messages($room_id)->toArray(); $limit = config('chat.message_limit'); - $count = count($messages); + $count = is_countable($messages) ? count($messages) : 0; // Lets purge all old messages and keep the database to the limit settings if ($count > $limit) {