Refactor ternary operators to null coalescing operators

This commit is contained in:
Nathan van der Werf 2018-08-15 19:15:13 +02:00
parent a3d7a028b4
commit 62c59a4903
22 changed files with 37 additions and 66 deletions

View File

@ -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

View File

@ -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

View File

@ -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] = [];

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}
/**

View File

@ -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;
}
/*

View File

@ -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);

View File

@ -55,7 +55,7 @@ trait SessionMaker
return $currentStore;
}
return isset($currentStore[$key]) ? $currentStore[$key] : $default;
return $currentStore[$key] ?? $default;
}
/**

View File

@ -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);

View File

@ -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;
});

View File

@ -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';
}
}

View File

@ -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) {

View File

@ -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;
}
/**

View File

@ -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);
}

View File

@ -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);

View File

@ -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;
}
/**

View File

@ -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);

View File

@ -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,

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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 '';
}