From 619ac1b15cb6f36c6814c01748b332862e94c61d Mon Sep 17 00:00:00 2001 From: Pierre-Henry Soria Date: Mon, 27 Jun 2022 19:41:14 +0930 Subject: [PATCH] Fixing incorrect datatypes and type incompatibilities --- .../classes/ProfileBaseController.php | 20 +++------ .../app/system/core/models/UserCoreModel.php | 3 +- .../controllers/MainController.php | 6 +-- .../mail/controllers/MainController.php | 31 ++++++------- .../system/modules/mail/models/MailModel.php | 8 +--- .../user/controllers/BrowseController.php | 12 +++-- .../user/controllers/ProfileController.php | 44 +++++-------------- _protected/framework/Mvc/Router/Uri.class.php | 4 +- .../framework/Navigation/Page.class.php | 6 +-- 9 files changed, 48 insertions(+), 86 deletions(-) diff --git a/_protected/app/includes/classes/ProfileBaseController.php b/_protected/app/includes/classes/ProfileBaseController.php index d5233fb178..f325bdd460 100644 --- a/_protected/app/includes/classes/ProfileBaseController.php +++ b/_protected/app/includes/classes/ProfileBaseController.php @@ -50,10 +50,8 @@ abstract public function index(): void; /** * Add CSS files for the profile page's design. - * - * @return void */ - abstract protected function addCssFiles(); + abstract protected function addCssFiles(): void; public function __construct() { @@ -90,18 +88,12 @@ protected function setVisitorId($iVisitorId): void $this->iVisitorId = (int)$iVisitorId; } - /** - * @return int - */ - public function getProfileId() + public function getProfileId(): int { return $this->iProfileId; } - /** - * @return int - */ - public function getVisitorId() + public function getVisitorId(): int { return $this->iVisitorId; } @@ -109,8 +101,6 @@ public function getVisitorId() /** * Privacy Profile. * - * @param stdClass $oUser - * * @throws Framework\File\IOException */ protected function initPrivacy(stdClass $oUser): void @@ -158,7 +148,7 @@ protected function setMenuBar(string $sFirstName, stdClass $oUser): void /** * Set the Google Maps code to the view. */ - protected function setMap(string $sCity, string $sCountry, stdClass $oUser) + protected function setMap(string $sCity, string $sCountry, stdClass $oUser): void { $sFullAddress = $sCity . ' ' . t($sCountry); $sMarkerText = t('Meet %0% near here!', $oUser->username); @@ -253,7 +243,7 @@ protected function getMessengerLink($sFirstName, stdClass $oUser): string * * @return string The correct anchor "Manage Friend" link. */ - protected function getFriendLink(string $sFirstName, stdClass $oUser) + protected function getFriendLink(string $sFirstName, stdClass $oUser): string { $sCsrfToken = (new Token)->generate('friend'); diff --git a/_protected/app/system/core/models/UserCoreModel.php b/_protected/app/system/core/models/UserCoreModel.php index 82dcceb6a6..1e14474a7d 100755 --- a/_protected/app/system/core/models/UserCoreModel.php +++ b/_protected/app/system/core/models/UserCoreModel.php @@ -476,7 +476,8 @@ public function search(array $aParams, $bCount, $iOffset, $iLimit) $aRow = $rStmt->fetchAll(PDO::FETCH_OBJ); Db::free($rStmt); - return $aRow; + + return (array)$aRow; } /** diff --git a/_protected/app/system/modules/cool-profile-page/controllers/MainController.php b/_protected/app/system/modules/cool-profile-page/controllers/MainController.php index d99871f31a..79a0c149ee 100755 --- a/_protected/app/system/modules/cool-profile-page/controllers/MainController.php +++ b/_protected/app/system/modules/cool-profile-page/controllers/MainController.php @@ -28,7 +28,7 @@ public function __construct() $this->setVisitorId($this->session->get('member_id')); } - public function index() + public function index(): void { $this->addCssFiles(); $this->addAdditionalAssetFiles(); @@ -120,10 +120,8 @@ public function index() /** * Add the General and Tabs Menu stylesheets. - * - * @return void */ - protected function addCssFiles() + protected function addCssFiles(): void { $this->design->addCss( PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS, diff --git a/_protected/app/system/modules/mail/controllers/MainController.php b/_protected/app/system/modules/mail/controllers/MainController.php index 16d4ba9bc9..127bd5f541 100755 --- a/_protected/app/system/modules/mail/controllers/MainController.php +++ b/_protected/app/system/modules/mail/controllers/MainController.php @@ -120,7 +120,7 @@ public function inbox() self::EMAILS_PER_PAGE ); $this->view->current_page = $this->oPage->getCurrentPage(); - $oMail = $this->oMailModel->search( + $aMail = $this->oMailModel->search( null, false, SearchCoreModel::SEND_DATE, @@ -131,13 +131,13 @@ public function inbox() MailModel::INBOX ); - if (empty($oMail)) { + if (empty($aMail)) { $this->sTitle = t('No messages in your inbox'); $this->notFound(); // We modify the default error message $this->view->error = t("You don't have any new messages ☹ Go speak with others!", Uri::get('user', 'browse', 'index')); } else { - $this->view->msgs = $oMail; + $this->view->msgs = $aMail; } $this->manualTplInclude('msglist.inc.tpl'); @@ -180,7 +180,7 @@ public function outbox() self::EMAILS_PER_PAGE ); $this->view->current_page = $this->oPage->getCurrentPage(); - $oMail = $this->oMailModel->search( + $aMail = $this->oMailModel->search( null, false, SearchCoreModel::SEND_DATE, @@ -191,13 +191,13 @@ public function outbox() MailModel::OUTBOX ); - if (empty($oMail)) { + if (empty($aMail)) { $this->sTitle = t('Not Found!'); $this->notFound(); // We modify the default error message $this->view->error = t('No messages found.'); } else { - $this->view->msgs = $oMail; + $this->view->msgs = $aMail; } $this->manualTplInclude('msglist.inc.tpl'); @@ -242,7 +242,7 @@ public function trash() self::EMAILS_PER_PAGE ); $this->view->current_page = $this->oPage->getCurrentPage(); - $oMail = $this->oMailModel->search( + $aMail = $this->oMailModel->search( null, false, SearchCoreModel::SEND_DATE, @@ -253,13 +253,13 @@ public function trash() MailModel::TRASH ); - if (empty($oMail)) { + if (empty($aMail)) { $this->sTitle = t('Not Found!'); $this->notFound(); // We modify the default 404 error message $this->view->error = t('Trash is empty!'); } else { - $this->view->msgs = $oMail; + $this->view->msgs = $aMail; } $this->manualTplInclude('msglist.inc.tpl'); @@ -298,7 +298,7 @@ public function result() self::EMAILS_PER_PAGE ); $this->view->current_page = $this->oPage->getCurrentPage(); - $oSearch = $this->oMailModel->search( + $aSearch = $this->oMailModel->search( $sKeywords, false, $sOrder, @@ -309,7 +309,7 @@ public function result() $iType ); - if (empty($oSearch)) { + if (empty($aSearch)) { $this->sTitle = t("Your search didn't match any of your messages."); $this->notFound(); } else { @@ -317,7 +317,7 @@ public function result() $this->view->page_title = $this->sTitle; $this->view->h2_title = $this->sTitle; $this->view->h3_title = nt('%n% message found!', '%n% messages found!', $this->iTotalMails); - $this->view->msgs = $oSearch; + $this->view->msgs = $aSearch; } $this->manualTplInclude('msglist.inc.tpl'); @@ -525,12 +525,7 @@ private function getStatusType() return $this->bStatus ? Design::SUCCESS_TYPE : Design::ERROR_TYPE; } - /** - * @param stdClass $oMsg - * - * @return void - */ - private function setRead(stdClass $oMsg) + private function setRead(stdClass $oMsg): void { if ($oMsg->status == MailModel::UNREAD_STATUS) { $this->oMailModel->setReadMsg($oMsg->messageId); diff --git a/_protected/app/system/modules/mail/models/MailModel.php b/_protected/app/system/modules/mail/models/MailModel.php index ec2445e64c..94b391486e 100755 --- a/_protected/app/system/modules/mail/models/MailModel.php +++ b/_protected/app/system/modules/mail/models/MailModel.php @@ -232,7 +232,7 @@ public function setTo($iProfileId, $iMessageId, $sMode) * @param int|null $iProfileId * @param int $iType * - * @return int|stdClass + * @return int|array */ public function search($mLooking, $bCount, $sOrderBy, $iSort, $iOffset, $iLimit, $iProfileId = null, $iType = self::ALL) { @@ -287,11 +287,7 @@ public function search($mLooking, $bCount, $sOrderBy, $iSort, $iOffset, $iLimit, $rStmt->execute(); - if (!$bCount) { - $mData = $rStmt->fetchAll(PDO::FETCH_OBJ); - } else { - $mData = (int)$rStmt->fetchColumn(); - } + $mData = $bCount ? (int)$rStmt->fetchColumn() : $rStmt->fetchAll(PDO::FETCH_OBJ); Db::free($rStmt); diff --git a/_protected/app/system/modules/user/controllers/BrowseController.php b/_protected/app/system/modules/user/controllers/BrowseController.php index fa11cac2eb..010ac35568 100755 --- a/_protected/app/system/modules/user/controllers/BrowseController.php +++ b/_protected/app/system/modules/user/controllers/BrowseController.php @@ -14,6 +14,7 @@ use PH7\Framework\Mvc\Router\Uri; use PH7\Framework\Navigation\Page; use PH7\Framework\Url\Header; +use stdClass; class BrowseController extends Controller { @@ -41,14 +42,14 @@ public function index(): void self::MAX_PROFILES_PER_PAGE ); $this->view->current_page = $this->oPage->getCurrentPage(); - $oUsers = $this->oUserModel->search( + $aUsers = $this->oUserModel->search( $_GET, false, $this->oPage->getFirstItem(), $this->oPage->getNbItemsPerPage() ); - if (empty($oUsers)) { + if (!empty($aUsers) && $this->isSearch()) { Header::redirect( Uri::get('user', 'browse', 'index'), t('No results. Please try again with wider or different search criteria.'), @@ -63,8 +64,13 @@ public function index(): void $this->view->h3_title = t('Meet new People with %0%', '' . $this->registry->site_name . ''); $this->view->meta_description = t('Meet new People and Friends near you with %site_name% - Browse Members'); $this->view->avatarDesign = new AvatarDesignCore; - $this->view->users = $oUsers; + $this->view->users = $aUsers; $this->output(); } } + + private function isSearch(): bool + { + return !empty($_GET) && count($_GET) > 1; + } } diff --git a/_protected/app/system/modules/user/controllers/ProfileController.php b/_protected/app/system/modules/user/controllers/ProfileController.php index d12aec7211..cded31334c 100755 --- a/_protected/app/system/modules/user/controllers/ProfileController.php +++ b/_protected/app/system/modules/user/controllers/ProfileController.php @@ -26,8 +26,7 @@ class ProfileController extends ProfileBaseController const MAP_WIDTH_SIZE = '100%'; const MAP_HEIGHT_SIZE = '300px'; - /** @var string */ - private $sUsername; + private string $sUsername; public function __construct() { @@ -40,7 +39,7 @@ public function __construct() $this->setVisitorId($this->session->get('member_id')); } - public function index() + public function index(): void { $this->addCssFiles(); $this->addAdditionalAssetFiles(); @@ -140,13 +139,7 @@ public function index() $this->output(); } - /** - * @param string $sFirstName - * @param stdClass $oUser - * - * @return void - */ - protected function setMenuBar($sFirstName, stdClass $oUser) + protected function setMenuBar(string $sFirstName, stdClass $oUser): void { parent::setMenuBar($sFirstName, $oUser); @@ -161,10 +154,8 @@ protected function setMenuBar($sFirstName, stdClass $oUser) /** * Add the General and Tabs Menu stylesheets. - * - * @return void */ - protected function addCssFiles() + protected function addCssFiles(): void { $this->design->addCss( PH7_LAYOUT, @@ -172,10 +163,7 @@ protected function addCssFiles() ); } - /** - * @return string - */ - private function getFriendLinkName() + private function getFriendLinkName(): string { $iNbFriend = FriendCoreModel::total($this->iProfileId); $sNbFriend = $iNbFriend > 0 ? ' (' . $iNbFriend . ')' : ''; @@ -184,10 +172,7 @@ private function getFriendLinkName() return $sFriendTxt . $sNbFriend; } - /** - * @return string - */ - private function getMutualFriendLinkName() + private function getMutualFriendLinkName(): string { $iNbMutFriend = (new FriendCoreModel)->get( $this->iVisitorId, @@ -205,12 +190,7 @@ private function getMutualFriendLinkName() return $sMutFriendTxt . $sNbMutFriend; } - /** - * @param stdClass $oUser - * - * @return bool - */ - private function doesProfileExist(stdClass $oUser) + private function doesProfileExist(stdClass $oUser): bool { return !empty($oUser->username) && $this->str->equalsIgnoreCase($this->sUsername, $oUser->username); } @@ -219,17 +199,15 @@ private function doesProfileExist(stdClass $oUser) /** * @return bool TRUE if the admin is not logged in (TRUE as well if the admin use "login as user"). */ - private function isNotAdmin() + private function isNotAdmin(): bool { return !AdminCore::auth() || UserCore::isAdminLoggedAs(); } /** - * @return void - * * @throws Framework\File\IOException */ - private function redirectToCoolProfileStyle() + private function redirectToCoolProfileStyle(): void { Header::redirect( Uri::get( @@ -244,11 +222,9 @@ private function redirectToCoolProfileStyle() /** * Show a Not Found page. * - * @return void - * * @throws Framework\Http\Exception */ - private function notFound() + private function notFound(): void { Http::setHeadersByCode(StatusCode::NOT_FOUND); diff --git a/_protected/framework/Mvc/Router/Uri.class.php b/_protected/framework/Mvc/Router/Uri.class.php index 398fa4ea9e..a522ec8adf 100755 --- a/_protected/framework/Mvc/Router/Uri.class.php +++ b/_protected/framework/Mvc/Router/Uri.class.php @@ -68,14 +68,14 @@ public static function loadFile(DOMDocument $oDom): DOMDocument * @param string $sModule * @param string $sController * @param string $sAction - * @param string|null $sVars Default NULL + * @param string|int|null $sVars Default NULL * @param bool $bFullClean Default TRUE * * @return string * * @throws IOException */ - public static function get(string $sModule, string $sController, string $sAction, ?string $sVars = null, bool $bFullClean = true): string + public static function get(string $sModule, string $sController, string $sAction, $sVars = null, bool $bFullClean = true): string { self::$bFullClean = $bFullClean; diff --git a/_protected/framework/Navigation/Page.class.php b/_protected/framework/Navigation/Page.class.php index 4733f1a2be..cc8202f9ae 100755 --- a/_protected/framework/Navigation/Page.class.php +++ b/_protected/framework/Navigation/Page.class.php @@ -38,7 +38,7 @@ public function __construct() $this->oHttpRequest = new HttpRequest; } - public function getTotalPages(int $iTotalItems, int $iNbItemsPerPage = self::DEFAULT_NUMBER_ITEMS): int + public function getTotalPages(?int $iTotalItems, int $iNbItemsPerPage = self::DEFAULT_NUMBER_ITEMS): int { $this->totalPages($iTotalItems, $iNbItemsPerPage); @@ -85,10 +85,10 @@ public static function cleanDynamicUrl(string $sVar): string return $sUrl . self::trailingSlash($sUrl) . '?' . $sVar . '='; } - private function totalPages(int $iTotalItems, int $iNbItemsPerPage): void + private function totalPages(?int $iTotalItems, int $iNbItemsPerPage): void { $this->iTotalItems = (int)$iTotalItems; - $this->iNbItemsPerPage = (int)$iNbItemsPerPage; // or intval() function, but it is slower than casting + $this->iNbItemsPerPage = $iNbItemsPerPage; // or intval() function, but it is slower than casting $this->iCurrentPage = $this->oHttpRequest->getExists(Pagination::REQUEST_PARAM_NAME) ? $this->oHttpRequest->get(Pagination::REQUEST_PARAM_NAME, 'int') : 1; // Ternary condition to prevent division by zero