From 62c59a4903fa30d5cebd298fa4cf420d6e3f1d19 Mon Sep 17 00:00:00 2001 From: Nathan van der Werf Date: Wed, 15 Aug 2018 19:15:13 +0200 Subject: [PATCH] Refactor ternary operators to null coalescing operators --- .../backend/behaviors/ImportExportController.php | 4 +--- modules/backend/behaviors/RelationController.php | 4 ++-- modules/backend/classes/AuthManager.php | 4 +--- modules/backend/classes/BackendController.php | 6 +++--- modules/backend/classes/FormTabs.php | 2 +- modules/backend/classes/NavigationManager.php | 4 +--- modules/backend/classes/WidgetBase.php | 2 +- modules/backend/classes/WidgetManager.php | 2 +- modules/backend/traits/SessionMaker.php | 2 +- modules/backend/widgets/Filter.php | 4 ++-- modules/backend/widgets/Form.php | 6 +++--- modules/backend/widgets/Lists.php | 8 +++----- modules/backend/widgets/ReportContainer.php | 2 +- modules/cms/classes/CodeBase.php | 2 +- modules/cms/classes/ComponentHelpers.php | 8 ++------ modules/cms/classes/Controller.php | 11 +++-------- modules/cms/twig/DebugExtension.php | 2 +- modules/cms/widgets/ComponentList.php | 14 +++----------- modules/system/classes/UpdateManager.php | 6 +++--- modules/system/classes/VersionManager.php | 4 +--- modules/system/models/PluginVersion.php | 4 +--- modules/system/traits/ViewMaker.php | 2 +- 22 files changed, 37 insertions(+), 66 deletions(-) diff --git a/modules/backend/behaviors/ImportExportController.php b/modules/backend/behaviors/ImportExportController.php index 6b371f68b..67676427d 100644 --- a/modules/backend/behaviors/ImportExportController.php +++ b/modules/backend/behaviors/ImportExportController.php @@ -590,9 +590,7 @@ class ImportExportController extends ControllerBehavior { $lists = $this->controller->makeLists(); - $widget = isset($lists[$definition]) - ? $lists[$definition] - : reset($lists); + $widget = $lists[$definition] ?? reset($lists); /* * Parse options diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 2781302ef..3e14bd615 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -421,7 +421,7 @@ class RelationController extends ControllerBehavior /* * Determine the partial to use based on the supplied section option */ - $section = isset($options['section']) ? $options['section'] : null; + $section = $options['section'] ?? null; switch (strtolower($section)) { case 'toolbar': return $this->toolbarWidget ? $this->toolbarWidget->render() : null; @@ -1520,7 +1520,7 @@ class RelationController extends ControllerBehavior */ protected function evalFormContext($mode = 'manage', $exists = false) { - $config = isset($this->config->{$mode}) ? $this->config->{$mode} : []; + $config = $this->config->{$mode} ?? []; if (($context = array_get($config, 'context')) && is_array($context)) { $context = $exists diff --git a/modules/backend/classes/AuthManager.php b/modules/backend/classes/AuthManager.php index 9fd62508d..c56dc6356 100644 --- a/modules/backend/classes/AuthManager.php +++ b/modules/backend/classes/AuthManager.php @@ -150,9 +150,7 @@ class AuthManager extends RainAuthManager $tabs = []; foreach ($this->listPermissions() as $permission) { - $tab = isset($permission->tab) - ? $permission->tab - : 'backend::lang.form.undefined_tab'; + $tab = $permission->tab ?? 'backend::lang.form.undefined_tab'; if (!array_key_exists($tab, $tabs)) { $tabs[$tab] = []; diff --git a/modules/backend/classes/BackendController.php b/modules/backend/classes/BackendController.php index 1a77aa8c8..6da4e0450 100644 --- a/modules/backend/classes/BackendController.php +++ b/modules/backend/classes/BackendController.php @@ -82,8 +82,8 @@ class BackendController extends ControllerBase /* * Look for a Module controller */ - $module = isset($params[0]) ? $params[0] : 'backend'; - $controller = isset($params[1]) ? $params[1] : 'index'; + $module = $params[0] ?? 'backend'; + $controller = $params[1] ?? 'index'; self::$action = $action = isset($params[2]) ? $this->parseAction($params[2]) : 'index'; self::$params = $controllerParams = array_slice($params, 3); $controllerClass = '\\'.$module.'\Controllers\\'.$controller; @@ -100,7 +100,7 @@ class BackendController extends ControllerBase */ if (count($params) >= 2) { list($author, $plugin) = $params; - $controller = isset($params[2]) ? $params[2] : 'index'; + $controller = $params[2] ?? 'index'; self::$action = $action = isset($params[3]) ? $this->parseAction($params[3]) : 'index'; self::$params = $controllerParams = array_slice($params, 4); $controllerClass = '\\'.$author.'\\'.$plugin.'\Controllers\\'.$controller; diff --git a/modules/backend/classes/FormTabs.php b/modules/backend/classes/FormTabs.php index f40e3be1d..ea755a55e 100644 --- a/modules/backend/classes/FormTabs.php +++ b/modules/backend/classes/FormTabs.php @@ -232,6 +232,6 @@ class FormTabs implements IteratorAggregate, ArrayAccess */ public function offsetGet($offset) { - return isset($this->fields[$offset]) ? $this->fields[$offset] : null; + return $this->fields[$offset] ?? null; } } diff --git a/modules/backend/classes/NavigationManager.php b/modules/backend/classes/NavigationManager.php index 128ff1103..8af12ee33 100644 --- a/modules/backend/classes/NavigationManager.php +++ b/modules/backend/classes/NavigationManager.php @@ -466,9 +466,7 @@ class NavigationManager { $key = $owner.$mainMenuItemCode; - return array_key_exists($key, $this->contextSidenavPartials) - ? $this->contextSidenavPartials[$key] - : null; + return $this->contextSidenavPartials[$key] ?? null; } /** diff --git a/modules/backend/classes/WidgetBase.php b/modules/backend/classes/WidgetBase.php index 31479a726..11c2814d8 100644 --- a/modules/backend/classes/WidgetBase.php +++ b/modules/backend/classes/WidgetBase.php @@ -65,7 +65,7 @@ abstract class WidgetBase extends Extendable * If no alias is set by the configuration. */ if (!isset($this->alias)) { - $this->alias = isset($this->config->alias) ? $this->config->alias : $this->defaultAlias; + $this->alias = $this->config->alias ?? $this->defaultAlias; } /* diff --git a/modules/backend/classes/WidgetManager.php b/modules/backend/classes/WidgetManager.php index 18ce9e47c..91972173b 100644 --- a/modules/backend/classes/WidgetManager.php +++ b/modules/backend/classes/WidgetManager.php @@ -103,7 +103,7 @@ class WidgetManager $widgetInfo = ['code' => $widgetInfo]; } - $widgetCode = isset($widgetInfo['code']) ? $widgetInfo['code'] : null; + $widgetCode = $widgetInfo['code'] ?? null; if (!$widgetCode) { $widgetCode = Str::getClassId($className); diff --git a/modules/backend/traits/SessionMaker.php b/modules/backend/traits/SessionMaker.php index 3f47fce51..1af4dc71f 100644 --- a/modules/backend/traits/SessionMaker.php +++ b/modules/backend/traits/SessionMaker.php @@ -55,7 +55,7 @@ trait SessionMaker return $currentStore; } - return isset($currentStore[$key]) ? $currentStore[$key] : $default; + return $currentStore[$key] ?? $default; } /** diff --git a/modules/backend/widgets/Filter.php b/modules/backend/widgets/Filter.php index 94c415a94..6cd2fd477 100644 --- a/modules/backend/widgets/Filter.php +++ b/modules/backend/widgets/Filter.php @@ -584,8 +584,8 @@ class Filter extends WidgetBase */ protected function makeFilterScope($name, $config) { - $label = isset($config['label']) ? $config['label'] : null; - $scopeType = isset($config['type']) ? $config['type'] : null; + $label = $config['label'] ?? null; + $scopeType = $config['type'] ?? null; $scope = new FilterScope($name, $label); $scope->displayAs($scopeType, $config); diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 154c97410..2dc30ab48 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -643,7 +643,7 @@ class Form extends WidgetBase */ protected function makeFormField($name, $config = []) { - $label = isset($config['label']) ? $config['label'] : null; + $label = $config['label'] ?? null; list($fieldName, $fieldContext) = $this->getFieldName($name); $field = new FormField($fieldName, $label); @@ -671,7 +671,7 @@ class Form extends WidgetBase */ else { - $fieldType = isset($config['type']) ? $config['type'] : null; + $fieldType = $config['type'] ?? null; if (!is_string($fieldType) && $fieldType !== null) { throw new ApplicationException(Lang::get( 'backend::lang.field.invalid_type', @@ -714,7 +714,7 @@ class Form extends WidgetBase * Defer the execution of option data collection */ $field->options(function () use ($field, $config) { - $fieldOptions = isset($config['options']) ? $config['options'] : null; + $fieldOptions = $config['options'] ?? null; $fieldOptions = $this->getOptionsFromModel($field, $fieldOptions); return $fieldOptions; }); diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index f0e52b472..b18d0391c 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -873,7 +873,7 @@ class Lists extends WidgetBase $config['searchable'] = false; } - $columnType = isset($config['type']) ? $config['type'] : null; + $columnType = $config['type'] ?? null; $column = new ListColumn($name, $label); $column->displayAs($columnType, $config); @@ -1220,7 +1220,7 @@ class Lists extends WidgetBase $dateTime = $this->validateDateTimeValue($value, $column); - $format = $column->format !== null ? $column->format : 'g:i A'; + $format = $column->format ?? 'g:i A'; $value = $dateTime->format($format); @@ -1489,9 +1489,7 @@ class Lists extends WidgetBase } elseif (is_array($this->defaultSort) && isset($this->defaultSort['column'])) { $this->sortColumn = $this->defaultSort['column']; - $this->sortDirection = (isset($this->defaultSort['direction'])) ? - $this->defaultSort['direction'] : - 'desc'; + $this->sortDirection = $this->defaultSort['direction'] ?? 'desc'; } } diff --git a/modules/backend/widgets/ReportContainer.php b/modules/backend/widgets/ReportContainer.php index 6abb44506..dc57bd8e7 100644 --- a/modules/backend/widgets/ReportContainer.php +++ b/modules/backend/widgets/ReportContainer.php @@ -416,7 +416,7 @@ class ReportContainer extends WidgetBase $property = [ 'property' => $name, 'title' => isset($params['title']) ? Lang::get($params['title']) : $name, - 'type' => isset($params['type']) ? $params['type'] : 'string' + 'type' => $params['type'] ?? 'string' ]; foreach ($params as $name => $value) { diff --git a/modules/cms/classes/CodeBase.php b/modules/cms/classes/CodeBase.php index 4fc9eb9bd..87df2dcc9 100644 --- a/modules/cms/classes/CodeBase.php +++ b/modules/cms/classes/CodeBase.php @@ -94,7 +94,7 @@ class CodeBase extends Extendable implements ArrayAccess */ public function offsetGet($offset) { - return isset($this->controller->vars[$offset]) ? $this->controller->vars[$offset] : null; + return $this->controller->vars[$offset] ?? null; } /** diff --git a/modules/cms/classes/ComponentHelpers.php b/modules/cms/classes/ComponentHelpers.php index 703327ecd..01226cddd 100644 --- a/modules/cms/classes/ComponentHelpers.php +++ b/modules/cms/classes/ComponentHelpers.php @@ -106,9 +106,7 @@ class ComponentHelpers public static function getComponentName($component) { $details = $component->componentDetails(); - $name = isset($details['name']) - ? $details['name'] - : 'cms::lang.component.unnamed'; + $name = $details['name'] ?? 'cms::lang.component.unnamed'; return Lang::get($name); } @@ -121,9 +119,7 @@ class ComponentHelpers public static function getComponentDescription($component) { $details = $component->componentDetails(); - $name = isset($details['description']) - ? $details['description'] - : 'cms::lang.component.no_description'; + $name = $details['description'] ?? 'cms::lang.component.no_description'; return Lang::get($name); } diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index 427933578..ab73e1111 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -484,7 +484,7 @@ class Controller $useCache = !Config::get('cms.twigNoCache'); $isDebugMode = Config::get('app.debug', false); $strictVariables = Config::get('cms.enableTwigStrictVariables', false); - $strictVariables = $strictVariables === null ? $isDebugMode : $strictVariables; + $strictVariables = $strictVariables ?? $isDebugMode; $forceBytecode = Config::get('cms.forceBytecodeInvalidation', false); $options = [ @@ -1354,15 +1354,10 @@ class Controller if (substr($paramName, 0, 1) == ':') { $routeParamName = substr($paramName, 1); - $newPropertyValue = array_key_exists($routeParamName, $routerParameters) - ? $routerParameters[$routeParamName] - : null; - + $newPropertyValue = $routerParameters[$routeParamName] ?? null; } else { - $newPropertyValue = array_key_exists($paramName, $parameters) - ? $parameters[$paramName] - : null; + $newPropertyValue = $parameters[$paramName] ?? null; } $component->setProperty($propertyName, $newPropertyValue); diff --git a/modules/cms/twig/DebugExtension.php b/modules/cms/twig/DebugExtension.php index f747e8c02..ab80f116a 100644 --- a/modules/cms/twig/DebugExtension.php +++ b/modules/cms/twig/DebugExtension.php @@ -374,7 +374,7 @@ class DebugExtension extends Twig_Extension } $method = $parts[1]; - return isset($this->commentMap[$method]) ? $this->commentMap[$method] : null; + return $this->commentMap[$method] ?? null; } /** diff --git a/modules/cms/widgets/ComponentList.php b/modules/cms/widgets/ComponentList.php index 8c0486004..6b0d008e3 100644 --- a/modules/cms/widgets/ComponentList.php +++ b/modules/cms/widgets/ComponentList.php @@ -78,17 +78,9 @@ class ComponentList extends WidgetBase $pluginDetails = $plugin->pluginDetails(); - $pluginName = isset($pluginDetails['name']) - ? $pluginDetails['name'] - : Lang::get('system::lang.plugin.unnamed'); - - $pluginIcon = isset($pluginDetails['icon']) - ? $pluginDetails['icon'] - : 'icon-puzzle-piece'; - - $pluginDescription = isset($pluginDetails['description']) - ? $pluginDetails['description'] - : null; + $pluginName = $pluginDetails['name'] ?? Lang::get('system::lang.plugin.unnamed'); + $pluginIcon = $pluginDetails['icon'] ?? 'icon-puzzle-piece'; + $pluginDescription = $pluginDetails['description'] ?? null; $pluginClass = get_class($plugin); diff --git a/modules/system/classes/UpdateManager.php b/modules/system/classes/UpdateManager.php index bac12a550..916e3f10b 100644 --- a/modules/system/classes/UpdateManager.php +++ b/modules/system/classes/UpdateManager.php @@ -251,9 +251,9 @@ class UpdateManager */ $plugins = []; foreach (array_get($result, 'plugins', []) as $code => $info) { - $info['name'] = isset($names[$code]) ? $names[$code] : $code; - $info['old_version'] = isset($versions[$code]) ? $versions[$code] : false; - $info['icon'] = isset($icons[$code]) ? $icons[$code] : false; + $info['name'] = $names[$code] ?? $code; + $info['old_version'] = $versions[$code] ?? false; + $info['icon'] = $icons[$code] ?? false; /* * If a plugin has updates frozen, or cannot be updated, diff --git a/modules/system/classes/VersionManager.php b/modules/system/classes/VersionManager.php index ace928272..4b1666a48 100644 --- a/modules/system/classes/VersionManager.php +++ b/modules/system/classes/VersionManager.php @@ -327,9 +327,7 @@ class VersionManager ; } - return isset($this->databaseVersions[$code]) - ? $this->databaseVersions[$code] - : self::NO_VERSION_VALUE; + return $this->databaseVersions[$code] ?? self::NO_VERSION_VALUE; } /** diff --git a/modules/system/models/PluginVersion.php b/modules/system/models/PluginVersion.php index 321cb60df..29ebe4298 100644 --- a/modules/system/models/PluginVersion.php +++ b/modules/system/models/PluginVersion.php @@ -146,9 +146,7 @@ class PluginVersion extends Model self::$versionCache = self::lists('version', 'code'); } - return isset(self::$versionCache[$pluginCode]) - ? self::$versionCache[$pluginCode] - : null; + return self::$versionCache[$pluginCode] ?? null; } /** diff --git a/modules/system/traits/ViewMaker.php b/modules/system/traits/ViewMaker.php index 2041f3f42..be2ef1fcc 100644 --- a/modules/system/traits/ViewMaker.php +++ b/modules/system/traits/ViewMaker.php @@ -140,7 +140,7 @@ trait ViewMaker */ public function makeLayout($name = null, $params = [], $throwException = true) { - $layout = $name === null ? $this->layout : $name; + $layout = $name ?? $this->layout; if ($layout == '') { return ''; }