Skip to content

Commit 993fa11

Browse files
committed
refactor: fix uses of empty() calls
1 parent ba6c224 commit 993fa11

65 files changed

Lines changed: 230 additions & 531 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

system/Commands/Database/CreateDatabase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function run(array $params)
7979
{
8080
$name = array_shift($params);
8181

82-
if (empty($name)) {
82+
if ($name === null || $name === '') {
8383
$name = CLI::prompt('Database name', null, 'required'); // @codeCoverageIgnore
8484
}
8585

system/Commands/Database/MigrateStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function run(array $params)
105105

106106
$migrations = $runner->findNamespaceMigrations($namespace);
107107

108-
if (empty($migrations)) {
108+
if ($migrations === []) {
109109
continue;
110110
}
111111

system/Commands/Database/Seed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function run(array $params)
7171
$seeder = new Seeder(new Database());
7272
$seedName = array_shift($params);
7373

74-
if (empty($seedName)) {
74+
if ($seedName === null || $seedName === '') {
7575
$seedName = CLI::prompt(lang('Migrations.migSeeder'), null, 'required'); // @codeCoverageIgnore
7676
}
7777

system/Config/BaseService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ protected static function getSharedInstance(string $key, ...$params)
276276
public static function autoloader(bool $getShared = true)
277277
{
278278
if ($getShared) {
279-
if (empty(static::$instances['autoloader'])) {
279+
if (! isset(static::$instances['autoloader'])) {
280280
static::$instances['autoloader'] = new Autoloader();
281281
}
282282

@@ -296,7 +296,7 @@ public static function autoloader(bool $getShared = true)
296296
public static function locator(bool $getShared = true)
297297
{
298298
if ($getShared) {
299-
if (empty(static::$instances['locator'])) {
299+
if (! isset(static::$instances['locator'])) {
300300
$cacheEnabled = class_exists(Optimize::class)
301301
&& (new Optimize())->locatorCacheEnabled;
302302

system/Config/DotEnv.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ protected function setVariable(string $name, string $value = '')
9898
putenv("{$name}={$value}");
9999
}
100100

101-
if (empty($_ENV[$name])) {
101+
if (! isset($_ENV[$name]) || in_array($_ENV[$name], ['', '0'], true)) {
102102
$_ENV[$name] = $value;
103103
}
104104

105-
if (empty($_SERVER[$name])) {
105+
if (! isset($_SERVER[$name]) || in_array($_SERVER[$name], ['', '0'], true)) {
106106
$_SERVER[$name] = $value;
107107
}
108108
}

system/Config/Services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public static function email($config = null, bool $getShared = true)
230230
return static::getSharedInstance('email', $config);
231231
}
232232

233-
if (empty($config) || (! is_array($config) && ! $config instanceof EmailConfig)) {
233+
if (! $config instanceof EmailConfig && (! is_array($config) || $config === [])) {
234234
$config = config(EmailConfig::class);
235235
}
236236

system/Database/BaseBuilder.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class BaseBuilder
306306
*/
307307
public function __construct($tableName, ConnectionInterface $db, ?array $options = null)
308308
{
309-
if (empty($tableName)) {
309+
if ($tableName === '' || $tableName === []) {
310310
throw new DatabaseException('A table must be specified when creating a new Query Builder.');
311311
}
312312

@@ -769,7 +769,7 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
769769
$escape = $this->db->protectIdentifiers;
770770
}
771771

772-
$prefix = empty($this->{$qbKey}) ? $this->groupGetType('') : $this->groupGetType($type);
772+
$prefix = $this->{$qbKey} === [] ? $this->groupGetType('') : $this->groupGetType($type);
773773

774774
foreach ($keyValue as $k => $v) {
775775
if ($rawSqlOnly) {
@@ -778,7 +778,7 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
778778
} elseif ($v !== null) {
779779
$op = $this->getOperatorFromWhereKey($k);
780780

781-
if (! empty($op)) {
781+
if ($op !== false) {
782782
$k = trim($k);
783783

784784
end($op);
@@ -985,7 +985,7 @@ protected function _whereIn(?string $key = null, $values = null, bool $not = fal
985985

986986
$ok = $this->setBind($ok, $whereIn, $escape);
987987

988-
$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);
988+
$prefix = $this->{$clause} === [] ? $this->groupGetType('') : $this->groupGetType($type);
989989

990990
$whereIn = [
991991
'condition' => "{$prefix}{$key}{$not} IN :{$ok}:",
@@ -1125,7 +1125,7 @@ protected function _like($field, string $match = '', string $type = 'AND ', stri
11251125
$v = $match;
11261126
$insensitiveSearch = false;
11271127

1128-
$prefix = empty($this->{$clause}) ? $this->groupGetType('') : $this->groupGetType($type);
1128+
$prefix = $this->{$clause} === [] ? $this->groupGetType('') : $this->groupGetType($type);
11291129

11301130
if ($side === 'none') {
11311131
$bind = $this->setBind($field->getBindingKey(), $v, $escape);
@@ -1357,7 +1357,7 @@ protected function groupStartPrepare(string $not = '', string $type = 'AND ', st
13571357
$type = $this->groupGetType($type);
13581358

13591359
$this->QBWhereGroupStarted = true;
1360-
$prefix = empty($this->{$clause}) ? '' : $type;
1360+
$prefix = $this->{$clause} === [] ? '' : $type;
13611361
$where = [
13621362
'condition' => $prefix . $not . str_repeat(' ', ++$this->QBWhereGroupCount) . ' (',
13631363
'escape' => false,
@@ -1626,7 +1626,7 @@ protected function compileFinalQuery(string $sql): string
16261626
$query = new Query($this->db);
16271627
$query->setQuery($sql, $this->binds, false);
16281628

1629-
if (! empty($this->db->swapPre) && ! empty($this->db->DBPrefix)) {
1629+
if ($this->db->swapPre !== '' && $this->db->DBPrefix !== '') {
16301630
$query->swapPrefix($this->db->DBPrefix, $this->db->swapPre);
16311631
}
16321632

@@ -1683,7 +1683,7 @@ public function countAll(bool $reset = true)
16831683

16841684
$query = $this->db->query($sql, null, false);
16851685

1686-
if (empty($query->getResult())) {
1686+
if ($query->getResult() === []) {
16871687
return 0;
16881688
}
16891689

@@ -1709,7 +1709,7 @@ public function countAllResults(bool $reset = true)
17091709
// for selecting COUNT(*) ...
17101710
$orderBy = [];
17111711

1712-
if (! empty($this->QBOrderBy)) {
1712+
if (is_array($this->QBOrderBy) && $this->QBOrderBy !== []) {
17131713
$orderBy = $this->QBOrderBy;
17141714

17151715
$this->QBOrderBy = null;
@@ -1720,7 +1720,7 @@ public function countAllResults(bool $reset = true)
17201720

17211721
$this->QBLimit = false;
17221722

1723-
if ($this->QBDistinct === true || ! empty($this->QBGroupBy)) {
1723+
if ($this->QBDistinct === true || $this->QBGroupBy !== []) {
17241724
// We need to backup the original SELECT in case DBPrefix is used
17251725
$select = $this->QBSelect;
17261726
$sql = $this->countString . $this->db->protectIdentifiers('numrows') . "\nFROM (\n" . $this->compileSelect() . "\n) CI_count_all_results";
@@ -1749,7 +1749,7 @@ public function countAllResults(bool $reset = true)
17491749

17501750
$row = $result instanceof ResultInterface ? $result->getRow() : null;
17511751

1752-
if (empty($row)) {
1752+
if ($row === null) {
17531753
return 0;
17541754
}
17551755

@@ -1813,7 +1813,7 @@ public function getWhere($where = null, ?int $limit = null, ?int $offset = 0, bo
18131813
*/
18141814
protected function batchExecute(string $renderMethod, int $batchSize = 100)
18151815
{
1816-
if (empty($this->QBSet)) {
1816+
if ($this->QBSet === []) {
18171817
if ($this->db->DBDebug) {
18181818
throw new DatabaseException(trim($renderMethod, '_') . '() has no data.');
18191819
}
@@ -1866,7 +1866,7 @@ protected function batchExecute(string $renderMethod, int $batchSize = 100)
18661866
*/
18671867
public function setData($set, ?bool $escape = null, string $alias = '')
18681868
{
1869-
if (empty($set)) {
1869+
if ($set === []) {
18701870
if ($this->db->DBDebug) {
18711871
throw new DatabaseException('setData() has no data.');
18721872
}
@@ -2073,7 +2073,7 @@ private function setAlias(string $alias): BaseBuilder
20732073
*/
20742074
public function updateFields($set, bool $addToDefault = false, ?array $ignore = null)
20752075
{
2076-
if (! empty($set)) {
2076+
if (! in_array($set, [null, [], ''], true)) {
20772077
if (! is_array($set)) {
20782078
$set = explode(',', $set);
20792079
}
@@ -2109,13 +2109,13 @@ public function updateFields($set, bool $addToDefault = false, ?array $ignore =
21092109
/**
21102110
* Sets constraints for batch upsert, update
21112111
*
2112-
* @param array|RawSql|string $set a string of columns, key value pairs, or RawSql
2112+
* @param array|RawSql|string|null $set A string of columns, key value pairs, or RawSql
21132113
*
21142114
* @return $this
21152115
*/
21162116
public function onConstraint($set)
21172117
{
2118-
if (! empty($set)) {
2118+
if (! in_array($set, [null, [], ''], true)) {
21192119
if (is_string($set)) {
21202120
$set = explode(',', $set);
21212121

@@ -2388,7 +2388,7 @@ protected function removeAlias(string $from): string
23882388
*/
23892389
protected function validateInsert(): bool
23902390
{
2391-
if (empty($this->QBSet)) {
2391+
if ($this->QBSet === []) {
23922392
if ($this->db->DBDebug) {
23932393
throw new DatabaseException('You must use the "set" method to insert an entry.');
23942394
}
@@ -2424,7 +2424,7 @@ public function replace(?array $set = null)
24242424
$this->set($set);
24252425
}
24262426

2427-
if (empty($this->QBSet)) {
2427+
if ($this->QBSet === []) {
24282428
if ($this->db->DBDebug) {
24292429
throw new DatabaseException('You must use the "set" method to update an entry.');
24302430
}
@@ -2576,7 +2576,7 @@ protected function _update(string $table, array $values): string
25762576
*/
25772577
protected function validateUpdate(): bool
25782578
{
2579-
if (empty($this->QBSet)) {
2579+
if ($this->QBSet === []) {
25802580
if ($this->db->DBDebug) {
25812581
throw new DatabaseException('You must use the "set" method to update an entry.');
25822582
}
@@ -2827,7 +2827,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true)
28272827
$this->where($where);
28282828
}
28292829

2830-
if (empty($this->QBWhere)) {
2830+
if ($this->QBWhere === []) {
28312831
if ($this->db->DBDebug) {
28322832
throw new DatabaseException('Deletes are not allowed unless they contain a "where" or "like" clause.');
28332833
}
@@ -2846,7 +2846,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true)
28462846
$this->QBLimit = $limit;
28472847
}
28482848

2849-
if (! empty($this->QBLimit)) {
2849+
if ($this->QBLimit !== false && $this->QBLimit !== 0) {
28502850
if (! $this->canLimitDeletes) {
28512851
throw new DatabaseException('SQLite3 does not allow LIMITs on DELETE queries.');
28522852
}
@@ -3105,7 +3105,7 @@ protected function compileSelect($selectOverride = false): string
31053105
} else {
31063106
$sql = $this->QBDistinct ? 'SELECT DISTINCT ' : 'SELECT ';
31073107

3108-
if (empty($this->QBSelect)) {
3108+
if ($this->QBSelect === []) {
31093109
$sql .= '*';
31103110
} else {
31113111
// Cycle through the "select" portion of the query and prep each column name.
@@ -3124,11 +3124,11 @@ protected function compileSelect($selectOverride = false): string
31243124
}
31253125
}
31263126

3127-
if (! empty($this->QBFrom)) {
3127+
if ($this->QBFrom !== []) {
31283128
$sql .= "\nFROM " . $this->_fromTables();
31293129
}
31303130

3131-
if (! empty($this->QBJoin)) {
3131+
if ($this->QBJoin !== []) {
31323132
$sql .= "\n" . implode("\n", $this->QBJoin);
31333133
}
31343134

@@ -3177,7 +3177,7 @@ protected function compileIgnore(string $statement)
31773177
*/
31783178
protected function compileWhereHaving(string $qbKey): string
31793179
{
3180-
if (! empty($this->{$qbKey})) {
3180+
if ($this->{$qbKey} !== []) {
31813181
foreach ($this->{$qbKey} as &$qbkey) {
31823182
// Is this condition already compiled?
31833183
if (is_string($qbkey)) {
@@ -3266,7 +3266,7 @@ protected function compileWhereHaving(string $qbKey): string
32663266
*/
32673267
protected function compileGroupBy(): string
32683268
{
3269-
if (! empty($this->QBGroupBy)) {
3269+
if ($this->QBGroupBy !== []) {
32703270
foreach ($this->QBGroupBy as &$groupBy) {
32713271
// Is it already compiled?
32723272
if (is_string($groupBy)) {
@@ -3453,7 +3453,7 @@ protected function resetSelect()
34533453
}
34543454

34553455
// Reset QBFrom part
3456-
if (! empty($this->QBFrom)) {
3456+
if ($this->QBFrom !== []) {
34573457
$this->from(array_shift($this->QBFrom), true);
34583458
}
34593459
}

0 commit comments

Comments
 (0)