Skip to content

Commit c0c5fca

Browse files
authored
Merge pull request #8344 from kenjis/add-DisallowedEmptyRuleFixerRector
refactor: apply DisallowedEmptyRuleFixerRector
2 parents 2bf28c0 + d7d3959 commit c0c5fca

Some content is hidden

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

52 files changed

+204
-222
lines changed

phpstan-baseline.php

Lines changed: 34 additions & 99 deletions
Large diffs are not rendered by default.

rector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
2424
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
2525
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
26+
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
2627
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
2728
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
2829
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
@@ -43,7 +44,9 @@
4344
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
4445
use Rector\Set\ValueObject\LevelSetList;
4546
use Rector\Set\ValueObject\SetList;
47+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
4648
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
49+
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
4750
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
4851
use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector;
4952
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;
@@ -139,6 +142,9 @@
139142
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
140143
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
141144
$rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class);
145+
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class);
146+
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
147+
$rectorConfig->rule(DisallowedEmptyRuleFixerRector::class);
142148
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
143149
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
144150
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);

system/API/ResponseTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function respond($data = null, ?int $status = null, string $message =
9494
$output = null;
9595
$this->format($data);
9696
} else {
97-
$status = empty($status) ? 200 : $status;
97+
$status ??= 200;
9898
$output = $this->format($data);
9999
}
100100

system/Autoloader/FileLocator.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public function __construct(Autoloader $autoloader)
3535
* Attempts to locate a file by examining the name for a namespace
3636
* and looking through the PSR-4 namespaced files that we know about.
3737
*
38-
* @param string $file The relative file path or namespaced file to
39-
* locate. If not namespaced, search in the app
40-
* folder.
41-
* @param string|null $folder The folder within the namespace that we should
42-
* look for the file. If $file does not contain
43-
* this value, it will be appended to the namespace
44-
* folder.
45-
* @param string $ext The file extension the file should have.
38+
* @param string $file The relative file path or namespaced file to
39+
* locate. If not namespaced, search in the app
40+
* folder.
41+
* @param non-empty-string|null $folder The folder within the namespace that we should
42+
* look for the file. If $file does not contain
43+
* this value, it will be appended to the namespace
44+
* folder.
45+
* @param string $ext The file extension the file should have.
4646
*
4747
* @return false|string The path to the file, or false if not found.
4848
*/
@@ -51,7 +51,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
5151
$file = $this->ensureExt($file, $ext);
5252

5353
// Clears the folder name if it is at the beginning of the filename
54-
if (! empty($folder) && strpos($file, $folder) === 0) {
54+
if ($folder !== null && strpos($file, $folder) === 0) {
5555
$file = substr($file, strlen($folder . '/'));
5656
}
5757

@@ -101,7 +101,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
101101
// If we have a folder name, then the calling function
102102
// expects this file to be within that folder, like 'Views',
103103
// or 'libraries'.
104-
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false) {
104+
if ($folder !== null && strpos($path . $filename, '/' . $folder . '/') === false) {
105105
$path .= trim($folder, '/') . '/';
106106
}
107107

@@ -154,7 +154,7 @@ public function getClassname(string $file): string
154154
}
155155
}
156156

157-
if (empty($className)) {
157+
if ($className === '') {
158158
return '';
159159
}
160160

@@ -305,7 +305,7 @@ public function findQualifiedNameFromPath(string $path)
305305
*/
306306
public function listFiles(string $path): array
307307
{
308-
if (empty($path)) {
308+
if ($path === '') {
309309
return [];
310310
}
311311

@@ -338,7 +338,7 @@ public function listFiles(string $path): array
338338
*/
339339
public function listNamespaceFiles(string $prefix, string $path): array
340340
{
341-
if (empty($path) || empty($prefix)) {
341+
if ($path === '' || ($prefix === '')) {
342342
return [];
343343
}
344344

@@ -368,11 +368,13 @@ public function listNamespaceFiles(string $prefix, string $path): array
368368
* Checks the app folder to see if the file can be found.
369369
* Only for use with filenames that DO NOT include namespacing.
370370
*
371+
* @param non-empty-string|null $folder
372+
*
371373
* @return false|string The path to the file, or false if not found.
372374
*/
373375
protected function legacyLocate(string $file, ?string $folder = null)
374376
{
375-
$path = APPPATH . (empty($folder) ? $file : $folder . '/' . $file);
377+
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
376378
$path = realpath($path) ?: $path;
377379

378380
if (is_file($path)) {

system/BaseModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ public function getValidationMessages(): array
15791579
*/
15801580
protected function cleanValidationRules(array $rules, ?array $row = null): array
15811581
{
1582-
if (empty($row)) {
1582+
if ($row === null || $row === []) {
15831583
return [];
15841584
}
15851585

@@ -1795,7 +1795,7 @@ protected function transformDataToArray($row, string $type): array
17951795
}
17961796

17971797
// If it's still empty here, means $row is no change or is empty object
1798-
if (! $this->allowEmptyInserts && empty($row)) {
1798+
if (! $this->allowEmptyInserts && ($row === null || $row === [])) {
17991799
throw DataException::forEmptyDataset($type);
18001800
}
18011801

system/CLI/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
849849
*/
850850
public static function wrap(?string $string = null, int $max = 0, int $padLeft = 0): string
851851
{
852-
if (empty($string)) {
852+
if ($string === null || $string === '') {
853853
return '';
854854
}
855855

system/Cache/CacheFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class CacheFactory
4040
/**
4141
* Attempts to create the desired cache handler, based upon the
4242
*
43+
* @param non-empty-string|null $handler
44+
* @param non-empty-string|null $backup
45+
*
4346
* @return CacheInterface
4447
*/
4548
public static function getHandler(Cache $config, ?string $handler = null, ?string $backup = null)
@@ -52,8 +55,8 @@ public static function getHandler(Cache $config, ?string $handler = null, ?strin
5255
throw CacheException::forNoBackup();
5356
}
5457

55-
$handler = ! empty($handler) ? $handler : $config->handler;
56-
$backup = ! empty($backup) ? $backup : $config->backupHandler;
58+
$handler ??= $config->handler;
59+
$backup ??= $config->backupHandler;
5760

5861
if (! array_key_exists($handler, $config->validHandlers) || ! array_key_exists($backup, $config->validHandlers)) {
5962
throw CacheException::forHandlerNotFound();

system/Common.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,24 @@ function csrf_hash(): string
288288
if (! function_exists('csrf_field')) {
289289
/**
290290
* Generates a hidden input field for use within manually generated forms.
291+
*
292+
* @param non-empty-string|null $id
291293
*/
292294
function csrf_field(?string $id = null): string
293295
{
294-
return '<input type="hidden"' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
296+
return '<input type="hidden"' . ($id !== null ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
295297
}
296298
}
297299

298300
if (! function_exists('csrf_meta')) {
299301
/**
300302
* Generates a meta tag for use within javascript calls.
303+
*
304+
* @param non-empty-string|null $id
301305
*/
302306
function csrf_meta(?string $id = null): string
303307
{
304-
return '<meta' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '"' . _solidus() . '>';
308+
return '<meta' . ($id !== null ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '"' . _solidus() . '>';
305309
}
306310
}
307311

@@ -850,13 +854,13 @@ function old(string $key, $default = null, $escape = 'html')
850854
*
851855
* If more control is needed, you must use $response->redirect explicitly.
852856
*
853-
* @param string|null $route Route name or Controller::method
857+
* @param non-empty-string|null $route Route name or Controller::method
854858
*/
855859
function redirect(?string $route = null): RedirectResponse
856860
{
857861
$response = Services::redirectresponse(null, true);
858862

859-
if (! empty($route)) {
863+
if ($route !== null) {
860864
return $response->route($route);
861865
}
862866

@@ -1121,15 +1125,17 @@ function stringify_attributes($attributes, bool $js = false): string
11211125
* returns its return value if any.
11221126
* Otherwise will start or stop the timer intelligently.
11231127
*
1128+
* @param non-empty-string|null $name
11241129
* @param (callable(): mixed)|null $callable
11251130
*
1126-
* @return Timer
1131+
* @return mixed|Timer
1132+
* @phpstan-return ($name is null ? Timer : ($callable is (callable(): mixed) ? mixed : Timer))
11271133
*/
11281134
function timer(?string $name = null, ?callable $callable = null)
11291135
{
11301136
$timer = Services::timer();
11311137

1132-
if (empty($name)) {
1138+
if ($name === null) {
11331139
return $timer;
11341140
}
11351141

system/Database/BaseBuilder.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public function __construct($tableName, ConnectionInterface $db, ?array $options
318318

319319
$this->from($tableName);
320320

321-
if (! empty($options)) {
321+
if ($options !== null && $options !== []) {
322322
foreach ($options as $key => $value) {
323323
if (property_exists($this, $key)) {
324324
$this->{$key} = $value;
@@ -920,6 +920,7 @@ public function orHavingNotIn(?string $key = null, $values = null, ?bool $escape
920920
* @used-by whereNotIn()
921921
* @used-by orWhereNotIn()
922922
*
923+
* @param non-empty-string|null $key
923924
* @param array|BaseBuilder|Closure|null $values The values searched on, or anonymous function with subquery
924925
*
925926
* @return $this
@@ -928,7 +929,7 @@ public function orHavingNotIn(?string $key = null, $values = null, ?bool $escape
928929
*/
929930
protected function _whereIn(?string $key = null, $values = null, bool $not = false, string $type = 'AND ', ?bool $escape = null, string $clause = 'QBWhere')
930931
{
931-
if (empty($key) || ! is_string($key)) {
932+
if ($key === null || $key === '') {
932933
throw new InvalidArgumentException(sprintf('%s() expects $key to be a non-empty string', debug_backtrace(0, 2)[1]['function']));
933934
}
934935

@@ -1434,7 +1435,7 @@ public function orHaving($key, $value = null, ?bool $escape = null)
14341435
public function orderBy(string $orderBy, string $direction = '', ?bool $escape = null)
14351436
{
14361437
$qbOrderBy = [];
1437-
if (empty($orderBy)) {
1438+
if ($orderBy === '') {
14381439
return $this;
14391440
}
14401441

@@ -1490,7 +1491,7 @@ public function limit(?int $value = null, ?int $offset = 0)
14901491
$this->QBLimit = $value;
14911492
}
14921493

1493-
if (! empty($offset)) {
1494+
if ($offset !== null && $offset !== 0) {
14941495
$this->QBOffset = $offset;
14951496
}
14961497

@@ -1504,8 +1505,8 @@ public function limit(?int $value = null, ?int $offset = 0)
15041505
*/
15051506
public function offset(int $offset)
15061507
{
1507-
if (! empty($offset)) {
1508-
$this->QBOffset = (int) $offset;
1508+
if ($offset !== 0) {
1509+
$this->QBOffset = $offset;
15091510
}
15101511

15111512
return $this;
@@ -1736,7 +1737,7 @@ public function getWhere($where = null, ?int $limit = null, ?int $offset = 0, bo
17361737
$this->where($where);
17371738
}
17381739

1739-
if (! empty($limit)) {
1740+
if ($limit !== null && $limit !== 0) {
17401741
$this->limit($limit, $offset);
17411742
}
17421743

@@ -2452,7 +2453,7 @@ public function update($set = null, $where = null, ?int $limit = null): bool
24522453
$this->where($where);
24532454
}
24542455

2455-
if (! empty($limit)) {
2456+
if ($limit !== null && $limit !== 0) {
24562457
if (! $this->canLimitWhereUpdates) {
24572458
throw new DatabaseException('This driver does not allow LIMITs on UPDATE queries using WHERE.');
24582459
}
@@ -2762,7 +2763,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true)
27622763

27632764
$sql = $this->_delete($this->removeAlias($table));
27642765

2765-
if (! empty($limit)) {
2766+
if ($limit !== null && $limit !== 0) {
27662767
$this->QBLimit = $limit;
27672768
}
27682769

@@ -3172,7 +3173,7 @@ protected function compileGroupBy(): string
31723173
*/
31733174
protected function compileOrderBy(): string
31743175
{
3175-
if (is_array($this->QBOrderBy) && ! empty($this->QBOrderBy)) {
3176+
if (is_array($this->QBOrderBy) && $this->QBOrderBy !== []) {
31763177
foreach ($this->QBOrderBy as &$orderBy) {
31773178
if ($orderBy['escape'] !== false && ! $this->isLiteral($orderBy['field'])) {
31783179
$orderBy['field'] = $this->db->protectIdentifiers($orderBy['field']);
@@ -3265,7 +3266,7 @@ protected function isLiteral(string $str): bool
32653266
{
32663267
$str = trim($str);
32673268

3268-
if (empty($str)
3269+
if ($str === ''
32693270
|| ctype_digit($str)
32703271
|| (string) (float) $str === $str
32713272
|| in_array(strtoupper($str), ['TRUE', 'FALSE'], true)

system/Database/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected function parseDSN(array $params): array
104104
'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : '',
105105
];
106106

107-
if (! empty($dsn['query'])) {
107+
if (isset($dsn['query']) && ($dsn['query'] !== '')) {
108108
parse_str($dsn['query'], $extra);
109109

110110
foreach ($extra as $key => $val) {

system/Database/MigrationRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public function getHistory(string $group = 'default'): array
639639
$builder = $this->db->table($this->table);
640640

641641
// If group was specified then use it
642-
if (! empty($group)) {
642+
if ($group !== '') {
643643
$builder->where('group', $group);
644644
}
645645

system/Database/MySQLi/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function connect(bool $persistent = false)
150150
$ssl['cipher'] = $this->encrypt['ssl_cipher'];
151151
}
152152

153-
if (! empty($ssl)) {
153+
if ($ssl !== []) {
154154
if (isset($this->encrypt['ssl_verify'])) {
155155
if ($this->encrypt['ssl_verify']) {
156156
if (defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT')) {

system/Database/OCI8/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected function _truncate(string $table): string
166166
*/
167167
public function delete($where = '', ?int $limit = null, bool $resetData = true)
168168
{
169-
if (! empty($limit)) {
169+
if ($limit !== null && $limit !== 0) {
170170
$this->QBLimit = $limit;
171171
}
172172

system/Database/Postgre/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
229229
*/
230230
public function delete($where = '', ?int $limit = null, bool $resetData = true)
231231
{
232-
if (! empty($limit) || ! empty($this->QBLimit)) {
232+
if ($limit !== null && $limit !== 0 || ! empty($this->QBLimit)) {
233233
throw new DatabaseException('PostgreSQL does not allow LIMITs on DELETE queries.');
234234
}
235235

system/Database/SQLSRV/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true)
522522
return false; // @codeCoverageIgnore
523523
}
524524

525-
if (! empty($limit)) {
525+
if ($limit !== null && $limit !== 0) {
526526
$this->QBLimit = $limit;
527527
}
528528

0 commit comments

Comments
 (0)