Skip to content

Commit

Permalink
refactor: PHPstan code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Feb 4, 2024
1 parent 4947905 commit 2b2d972
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 204 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main",
"codeception/codeception": "^3.0",
"markhuot/craftql": "^1.0.0",
"vlucas/phpdotenv": "^3.0"
},
"scripts": {
Expand Down
117 changes: 62 additions & 55 deletions src/Retour.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use craft\web\ErrorHandler;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use craft\web\User;
use markhuot\CraftQL\Builders\Schema;
use markhuot\CraftQL\CraftQL;
use markhuot\CraftQL\Events\AlterSchemaFields;
Expand All @@ -47,6 +48,7 @@
use nystudio107\retour\services\ServicesTrait;
use nystudio107\retour\variables\RetourVariable;
use nystudio107\retour\widgets\RetourWidget;
use Twig\Error\RuntimeError;
use yii\base\Event;
use yii\web\HttpException;

Expand All @@ -58,6 +60,7 @@
* @author nystudio107
* @package Retour
* @since 3.0.0
* @method Settings getSettings()
*/
class Retour extends Plugin
{
Expand Down Expand Up @@ -186,36 +189,38 @@ public function getCpNavItem()
{
$subNavs = [];
$navItem = parent::getCpNavItem();
$currentUser = Craft::$app->getUser()->getIdentity();
// Only show sub-navs the user has permission to view
if ($currentUser->can('retour:dashboard')) {
$subNavs['dashboard'] = [
'label' => 'Dashboard',
'url' => 'retour/dashboard',
];
}
if ($currentUser->can('retour:redirects')) {
$subNavs['redirects'] = [
'label' => 'Redirects',
'url' => 'retour/redirects',
];
}
if ($currentUser->can('retour:shortlinks')) {
$subNavs['shortlinks'] = [
'label' => 'Short Links',
'url' => 'retour/shortlinks',
];
}
$editableSettings = true;
$general = Craft::$app->getConfig()->getGeneral();
if (self::$craft31 && !$general->allowAdminChanges) {
$editableSettings = false;
}
if ($currentUser->can('retour:settings') && $editableSettings) {
$subNavs['settings'] = [
'label' => 'Settings',
'url' => 'retour/settings',
];
$user = Craft::$app->getUser();
if (($user instanceof User) && $currentUser = $user->getIdentity()) {
// Only show sub-navs the user has permission to view
if ($currentUser->can('retour:dashboard')) {
$subNavs['dashboard'] = [
'label' => 'Dashboard',
'url' => 'retour/dashboard',
];
}
if ($currentUser->can('retour:redirects')) {
$subNavs['redirects'] = [
'label' => 'Redirects',
'url' => 'retour/redirects',
];
}
if ($currentUser->can('retour:shortlinks')) {
$subNavs['shortlinks'] = [
'label' => 'Short Links',
'url' => 'retour/shortlinks',
];
}
$editableSettings = true;
$general = Craft::$app->getConfig()->getGeneral();
if (self::$craft31 && !$general->allowAdminChanges) {
$editableSettings = false;
}
if ($currentUser->can('retour:settings') && $editableSettings) {
$subNavs['settings'] = [
'label' => 'Settings',
'url' => 'retour/settings',
];
}
}
// Retour doesn't really have an index page, so if the user can't access any sub nav items, we probably shouldn't show the main sub nav item either
if (empty($subNavs)) {
Expand Down Expand Up @@ -279,7 +284,7 @@ protected function installEventListeners()
Event::on(
ClearCaches::class,
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
function(RegisterCacheOptionsEvent $event) {
function (RegisterCacheOptionsEvent $event) {
Craft::debug(
'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
__METHOD__
Expand All @@ -295,7 +300,7 @@ function(RegisterCacheOptionsEvent $event) {
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function(PluginEvent $event) {
function (PluginEvent $event) {
if ($event->plugin === $this) {
// Invalidate our caches after we've been installed
$this->clearAllCaches();
Expand All @@ -322,7 +327,7 @@ protected function installGlobalEventListeners()
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function(Event $event) {
function (Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('retour', [
Expand All @@ -332,10 +337,10 @@ function(Event $event) {
}
);

$prepareRedirectOnElementChange = function(ElementEvent $event) {
$prepareRedirectOnElementChange = function (ElementEvent $event) {
/** @var Element $element */
$element = $event->element;
if ($element !== null && !$event->isNew && $element->getUrl() !== null && !$element->propagating) {
if ($element !== null && !$element->propagating && !$event->isNew && $element->getUrl() !== null) {
$checkElementSlug = true;
// If we're running Craft 3.2 or later, also check that isn't not a draft or revision
if (Retour::$craft32 && (
Expand All @@ -348,14 +353,14 @@ function(Event $event) {
if (self::$settings->createUriChangeRedirects && $checkElementSlug) {
// Make sure this isn't a transitioning temporary draft/revision and that it's
// not propagating to other sites
if (strpos($element->uri, '__temp_') === false && !$element->propagating) {
if (strpos($element->uri, '__temp_') === false) {
Retour::$plugin->events->stashElementUris($element);
}
}
}
};

$insertRedirectOnElementChange = function(ElementEvent $event) {
$insertRedirectOnElementChange = function (ElementEvent $event) {
/** @var Element $element */
$element = $event->element;
if ($element !== null && !$event->isNew && $element->getUrl() !== null) {
Expand All @@ -373,7 +378,7 @@ function(Event $event) {
Event::on(
Elements::class,
Elements::EVENT_BEFORE_SAVE_ELEMENT,
function(ElementEvent $event) use ($prepareRedirectOnElementChange) {
function (ElementEvent $event) use ($prepareRedirectOnElementChange) {
Craft::debug(
'Elements::EVENT_BEFORE_SAVE_ELEMENT',
__METHOD__
Expand All @@ -385,7 +390,7 @@ function(ElementEvent $event) use ($prepareRedirectOnElementChange) {
Event::on(
Elements::class,
Elements::EVENT_AFTER_SAVE_ELEMENT,
function(ElementEvent $event) use ($insertRedirectOnElementChange) {
function (ElementEvent $event) use ($insertRedirectOnElementChange) {
Craft::debug(
'Elements::EVENT_AFTER_SAVE_ELEMENT',
__METHOD__
Expand All @@ -397,7 +402,7 @@ function(ElementEvent $event) use ($insertRedirectOnElementChange) {
Event::on(
Elements::class,
Elements::EVENT_BEFORE_UPDATE_SLUG_AND_URI,
function(ElementEvent $event) use ($prepareRedirectOnElementChange) {
function (ElementEvent $event) use ($prepareRedirectOnElementChange) {
Craft::debug(
'Elements::EVENT_BEFORE_UPDATE_SLUG_AND_URI',
__METHOD__
Expand All @@ -409,7 +414,7 @@ function(ElementEvent $event) use ($prepareRedirectOnElementChange) {
Event::on(
Elements::class,
Elements::EVENT_AFTER_UPDATE_SLUG_AND_URI,
function(ElementEvent $event) use ($insertRedirectOnElementChange) {
function (ElementEvent $event) use ($insertRedirectOnElementChange) {
Craft::debug(
'Elements::EVENT_AFTER_UPDATE_SLUG_AND_URI',
__METHOD__
Expand All @@ -422,7 +427,7 @@ function(ElementEvent $event) use ($insertRedirectOnElementChange) {
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_LOAD_PLUGINS,
function() {
function () {
// Install these only after all other plugins have loaded
$request = Craft::$app->getRequest();
// Only respond to non-console site requests
Expand All @@ -439,7 +444,7 @@ function() {
Event::on(
Fields::class,
Fields::EVENT_REGISTER_FIELD_TYPES,
function(RegisterComponentTypesEvent $event) {
function (RegisterComponentTypesEvent $event) {
$event->types[] = ShortLinkField::class;
}
);
Expand All @@ -448,7 +453,7 @@ function(RegisterComponentTypesEvent $event) {
Event::on(
Gql::class,
Gql::EVENT_REGISTER_GQL_TYPES,
function(RegisterGqlTypesEvent $event) {
function (RegisterGqlTypesEvent $event) {
Craft::debug(
'Gql::EVENT_REGISTER_GQL_TYPES',
__METHOD__
Expand All @@ -460,7 +465,7 @@ function(RegisterGqlTypesEvent $event) {
Event::on(
Gql::class,
Gql::EVENT_REGISTER_GQL_QUERIES,
function(RegisterGqlQueriesEvent $event) {
function (RegisterGqlQueriesEvent $event) {
Craft::debug(
'Gql::EVENT_REGISTER_GQL_QUERIES',
__METHOD__
Expand All @@ -476,7 +481,7 @@ function(RegisterGqlQueriesEvent $event) {
Event::on(
Gql::class,
Gql::EVENT_REGISTER_GQL_SCHEMA_COMPONENTS,
function(RegisterGqlSchemaComponentsEvent $event) {
function (RegisterGqlSchemaComponentsEvent $event) {
Craft::debug(
'Gql::EVENT_REGISTER_GQL_SCHEMA_COMPONENTS',
__METHOD__
Expand Down Expand Up @@ -506,7 +511,7 @@ protected function installSiteEventListeners()
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function(RegisterUrlRulesEvent $event) {
function (RegisterUrlRulesEvent $event) {
Craft::debug(
'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
__METHOD__
Expand All @@ -529,18 +534,20 @@ protected function installCpEventListeners()
Event::on(
Dashboard::class,
Dashboard::EVENT_REGISTER_WIDGET_TYPES,
function(RegisterComponentTypesEvent $event) {
$currentUser = Craft::$app->getUser()->getIdentity();
if ($currentUser->can('accessPlugin-retour')) {
$event->types[] = RetourWidget::class;
function (RegisterComponentTypesEvent $event) {
$user = Craft::$app->getUser();
if (($user instanceof User) && $currentUser = $user->getIdentity()) {
if ($currentUser->can('accessPlugin-retour')) {
$event->types[] = RetourWidget::class;
}
}
}
);
// Handler: UrlManager::EVENT_REGISTER_CP_URL_RULES
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function(RegisterUrlRulesEvent $event) {
function (RegisterUrlRulesEvent $event) {
Craft::debug(
'UrlManager::EVENT_REGISTER_CP_URL_RULES',
__METHOD__
Expand All @@ -556,7 +563,7 @@ function(RegisterUrlRulesEvent $event) {
Event::on(
UserPermissions::class,
UserPermissions::EVENT_REGISTER_PERMISSIONS,
function(RegisterUserPermissionsEvent $event) {
function (RegisterUserPermissionsEvent $event) {
Craft::debug(
'UserPermissions::EVENT_REGISTER_PERMISSIONS',
__METHOD__
Expand All @@ -578,14 +585,14 @@ protected function handleSiteRequest()
Event::on(
ErrorHandler::class,
ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION,
function(ExceptionEvent $event) {
function (ExceptionEvent $event) {
Craft::debug(
'ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION',
__METHOD__
);
$exception = $event->exception;
// If this is a Twig Runtime exception, use the previous one instead
if ($exception instanceof \Twig\Error\RuntimeError &&
if ($exception instanceof RuntimeError &&
($previousException = $exception->getPrevious()) !== null) {
$exception = $previousException;
}
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use craft\web\Controller;
use nystudio107\retour\Retour;
use yii\web\Response;

/**
* @author nystudio107
Expand Down Expand Up @@ -51,12 +52,12 @@ public function beforeAction($action)

/**
* @param null $siteId
* @return \yii\web\Response
* @return Response
*/
public function actionGetRedirects($siteId = null)
{
$redirects = Retour::$plugin->redirects->getAllStaticRedirects(null, $siteId);

return $this->asJson($redirects ?? []);
return $this->asJson($redirects);
}
}
4 changes: 1 addition & 3 deletions src/controllers/ChartsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

use Craft;
use craft\db\Query;

use craft\helpers\ArrayHelper;
use craft\web\Controller;
use nystudio107\retour\helpers\Permission as PermissionHelper;

use yii\web\ForbiddenHttpException;
use yii\web\Response;

Expand All @@ -35,7 +33,7 @@ class ChartsController extends Controller
// =========================================================================

/**
* @var bool|array
* @inheritdoc
*/
protected $allowAnonymous = [
];
Expand Down
Loading

0 comments on commit 2b2d972

Please sign in to comment.