Skip to content

Commit

Permalink
Merge pull request #4796 from jeromegamez/coalesce
Browse files Browse the repository at this point in the history
Use null coalescing operator
  • Loading branch information
paulbalandan authored Jun 7, 2021
2 parents c3b33f5 + ace9df0 commit 9bf2e85
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 51 deletions.
7 changes: 1 addition & 6 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1707,12 +1707,7 @@ public function __get(string $name)
return $this->$name;
}

if (isset($this->db->$name))
{
return $this->db->$name;
}

return null;
return $this->db->$name ?? null;
}

/**
Expand Down
7 changes: 1 addition & 6 deletions system/CLI/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,7 @@ public function getPad(array $array, int $pad): int
*/
public function __get(string $key)
{
if (isset($this->$key))
{
return $this->$key;
}

return null;
return $this->$key ?? null;
}

/**
Expand Down
7 changes: 1 addition & 6 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,12 +944,7 @@ public static function getURI(): string
*/
public static function getSegment(int $index)
{
if (! isset(static::$segments[$index - 1]))
{
return null;
}

return static::$segments[$index - 1];
return static::$segments[$index - 1] ?? null;
}

//--------------------------------------------------------------------
Expand Down
7 changes: 1 addition & 6 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1397,12 +1397,7 @@ public function escape($str)
return "'{$str}'";
}

if ($str === null)
{
return 'NULL';
}

return $str;
return $str ?? 'NULL';
}

//--------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,12 @@ protected function getHostname()
return $_SERVER['SERVER_NAME'];
}

return isset($_SERVER['SERVER_ADDR']) ? '[' . $_SERVER['SERVER_ADDR'] . ']' : '[127.0.0.1]';
if (isset($_SERVER['SERVER_ADDR']))
{
return '[' . $_SERVER['SERVER_ADDR'] . ']';
}

return '[127.0.0.1]';
}

/**
Expand Down
12 changes: 1 addition & 11 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,17 +753,7 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur
*/
public function __get(string $name)
{
if (parent::__isset($name))
{
return parent::__get($name);
}

if (isset($this->builder()->$name))
{
return $this->builder()->$name;
}

return null;
return parent::__get($name) ?? $this->builder()->$name ?? null;
}

/**
Expand Down
14 changes: 2 additions & 12 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,12 +812,7 @@ public function resource(string $name, array $options = null): RouteCollectionIn

// In order to allow customization of allowed id values
// we need someplace to store them.
$id = $this->placeholders[$this->defaultPlaceholder] ?? '(:segment)';

if (isset($options['placeholder']))
{
$id = $options['placeholder'];
}
$id = $options['placeholder'] ?? $this->placeholders[$this->defaultPlaceholder] ?? '(:segment)';

// Make sure we capture back-references
$id = '(' . trim($id, '()') . ')';
Expand Down Expand Up @@ -925,12 +920,7 @@ public function presenter(string $name, array $options = null): RouteCollectionI

// In order to allow customization of allowed id values
// we need someplace to store them.
$id = $this->placeholders[$this->defaultPlaceholder] ?? '(:segment)';

if (isset($options['placeholder']))
{
$id = $options['placeholder'];
}
$id = $options['placeholder'] ?? $this->placeholders[$this->defaultPlaceholder] ?? '(:segment)';

// Make sure we capture back-references
$id = '(' . trim($id, '()') . ')';
Expand Down
4 changes: 1 addition & 3 deletions system/Test/Mock/MockCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public function get(string $key)
{
$key = static::validateKey($key, $this->prefix);

return array_key_exists($key, $this->cache)
? $this->cache[$key]
: null;
return $this->cache[$key] ?? null;
}

//--------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions utils/PhpCsFixer/CodeIgniter4.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct()
'sets' => ['@all'],
],
'static_lambda' => true,
'ternary_to_null_coalescing' => true,
'yoda_style' => [
'equal' => false,
'identical' => null,
Expand Down

0 comments on commit 9bf2e85

Please sign in to comment.