Skip to content

Commit

Permalink
refactor: Use @var instead of instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Feb 5, 2024
1 parent 8c15dfc commit 2f3b52e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
6 changes: 4 additions & 2 deletions src/Retour.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ public function getCpNavItem()
{
$subNavs = [];
$navItem = parent::getCpNavItem();
/** @var User $user */
$user = Craft::$app->getUser();
if (($user instanceof User) && $currentUser = $user->getIdentity()) {
if ($currentUser = $user->getIdentity()) {
// Only show sub-navs the user has permission to view
if ($currentUser->can('retour:dashboard')) {
$subNavs['dashboard'] = [
Expand Down Expand Up @@ -535,8 +536,9 @@ protected function installCpEventListeners()
Dashboard::class,
Dashboard::EVENT_REGISTER_WIDGET_TYPES,
function (RegisterComponentTypesEvent $event) {
/** @var User $user */
$user = Craft::$app->getUser();
if (($user instanceof User) && $currentUser = $user->getIdentity()) {
if ($currentUser = $user->getIdentity()) {
if ($currentUser->can('accessPlugin-retour')) {
$event->types[] = RetourWidget::class;
}
Expand Down
18 changes: 8 additions & 10 deletions src/controllers/RedirectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,11 @@ public function actionSaveRedirect()
if (!$redirect->validate()) {
Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save redirect settings."));
// Send the redirect back to the template
/** @var UrlManager $urlManager */
$urlManager = Craft::$app->getUrlManager();
if ($urlManager instanceof UrlManager) {
$urlManager->setRouteParams([
'redirect' => $redirect,
]);
}
$urlManager->setRouteParams([
'redirect' => $redirect,
]);

return null;
}
Expand All @@ -290,12 +289,11 @@ public function actionSaveRedirect()
if ($testRedirectConfig === null) {
Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save redirect settings because it'd create a redirect loop."));
// Send the redirect back to the template
/** @var UrlManager $urlManager */
$urlManager = Craft::$app->getUrlManager();
if ($urlManager instanceof UrlManager) {
$urlManager->setRouteParams([
'redirect' => $redirect,
]);
}
$urlManager->setRouteParams([
'redirect' => $redirect,
]);

return null;
}
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ public function actionSavePluginSettings()
Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings."));

// Send the plugin back to the template
/** @var UrlManager $urlManager */
$urlManager = Craft::$app->getUrlManager();
if ($urlManager instanceof UrlManager) {
$urlManager->setRouteParams([
'plugin' => $plugin,
]);
}
$urlManager->setRouteParams([
'plugin' => $plugin,
]);

return null;
}
Expand Down
13 changes: 6 additions & 7 deletions src/helpers/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ class Permission
*/
public static function controllerPermissionCheck(string $permission)
{
/** @var User $user */
$user = Craft::$app->getUser();
if ($user instanceof User) {
if (($currentUser = $user->getIdentity()) === null) {
throw new ForbiddenHttpException('Your account has no identity.');
}
if (($currentUser = $user->getIdentity()) === null) {
throw new ForbiddenHttpException('Your account has no identity.');
}

if (!$currentUser->can($permission)) {
throw new ForbiddenHttpException("Your account doesn't have permission to assign access this resource.");
}
if (!$currentUser->can($permission)) {
throw new ForbiddenHttpException("Your account doesn't have permission to assign access this resource.");
}
}

Expand Down

0 comments on commit 2f3b52e

Please sign in to comment.