From 1cc5f156359403a3877acb7055830be2431f9011 Mon Sep 17 00:00:00 2001 From: SilverFire - Dmitry Naumenko Date: Sun, 13 May 2018 17:33:10 +0300 Subject: [PATCH] Use null coalesce operator where possible Added ternary_to_null_coalescing to php-cs-fixer config Related to #11397 --- cs/src/YiiConfig.php | 3 ++- framework/base/Application.php | 2 +- framework/base/Model.php | 4 ++-- framework/console/Controller.php | 6 ++--- framework/console/ExitCode.php | 2 +- framework/console/Request.php | 4 ++-- .../console/controllers/MigrateController.php | 5 ++--- framework/console/widgets/Table.php | 7 +++--- framework/data/Sort.php | 4 ++-- framework/db/ActiveRelationTrait.php | 8 +++---- framework/db/Command.php | 6 ++--- framework/db/Schema.php | 4 ++-- framework/db/conditions/InConditionBuilder.php | 2 +- framework/db/pgsql/Schema.php | 4 ++-- framework/db/sqlite/Schema.php | 4 ++-- framework/filters/PageCache.php | 2 +- framework/grid/GridView.php | 4 ++-- framework/helpers/BaseArrayHelper.php | 2 +- framework/helpers/BaseConsole.php | 2 +- framework/helpers/BaseFileHelper.php | 4 ++-- framework/helpers/BaseHtml.php | 26 +++++++++++----------- framework/helpers/BaseIpHelper.php | 4 ++-- framework/helpers/BaseStringHelper.php | 2 +- framework/helpers/BaseVarDumper.php | 2 +- framework/rbac/PhpManager.php | 6 ++--- framework/test/BaseActiveFixture.php | 2 +- framework/test/FixtureTrait.php | 6 ++--- framework/validators/IpValidator.php | 2 +- framework/web/MultiFieldSession.php | 2 +- framework/web/Request.php | 4 ++-- framework/web/Response.php | 4 ++-- framework/web/Session.php | 8 +++---- framework/web/UrlRule.php | 2 +- framework/widgets/DetailView.php | 4 ++-- framework/widgets/Menu.php | 2 +- tests/TestCase.php | 2 +- .../console/controllers/CacheControllerTest.php | 6 ++--- tests/framework/db/CommandTest.php | 2 +- tests/framework/db/pgsql/ActiveRecordTest.php | 2 +- tests/framework/log/LoggerDispatchingTest.php | 2 +- tests/framework/log/SyslogTargetTest.php | 2 +- tests/framework/validators/FileValidatorTest.php | 8 +++---- 42 files changed, 89 insertions(+), 90 deletions(-) diff --git a/cs/src/YiiConfig.php b/cs/src/YiiConfig.php index 4e2e28f..0627a73 100644 --- a/cs/src/YiiConfig.php +++ b/cs/src/YiiConfig.php @@ -147,8 +147,9 @@ class YiiConfig extends Config 'single_blank_line_before_namespace' => true, 'single_quote' => true, 'standardize_not_equals' => true, - 'ternary_operator_spaces' => true, 'trailing_comma_in_multiline_array' => true, + 'ternary_operator_spaces' => true, + 'ternary_to_null_coalescing' => true, 'trim_array_spaces' => true, 'unary_operator_spaces' => true, 'whitespace_after_comma_in_array' => true, diff --git a/framework/base/Application.php b/framework/base/Application.php index cb109f1..9ad8658 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -403,7 +403,7 @@ abstract class Application extends Module return $response->exitStatus; } catch (ExitException $e) { - $this->end($e->statusCode, isset($response) ? $response : null); + $this->end($e->statusCode, $response ?? null); return $e->statusCode; } } diff --git a/framework/base/Model.php b/framework/base/Model.php index c885887..11a299f 100644 --- a/framework/base/Model.php +++ b/framework/base/Model.php @@ -522,7 +522,7 @@ class Model extends Component implements StaticInstanceInterface, IteratorAggreg public function getAttributeLabel($attribute) { $labels = $this->attributeLabels(); - return isset($labels[$attribute]) ? $labels[$attribute] : $this->generateAttributeLabel($attribute); + return $labels[$attribute] ?? $this->generateAttributeLabel($attribute); } /** @@ -535,7 +535,7 @@ class Model extends Component implements StaticInstanceInterface, IteratorAggreg public function getAttributeHint($attribute) { $hints = $this->attributeHints(); - return isset($hints[$attribute]) ? $hints[$attribute] : ''; + return $hints[$attribute] ?? ''; } /** diff --git a/framework/console/Controller.php b/framework/console/Controller.php index f7928d9..facbe4e 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -290,7 +290,7 @@ class Controller extends \yii\base\Controller return Console::prompt($text, $options); } - return isset($options['default']) ? $options['default'] : ''; + return $options['default'] ?? ''; } /** @@ -488,7 +488,7 @@ class Controller extends \yii\base\Controller continue; } $name = $reflection->getName(); - $tag = isset($params[$i]) ? $params[$i] : ''; + $tag = $params[$i] ?? ''; if (preg_match('/^(\S+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) { $type = $matches[1]; $comment = $matches[3]; @@ -553,7 +553,7 @@ class Controller extends \yii\base\Controller $name = Inflector::camel2id($name, '-', true); if (isset($tags['var']) || isset($tags['property'])) { - $doc = isset($tags['var']) ? $tags['var'] : $tags['property']; + $doc = $tags['var'] ?? $tags['property']; if (is_array($doc)) { $doc = reset($doc); } diff --git a/framework/console/ExitCode.php b/framework/console/ExitCode.php index b67f3e7..03d2b36 100644 --- a/framework/console/ExitCode.php +++ b/framework/console/ExitCode.php @@ -155,6 +155,6 @@ class ExitCode */ public static function getReason($exitCode) { - return isset(static::$reasons[$exitCode]) ? static::$reasons[$exitCode] : 'Unknown exit code'; + return static::$reasons[$exitCode] ?? 'Unknown exit code'; } } diff --git a/framework/console/Request.php b/framework/console/Request.php index be76ff4..4aef93b 100644 --- a/framework/console/Request.php +++ b/framework/console/Request.php @@ -84,7 +84,7 @@ class Request extends \yii\base\Request } if ($name !== Application::OPTION_APPCONFIG) { - $params[$name] = isset($matches[2]) ? $matches[2] : true; + $params[$name] = $matches[2] ?? true; $prevOption = &$params[$name]; } } elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) { @@ -92,7 +92,7 @@ class Request extends \yii\base\Request if (is_numeric($name)) { $params[] = $param; } else { - $params['_aliases'][$name] = isset($matches[2]) ? $matches[2] : true; + $params['_aliases'][$name] = $matches[2] ?? true; $prevOption = &$params['_aliases'][$name]; } } elseif ($prevOption === true) { diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 96e19b3..ed28839 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -486,9 +486,8 @@ class MigrateController extends BaseMigrateController if (strncmp($chunk, 'foreignKey', 10) === 0) { preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches); $foreignKeys[$property] = [ - 'table' => isset($matches[1]) - ? $matches[1] - : preg_replace('/_id$/', '', $property), + 'table' => $matches[1] + ?? preg_replace('/_id$/', '', $property), 'column' => !empty($matches[2]) ? $matches[2] : null, diff --git a/framework/console/widgets/Table.php b/framework/console/widgets/Table.php index 66cbb09..f61b65c 100644 --- a/framework/console/widgets/Table.php +++ b/framework/console/widgets/Table.php @@ -231,7 +231,7 @@ class Table extends Widget for ($i = 0, ($max = $this->calculateRowHeight($row)) ?: $max = 1; $i < $max; $i++) { $buffer .= $spanLeft . ' '; foreach ($size as $index => $cellSize) { - $cell = isset($row[$index]) ? $row[$index] : null; + $cell = $row[$index] ?? null; $prefix = ''; if ($index !== 0) { $buffer .= $spanMiddle . ' '; @@ -377,9 +377,8 @@ class Table extends Widget { if (!$this->_screenWidth) { $size = Console::getScreenSize(); - $this->_screenWidth = isset($size[0]) - ? $size[0] - : self::DEFAULT_CONSOLE_SCREEN_WIDTH + self::CONSOLE_SCROLLBAR_OFFSET; + $this->_screenWidth = $size[0] + ?? self::DEFAULT_CONSOLE_SCREEN_WIDTH + self::CONSOLE_SCROLLBAR_OFFSET; } return $this->_screenWidth; } diff --git a/framework/data/Sort.php b/framework/data/Sort.php index 7ee59d2..69149e2 100644 --- a/framework/data/Sort.php +++ b/framework/data/Sort.php @@ -344,7 +344,7 @@ class Sort extends BaseObject { $orders = $this->getAttributeOrders(); - return isset($orders[$attribute]) ? $orders[$attribute] : null; + return $orders[$attribute] ?? null; } /** @@ -435,7 +435,7 @@ class Sort extends BaseObject $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC; unset($directions[$attribute]); } else { - $direction = isset($definition['default']) ? $definition['default'] : SORT_ASC; + $direction = $definition['default'] ?? SORT_ASC; } if ($this->enableMultiSort) { diff --git a/framework/db/ActiveRelationTrait.php b/framework/db/ActiveRelationTrait.php index d4f5a9d..9655df6 100644 --- a/framework/db/ActiveRelationTrait.php +++ b/framework/db/ActiveRelationTrait.php @@ -297,7 +297,7 @@ trait ActiveRelationTrait } } else { $key = $this->getModelKey($primaryModel, $link); - $value = isset($buckets[$key]) ? $buckets[$key] : ($this->multiple ? [] : null); + $value = $buckets[$key] ?? ($this->multiple ? [] : null); } if ($primaryModel instanceof ActiveRecordInterface) { $primaryModel->populateRelation($name, $value); @@ -338,18 +338,18 @@ trait ActiveRelationTrait if ($model instanceof ActiveRecordInterface) { foreach ($models as $model) { $key = $this->getModelKey($model, $relation->link); - $model->populateRelation($name, isset($buckets[$key]) ? $buckets[$key] : []); + $model->populateRelation($name, $buckets[$key] ?? []); } } else { foreach ($primaryModels as $i => $primaryModel) { if ($this->multiple) { foreach ($primaryModel as $j => $m) { $key = $this->getModelKey($m, $relation->link); - $primaryModels[$i][$j][$name] = isset($buckets[$key]) ? $buckets[$key] : []; + $primaryModels[$i][$j][$name] = $buckets[$key] ?? []; } } elseif (!empty($primaryModel[$primaryName])) { $key = $this->getModelKey($primaryModel[$primaryName], $relation->link); - $primaryModels[$i][$primaryName][$name] = isset($buckets[$key]) ? $buckets[$key] : []; + $primaryModels[$i][$primaryName][$name] = $buckets[$key] ?? []; } } } diff --git a/framework/db/Command.php b/framework/db/Command.php index d7a5bf5..27d90ee 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -220,7 +220,7 @@ class Command extends Component } $sql = ''; foreach (explode('?', $this->_sql) as $i => $part) { - $sql .= (isset($params[$i]) ? $params[$i] : '') . $part; + $sql .= ($params[$i] ?? '') . $part; } return $sql; @@ -1100,10 +1100,10 @@ class Command extends Component Yii::info($rawSql, $category); } if (!$this->db->enableProfiling) { - return [false, isset($rawSql) ? $rawSql : null]; + return [false, $rawSql ?? null]; } - return [true, isset($rawSql) ? $rawSql : $this->getRawSql()]; + return [true, $rawSql ?? $this->getRawSql()]; } /** diff --git a/framework/db/Schema.php b/framework/db/Schema.php index 57b378a..ede2929 100644 --- a/framework/db/Schema.php +++ b/framework/db/Schema.php @@ -270,7 +270,7 @@ abstract class Schema extends BaseObject ]; $type = gettype($data); - return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; + return $typeMap[$type] ?? \PDO::PARAM_STR; } /** @@ -441,7 +441,7 @@ abstract class Schema extends BaseObject break; } - $result[$name] = isset($columns[$name]) ? $columns[$name] : $tableSchema->columns[$name]->defaultValue; + $result[$name] = $columns[$name] ?? $tableSchema->columns[$name]->defaultValue; } return $result; diff --git a/framework/db/conditions/InConditionBuilder.php b/framework/db/conditions/InConditionBuilder.php index 87e4d8d..13971a6 100644 --- a/framework/db/conditions/InConditionBuilder.php +++ b/framework/db/conditions/InConditionBuilder.php @@ -90,7 +90,7 @@ class InConditionBuilder implements ExpressionBuilderInterface foreach ($values as $i => $value) { if (is_array($value) || $value instanceof \ArrayAccess) { - $value = isset($value[$column]) ? $value[$column] : null; + $value = $value[$column] ?? null; } if ($value === null) { $sqlValues[$i] = 'NULL'; diff --git a/framework/db/pgsql/Schema.php b/framework/db/pgsql/Schema.php index 462e8ba..5470f30 100644 --- a/framework/db/pgsql/Schema.php +++ b/framework/db/pgsql/Schema.php @@ -695,8 +695,8 @@ SQL; 'foreignSchemaName' => $constraint[0]['foreign_table_schema'], 'foreignTableName' => $constraint[0]['foreign_table_name'], 'foreignColumnNames' => array_keys(array_count_values(ArrayHelper::getColumn($constraint, 'foreign_column_name'))), - 'onDelete' => isset($actionTypes[$constraint[0]['on_delete']]) ? $actionTypes[$constraint[0]['on_delete']] : null, - 'onUpdate' => isset($actionTypes[$constraint[0]['on_update']]) ? $actionTypes[$constraint[0]['on_update']] : null, + 'onDelete' => $actionTypes[$constraint[0]['on_delete']] ?? null, + 'onUpdate' => $actionTypes[$constraint[0]['on_update']] ?? null, ]); break; case 'u': diff --git a/framework/db/sqlite/Schema.php b/framework/db/sqlite/Schema.php index 9274914..544299c 100644 --- a/framework/db/sqlite/Schema.php +++ b/framework/db/sqlite/Schema.php @@ -127,8 +127,8 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface 'columnNames' => ArrayHelper::getColumn($foreignKey, 'from'), 'foreignTableName' => $table, 'foreignColumnNames' => ArrayHelper::getColumn($foreignKey, 'to'), - 'onDelete' => isset($foreignKey[0]['on_delete']) ? $foreignKey[0]['on_delete'] : null, - 'onUpdate' => isset($foreignKey[0]['on_update']) ? $foreignKey[0]['on_update'] : null, + 'onDelete' => $foreignKey[0]['on_delete'] ?? null, + 'onUpdate' => $foreignKey[0]['on_update'] ?? null, ]); } diff --git a/framework/filters/PageCache.php b/framework/filters/PageCache.php index ba96522..4f9e633 100644 --- a/framework/filters/PageCache.php +++ b/framework/filters/PageCache.php @@ -226,7 +226,7 @@ class PageCache extends ActionFilter implements DynamicContentAwareInterface if (!empty($data['dynamicPlaceholders']) && is_array($data['dynamicPlaceholders'])) { $response->content = $this->updateDynamicContent($response->content, $data['dynamicPlaceholders'], true); } - $this->afterRestoreResponse(isset($data['cacheData']) ? $data['cacheData'] : null); + $this->afterRestoreResponse($data['cacheData'] ?? null); } /** diff --git a/framework/grid/GridView.php b/framework/grid/GridView.php index 7be1e28..ccec842 100644 --- a/framework/grid/GridView.php +++ b/framework/grid/GridView.php @@ -532,8 +532,8 @@ class GridView extends BaseListView '__class' => $this->dataColumnClass ?: DataColumn::class, 'grid' => $this, 'attribute' => $matches[1], - 'format' => isset($matches[3]) ? $matches[3] : 'text', - 'label' => isset($matches[5]) ? $matches[5] : null, + 'format' => $matches[3] ?? 'text', + 'label' => $matches[5] ?? null, ]); } diff --git a/framework/helpers/BaseArrayHelper.php b/framework/helpers/BaseArrayHelper.php index 94858eb..4714a20 100644 --- a/framework/helpers/BaseArrayHelper.php +++ b/framework/helpers/BaseArrayHelper.php @@ -927,7 +927,7 @@ class BaseArrayHelper foreach ($filters as $var) { $keys = explode('.', $var); $globalKey = $keys[0]; - $localKey = isset($keys[1]) ? $keys[1] : null; + $localKey = $keys[1] ?? null; if ($globalKey[0] === '!') { $forbiddenVars[] = [ diff --git a/framework/helpers/BaseConsole.php b/framework/helpers/BaseConsole.php index 3ad723f..1a09072 100644 --- a/framework/helpers/BaseConsole.php +++ b/framework/helpers/BaseConsole.php @@ -814,7 +814,7 @@ class BaseConsole } elseif ($options['validator'] && !call_user_func_array($options['validator'], [$input, &$error]) ) { - static::output(isset($error) ? $error : $options['error']); + static::output($error ?? $options['error']); goto top; } diff --git a/framework/helpers/BaseFileHelper.php b/framework/helpers/BaseFileHelper.php index 3a51d43..d0ae900 100644 --- a/framework/helpers/BaseFileHelper.php +++ b/framework/helpers/BaseFileHelper.php @@ -303,7 +303,7 @@ class BaseFileHelper } $dstExists = is_dir($dst); if (!$dstExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) { - static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true); + static::createDirectory($dst, $options['dirMode'] ?? 0775, true); $dstExists = true; } @@ -329,7 +329,7 @@ class BaseFileHelper if (is_file($from)) { if (!$dstExists) { // delay creation of destination directory until the first file is copied to avoid creating empty directories - static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true); + static::createDirectory($dst, $options['dirMode'] ?? 0775, true); $dstExists = true; } copy($from, $to); diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 32245ad..5b5ca9d 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -766,7 +766,7 @@ class BaseHtml } if (isset($options['label'])) { $label = $options['label']; - $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : []; + $labelOptions = $options['labelOptions'] ?? []; unset($options['label'], $options['labelOptions']); $content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions); return $hidden . $content; @@ -1190,7 +1190,7 @@ class BaseHtml public static function activeHint($model, $attribute, $options = []) { $attribute = static::getAttributeName($attribute); - $hint = isset($options['hint']) ? $options['hint'] : $model->getAttributeHint($attribute); + $hint = $options['hint'] ?? $model->getAttributeHint($attribute); if (empty($hint)) { return ''; } @@ -1218,7 +1218,7 @@ class BaseHtml */ public static function errorSummary($models, $options = []) { - $header = isset($options['header']) ? $options['header'] : '

' . Yii::t('yii', 'Please fix the following errors:') . '

'; + $header = $options['header'] ?? '

' . Yii::t('yii', 'Please fix the following errors:') . '

'; $footer = ArrayHelper::remove($options, 'footer', ''); $encode = ArrayHelper::remove($options, 'encode', true); $showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false); @@ -1319,8 +1319,8 @@ class BaseHtml */ public static function activeInput($type, $model, $attribute, $options = []) { - $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); - $value = isset($options['value']) ? $options['value'] : static::getAttributeValue($model, $attribute); + $name = $options['name'] ?? static::getInputName($model, $attribute); + $value = $options['value'] ?? static::getAttributeValue($model, $attribute); if (!array_key_exists('id', $options)) { $options['id'] = static::getInputId($model, $attribute); } @@ -1491,7 +1491,7 @@ class BaseHtml */ public static function activeTextarea($model, $attribute, $options = []) { - $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); + $name = $options['name'] ?? static::getInputName($model, $attribute); if (isset($options['value'])) { $value = $options['value']; unset($options['value']); @@ -1552,7 +1552,7 @@ class BaseHtml */ protected static function activeBooleanInput($type, $model, $attribute, $options = []) { - $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); + $name = $options['name'] ?? static::getInputName($model, $attribute); $value = static::getAttributeValue($model, $attribute); if (!array_key_exists('value', $options)) { @@ -1788,8 +1788,8 @@ class BaseHtml */ protected static function activeListInput($type, $model, $attribute, $items, $options = []) { - $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute); - $selection = isset($options['value']) ? $options['value'] : static::getAttributeValue($model, $attribute); + $name = $options['name'] ?? static::getInputName($model, $attribute); + $selection = $options['value'] ?? static::getAttributeValue($model, $attribute); if (!array_key_exists('unselect', $options)) { $options['unselect'] = ''; } @@ -1841,15 +1841,15 @@ class BaseHtml $lines[] = static::tag('option', $promptText, $promptOptions); } - $options = isset($tagOptions['options']) ? $tagOptions['options'] : []; - $groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : []; + $options = $tagOptions['options'] ?? []; + $groups = $tagOptions['groups'] ?? []; unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']); $options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces); $options['encode'] = ArrayHelper::getValue($options, 'encode', $encode); foreach ($items as $key => $value) { if (is_array($value)) { - $groupAttrs = isset($groups[$key]) ? $groups[$key] : []; + $groupAttrs = $groups[$key] ?? []; if (!isset($groupAttrs['label'])) { $groupAttrs['label'] = $key; } @@ -1857,7 +1857,7 @@ class BaseHtml $content = static::renderSelectOptions($selection, $value, $attrs); $lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs); } else { - $attrs = isset($options[$key]) ? $options[$key] : []; + $attrs = $options[$key] ?? []; $attrs['value'] = (string) $key; if (!array_key_exists('selected', $attrs)) { $attrs['selected'] = $selection !== null && diff --git a/framework/helpers/BaseIpHelper.php b/framework/helpers/BaseIpHelper.php index 5af851b..6d71d49 100644 --- a/framework/helpers/BaseIpHelper.php +++ b/framework/helpers/BaseIpHelper.php @@ -75,8 +75,8 @@ class BaseIpHelper } $maxMask = $ipVersion === self::IPV4 ? self::IPV4_ADDRESS_LENGTH : self::IPV6_ADDRESS_LENGTH; - $mask = isset($mask) ? $mask : $maxMask; - $netMask = isset($netMask) ? $netMask : $maxMask; + $mask = $mask ?? $maxMask; + $netMask = $netMask ?? $maxMask; $binIp = static::ip2bin($ip); $binNet = static::ip2bin($net); diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index 3fb6df4..5f469c7 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -310,7 +310,7 @@ class BaseStringHelper $value = (string)$value; $localeInfo = localeconv(); - $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null; + $decimalSeparator = $localeInfo['decimal_point'] ?? null; if ($decimalSeparator !== null && $decimalSeparator !== '.') { $value = str_replace($decimalSeparator, '.', $value); diff --git a/framework/helpers/BaseVarDumper.php b/framework/helpers/BaseVarDumper.php index 5cb57ce..8574fe1 100644 --- a/framework/helpers/BaseVarDumper.php +++ b/framework/helpers/BaseVarDumper.php @@ -255,7 +255,7 @@ class BaseVarDumper continue; } if ($closureTokens !== []) { - $closureTokens[] = isset($token[1]) ? $token[1] : $token; + $closureTokens[] = $token[1] ?? $token; if ($token === '}') { $pendingParenthesisCount--; if ($pendingParenthesisCount === 0) { diff --git a/framework/rbac/PhpManager.php b/framework/rbac/PhpManager.php index df3c531..bef8b81 100644 --- a/framework/rbac/PhpManager.php +++ b/framework/rbac/PhpManager.php @@ -725,9 +725,9 @@ class PhpManager extends BaseManager $this->items[$name] = new $class([ 'name' => $name, - 'description' => isset($item['description']) ? $item['description'] : null, - 'ruleName' => isset($item['ruleName']) ? $item['ruleName'] : null, - 'data' => isset($item['data']) ? $item['data'] : null, + 'description' => $item['description'] ?? null, + 'ruleName' => $item['ruleName'] ?? null, + 'data' => $item['data'] ?? null, 'createdAt' => $itemsMtime, 'updatedAt' => $itemsMtime, ]); diff --git a/framework/test/BaseActiveFixture.php b/framework/test/BaseActiveFixture.php index 317d0fa..ba2b7f5 100644 --- a/framework/test/BaseActiveFixture.php +++ b/framework/test/BaseActiveFixture.php @@ -62,7 +62,7 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate $modelClass = $this->modelClass; $keys = []; foreach ($modelClass::primaryKey() as $key) { - $keys[$key] = isset($row[$key]) ? $row[$key] : null; + $keys[$key] = $row[$key] ?? null; } return $this->_models[$name] = $modelClass::findOne($keys); diff --git a/framework/test/FixtureTrait.php b/framework/test/FixtureTrait.php index 01f578f..fe0a451 100644 --- a/framework/test/FixtureTrait.php +++ b/framework/test/FixtureTrait.php @@ -197,18 +197,18 @@ trait FixtureTrait while (($fixture = array_pop($stack)) !== null) { if ($fixture instanceof Fixture) { $class = get_class($fixture); - $name = isset($aliases[$class]) ? $aliases[$class] : $class; + $name = $aliases[$class] ?? $class; unset($instances[$name]); // unset so that the fixture is added to the last in the next line $instances[$name] = $fixture; } else { $class = ltrim($fixture['__class'], '\\'); - $name = isset($aliases[$class]) ? $aliases[$class] : $class; + $name = $aliases[$class] ?? $class; if (!isset($instances[$name])) { $instances[$name] = false; $stack[] = $fixture = Yii::createObject($fixture); foreach ($fixture->depends as $dep) { // need to use the configuration provided in test case - $stack[] = isset($config[$dep]) ? $config[$dep] : ['__class' => $dep]; + $stack[] = $config[$dep] ?? ['__class' => $dep]; } } elseif ($instances[$name] === false) { throw new InvalidConfigException("A circular dependency is detected for fixture '$class'."); diff --git a/framework/validators/IpValidator.php b/framework/validators/IpValidator.php index c768aa1..cfe91a6 100644 --- a/framework/validators/IpValidator.php +++ b/framework/validators/IpValidator.php @@ -331,7 +331,7 @@ class IpValidator extends Validator if (preg_match($this->getIpParsePattern(), $ip, $matches)) { $negation = ($matches[1] !== '') ? $matches[1] : null; $ip = $matches[2]; - $cidr = isset($matches[4]) ? $matches[4] : null; + $cidr = $matches[4] ?? null; } if ($this->subnet === true && $cidr === null) { diff --git a/framework/web/MultiFieldSession.php b/framework/web/MultiFieldSession.php index 56ff240..39bf7bd 100644 --- a/framework/web/MultiFieldSession.php +++ b/framework/web/MultiFieldSession.php @@ -137,6 +137,6 @@ abstract class MultiFieldSession extends Session return $fields['data']; } - return isset($fields['data']) ? $fields['data'] : ''; + return $fields['data'] ?? ''; } } diff --git a/framework/web/Request.php b/framework/web/Request.php index 8c02266..4f8fc59 100644 --- a/framework/web/Request.php +++ b/framework/web/Request.php @@ -761,7 +761,7 @@ class Request extends \yii\base\Request implements ServerRequestInterface } } - return isset($params[$name]) ? $params[$name] : $defaultValue; + return $params[$name] ?? $defaultValue; } /** @@ -851,7 +851,7 @@ class Request extends \yii\base\Request implements ServerRequestInterface { $params = $this->getQueryParams(); - return isset($params[$name]) ? $params[$name] : $defaultValue; + return $params[$name] ?? $defaultValue; } /** diff --git a/framework/web/Response.php b/framework/web/Response.php index 31df1a8..c6cf456 100644 --- a/framework/web/Response.php +++ b/framework/web/Response.php @@ -548,7 +548,7 @@ class Response extends \yii\base\Response implements ResponseInterface $body->write($content); } - $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream'; + $mimeType = $options['mimeType'] ?? 'application/octet-stream'; $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $end - $begin + 1); $this->format = self::FORMAT_RAW; @@ -600,7 +600,7 @@ class Response extends \yii\base\Response implements ResponseInterface $this->setStatusCode(200); } - $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream'; + $mimeType = $options['mimeType'] ?? 'application/octet-stream'; $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $end - $begin + 1); $this->format = self::FORMAT_RAW; diff --git a/framework/web/Session.php b/framework/web/Session.php index c3a4075..575a858 100644 --- a/framework/web/Session.php +++ b/framework/web/Session.php @@ -141,7 +141,7 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co $this->updateFlashCounters(); } else { $error = error_get_last(); - $message = isset($error['message']) ? $error['message'] : 'Failed to start session.'; + $message = $error['message'] ?? 'Failed to start session.'; Yii::error($message, __METHOD__); } } @@ -611,7 +611,7 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co public function get($key, $defaultValue = null) { $this->open(); - return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue; + return $_SESSION[$key] ?? $defaultValue; } /** @@ -895,7 +895,7 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co { $this->open(); - return isset($_SESSION[$offset]) ? $_SESSION[$offset] : null; + return $_SESSION[$offset] ?? null; } /** @@ -949,7 +949,7 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co Yii::info('Session unfrozen', __METHOD__); } else { $error = error_get_last(); - $message = isset($error['message']) ? $error['message'] : 'Failed to unfreeze session.'; + $message = $error['message'] ?? 'Failed to unfreeze session.'; Yii::error($message, __METHOD__); } diff --git a/framework/web/UrlRule.php b/framework/web/UrlRule.php index f69a06a..c2a0118 100644 --- a/framework/web/UrlRule.php +++ b/framework/web/UrlRule.php @@ -284,7 +284,7 @@ class UrlRule extends BaseObject implements UrlRuleInterface $appendSlash = false; foreach ($matches as $match) { $name = $match[1][0]; - $pattern = isset($match[2][0]) ? $match[2][0] : '[^\/]+'; + $pattern = $match[2][0] ?? '[^\/]+'; $placeholder = 'a' . hash('crc32b', $name); // placeholder must begin with a letter $this->placeholders[$placeholder] = $name; if (array_key_exists($name, $this->defaults)) { diff --git a/framework/widgets/DetailView.php b/framework/widgets/DetailView.php index a98cf68..78baf00 100644 --- a/framework/widgets/DetailView.php +++ b/framework/widgets/DetailView.php @@ -213,8 +213,8 @@ class DetailView extends Widget } $attribute = [ 'attribute' => $matches[1], - 'format' => isset($matches[3]) ? $matches[3] : 'text', - 'label' => isset($matches[5]) ? $matches[5] : null, + 'format' => $matches[3] ?? 'text', + 'label' => $matches[5] ?? null, ]; } diff --git a/framework/widgets/Menu.php b/framework/widgets/Menu.php index e550c66..1e819d6 100644 --- a/framework/widgets/Menu.php +++ b/framework/widgets/Menu.php @@ -266,7 +266,7 @@ class Menu extends Widget if (!isset($item['label'])) { $item['label'] = ''; } - $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; + $encodeLabel = $item['encode'] ?? $this->encodeLabels; $items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label']; $hasActiveChild = false; if (isset($item['items'])) { diff --git a/tests/TestCase.php b/tests/TestCase.php index 7591726..42f3069 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -29,7 +29,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase static::$params = require __DIR__ . '/data/config.php'; } - return isset(static::$params[$name]) ? static::$params[$name] : $default; + return static::$params[$name] ?? $default; } /** diff --git a/tests/framework/console/controllers/CacheControllerTest.php b/tests/framework/console/controllers/CacheControllerTest.php index a4911d6..af3a081 100644 --- a/tests/framework/console/controllers/CacheControllerTest.php +++ b/tests/framework/console/controllers/CacheControllerTest.php @@ -55,10 +55,10 @@ class CacheControllerTest extends TestCase }, 'session' => 'yii\web\CacheSession', // should be ignored at `actionFlushAll()` 'db' => [ - '__class' => isset($config['__class']) ? $config['__class'] : \yii\db\Connection::class, + '__class' => $config['__class'] ?? \yii\db\Connection::class, 'dsn' => $config['dsn'], - 'username' => isset($config['username']) ? $config['username'] : null, - 'password' => isset($config['password']) ? $config['password'] : null, + 'username' => $config['username'] ?? null, + 'password' => $config['password'] ?? null, 'enableSchemaCache' => true, 'schemaCache' => 'firstCache', ], diff --git a/tests/framework/db/CommandTest.php b/tests/framework/db/CommandTest.php index 6ed824b..f08d7bb 100644 --- a/tests/framework/db/CommandTest.php +++ b/tests/framework/db/CommandTest.php @@ -1018,7 +1018,7 @@ SQL; protected function performAndCompareUpsertResult(Connection $db, array $data) { $params = $data['params']; - $expected = isset($data['expected']) ? $data['expected'] : $params[1]; + $expected = $data['expected'] ?? $params[1]; $command = $db->createCommand(); call_user_func_array([$command, 'upsert'], $params); $command->execute(); diff --git a/tests/framework/db/pgsql/ActiveRecordTest.php b/tests/framework/db/pgsql/ActiveRecordTest.php index a3c6e52..23b5b30 100644 --- a/tests/framework/db/pgsql/ActiveRecordTest.php +++ b/tests/framework/db/pgsql/ActiveRecordTest.php @@ -191,7 +191,7 @@ class ActiveRecordTest extends \yiiunit\framework\db\ActiveRecordTest $type = ArrayAndJsonTypes::find()->one(); foreach ($attributes as $attribute => $expected) { - $expected = isset($expected[1]) ? $expected[1] : $expected[0]; + $expected = $expected[1] ?? $expected[0]; $value = $type->$attribute; $this->assertEquals($expected, $value, 'In column ' . $attribute); diff --git a/tests/framework/log/LoggerDispatchingTest.php b/tests/framework/log/LoggerDispatchingTest.php index 55830e2..76a9d3e 100644 --- a/tests/framework/log/LoggerDispatchingTest.php +++ b/tests/framework/log/LoggerDispatchingTest.php @@ -166,7 +166,7 @@ namespace yiiunit\framework\log { public static function __callStatic($name, $arguments) { if (isset(static::$functions[$name]) && is_callable(static::$functions[$name])) { - $arguments = isset($arguments[0]) ? $arguments[0] : $arguments; + $arguments = $arguments[0] ?? $arguments; return forward_static_call(static::$functions[$name], $arguments); } static::fail("Function '$name' has not implemented yet!"); diff --git a/tests/framework/log/SyslogTargetTest.php b/tests/framework/log/SyslogTargetTest.php index 4885105..bdf4316 100644 --- a/tests/framework/log/SyslogTargetTest.php +++ b/tests/framework/log/SyslogTargetTest.php @@ -197,7 +197,7 @@ namespace yiiunit\framework\log { public static function __callStatic($name, $arguments) { if (isset(static::$functions[$name]) && is_callable(static::$functions[$name])) { - $arguments = isset($arguments[0]) ? $arguments[0] : $arguments; + $arguments = $arguments[0] ?? $arguments; return forward_static_call(static::$functions[$name], $arguments); } static::fail("Function '$name' has not implemented yet!"); diff --git a/tests/framework/validators/FileValidatorTest.php b/tests/framework/validators/FileValidatorTest.php index 37314e5..54b6272 100644 --- a/tests/framework/validators/FileValidatorTest.php +++ b/tests/framework/validators/FileValidatorTest.php @@ -338,18 +338,18 @@ class FileValidatorTest extends TestCase $files[$key] = ['no instance of UploadedFile']; continue; } - $name = isset($param['clientFilename']) ? $param['clientFilename'] : $rndString(); + $name = $param['clientFilename'] ?? $rndString(); $tempName = \Yii::getAlias('@yiiunit/runtime/validators/file/tmp/') . $name; if (is_readable($tempName)) { $size = filesize($tempName); } else { - $size = isset($param['size']) ? $param['size'] : rand( + $size = $param['size'] ?? rand( 1, $this->sizeToBytes(ini_get('upload_max_filesize')) ); } - $type = isset($param['clientMediaType']) ? $param['clientMediaType'] : 'text/plain'; - $error = isset($param['error']) ? $param['error'] : UPLOAD_ERR_OK; + $type = $param['clientMediaType'] ?? 'text/plain'; + $error = $param['error'] ?? UPLOAD_ERR_OK; if (count($params) == 1) { $error = empty($param) ? UPLOAD_ERR_NO_FILE : $error;