Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: apply DisallowedEmptyRuleFixerRector #8344

Merged
merged 15 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 34 additions & 99 deletions phpstan-baseline.php

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
Expand All @@ -43,7 +44,9 @@
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector;
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;
Expand Down Expand Up @@ -139,6 +142,9 @@
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
$rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class);
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class);
$rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class);
$rectorConfig->rule(DisallowedEmptyRuleFixerRector::class);
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
Expand Down
2 changes: 1 addition & 1 deletion system/API/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function respond($data = null, ?int $status = null, string $message =
$output = null;
$this->format($data);
} else {
$status = empty($status) ? 200 : $status;
$status ??= 200;
$output = $this->format($data);
}

Expand Down
30 changes: 16 additions & 14 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public function __construct(Autoloader $autoloader)
* Attempts to locate a file by examining the name for a namespace
* and looking through the PSR-4 namespaced files that we know about.
*
* @param string $file The relative file path or namespaced file to
* locate. If not namespaced, search in the app
* folder.
* @param string|null $folder The folder within the namespace that we should
* look for the file. If $file does not contain
* this value, it will be appended to the namespace
* folder.
* @param string $ext The file extension the file should have.
* @param string $file The relative file path or namespaced file to
* locate. If not namespaced, search in the app
* folder.
* @param non-empty-string|null $folder The folder within the namespace that we should
* look for the file. If $file does not contain
* this value, it will be appended to the namespace
* folder.
* @param string $ext The file extension the file should have.
*
* @return false|string The path to the file, or false if not found.
*/
Expand All @@ -51,7 +51,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
$file = $this->ensureExt($file, $ext);

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

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

Expand Down Expand Up @@ -154,7 +154,7 @@ public function getClassname(string $file): string
}
}

if (empty($className)) {
if ($className === '') {
return '';
}

Expand Down Expand Up @@ -305,7 +305,7 @@ public function findQualifiedNameFromPath(string $path)
*/
public function listFiles(string $path): array
{
if (empty($path)) {
if ($path === '') {
return [];
}

Expand Down Expand Up @@ -338,7 +338,7 @@ public function listFiles(string $path): array
*/
public function listNamespaceFiles(string $prefix, string $path): array
{
if (empty($path) || empty($prefix)) {
if ($path === '' || ($prefix === '')) {
return [];
}

Expand Down Expand Up @@ -368,11 +368,13 @@ public function listNamespaceFiles(string $prefix, string $path): array
* Checks the app folder to see if the file can be found.
* Only for use with filenames that DO NOT include namespacing.
*
* @param non-empty-string|null $folder
*
* @return false|string The path to the file, or false if not found.
*/
protected function legacyLocate(string $file, ?string $folder = null)
{
$path = APPPATH . (empty($folder) ? $file : $folder . '/' . $file);
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
$path = realpath($path) ?: $path;

if (is_file($path)) {
Expand Down
4 changes: 2 additions & 2 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ public function getValidationMessages(): array
*/
protected function cleanValidationRules(array $rules, ?array $row = null): array
{
if (empty($row)) {
if ($row === null || $row === []) {
return [];
}

Expand Down Expand Up @@ -1795,7 +1795,7 @@ protected function transformDataToArray($row, string $type): array
}

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

Expand Down
2 changes: 1 addition & 1 deletion system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
*/
public static function wrap(?string $string = null, int $max = 0, int $padLeft = 0): string
{
if (empty($string)) {
if ($string === null || $string === '') {
return '';
}

Expand Down
7 changes: 5 additions & 2 deletions system/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class CacheFactory
/**
* Attempts to create the desired cache handler, based upon the
*
* @param non-empty-string|null $handler
* @param non-empty-string|null $backup
*
* @return CacheInterface
*/
public static function getHandler(Cache $config, ?string $handler = null, ?string $backup = null)
Expand All @@ -52,8 +55,8 @@ public static function getHandler(Cache $config, ?string $handler = null, ?strin
throw CacheException::forNoBackup();
}

$handler = ! empty($handler) ? $handler : $config->handler;
$backup = ! empty($backup) ? $backup : $config->backupHandler;
$handler ??= $config->handler;
$backup ??= $config->backupHandler;

if (! array_key_exists($handler, $config->validHandlers) || ! array_key_exists($backup, $config->validHandlers)) {
throw CacheException::forHandlerNotFound();
Expand Down
18 changes: 12 additions & 6 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,24 @@ function csrf_hash(): string
if (! function_exists('csrf_field')) {
/**
* Generates a hidden input field for use within manually generated forms.
*
* @param non-empty-string|null $id
*/
function csrf_field(?string $id = null): string
{
return '<input type="hidden"' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
return '<input type="hidden"' . ($id !== null ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
}
}

if (! function_exists('csrf_meta')) {
/**
* Generates a meta tag for use within javascript calls.
*
* @param non-empty-string|null $id
*/
function csrf_meta(?string $id = null): string
{
return '<meta' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '"' . _solidus() . '>';
return '<meta' . ($id !== null ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '"' . _solidus() . '>';
}
}

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

if (! empty($route)) {
if ($route !== null) {
return $response->route($route);
}

Expand Down Expand Up @@ -1121,15 +1125,17 @@ function stringify_attributes($attributes, bool $js = false): string
* returns its return value if any.
* Otherwise will start or stop the timer intelligently.
*
* @param non-empty-string|null $name
* @param (callable(): mixed)|null $callable
*
* @return Timer
* @return mixed|Timer
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
* @phpstan-return ($name is null ? Timer : ($callable is (callable(): mixed) ? mixed : Timer))
*/
function timer(?string $name = null, ?callable $callable = null)
{
$timer = Services::timer();

if (empty($name)) {
if ($name === null) {
return $timer;
}

Expand Down
23 changes: 12 additions & 11 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function __construct($tableName, ConnectionInterface $db, ?array $options

$this->from($tableName);

if (! empty($options)) {
if ($options !== null && $options !== []) {
foreach ($options as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
Expand Down Expand Up @@ -920,6 +920,7 @@ public function orHavingNotIn(?string $key = null, $values = null, ?bool $escape
* @used-by whereNotIn()
* @used-by orWhereNotIn()
*
* @param non-empty-string|null $key
* @param array|BaseBuilder|Closure|null $values The values searched on, or anonymous function with subquery
*
* @return $this
Expand All @@ -928,7 +929,7 @@ public function orHavingNotIn(?string $key = null, $values = null, ?bool $escape
*/
protected function _whereIn(?string $key = null, $values = null, bool $not = false, string $type = 'AND ', ?bool $escape = null, string $clause = 'QBWhere')
{
if (empty($key) || ! is_string($key)) {
if ($key === null || $key === '') {
throw new InvalidArgumentException(sprintf('%s() expects $key to be a non-empty string', debug_backtrace(0, 2)[1]['function']));
}

Expand Down Expand Up @@ -1434,7 +1435,7 @@ public function orHaving($key, $value = null, ?bool $escape = null)
public function orderBy(string $orderBy, string $direction = '', ?bool $escape = null)
{
$qbOrderBy = [];
if (empty($orderBy)) {
if ($orderBy === '') {
return $this;
}

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

if (! empty($offset)) {
if ($offset !== null && $offset !== 0) {
$this->QBOffset = $offset;
}

Expand All @@ -1504,8 +1505,8 @@ public function limit(?int $value = null, ?int $offset = 0)
*/
public function offset(int $offset)
{
if (! empty($offset)) {
$this->QBOffset = (int) $offset;
if ($offset !== 0) {
$this->QBOffset = $offset;
}

return $this;
Expand Down Expand Up @@ -1736,7 +1737,7 @@ public function getWhere($where = null, ?int $limit = null, ?int $offset = 0, bo
$this->where($where);
}

if (! empty($limit)) {
if ($limit !== null && $limit !== 0) {
$this->limit($limit, $offset);
}

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

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

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

if (! empty($limit)) {
if ($limit !== null && $limit !== 0) {
$this->QBLimit = $limit;
}

Expand Down Expand Up @@ -3172,7 +3173,7 @@ protected function compileGroupBy(): string
*/
protected function compileOrderBy(): string
{
if (is_array($this->QBOrderBy) && ! empty($this->QBOrderBy)) {
if (is_array($this->QBOrderBy) && $this->QBOrderBy !== []) {
foreach ($this->QBOrderBy as &$orderBy) {
if ($orderBy['escape'] !== false && ! $this->isLiteral($orderBy['field'])) {
$orderBy['field'] = $this->db->protectIdentifiers($orderBy['field']);
Expand Down Expand Up @@ -3265,7 +3266,7 @@ protected function isLiteral(string $str): bool
{
$str = trim($str);

if (empty($str)
if ($str === ''
|| ctype_digit($str)
|| (string) (float) $str === $str
|| in_array(strtoupper($str), ['TRUE', 'FALSE'], true)
Expand Down
2 changes: 1 addition & 1 deletion system/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function parseDSN(array $params): array
'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : '',
];

if (! empty($dsn['query'])) {
if (isset($dsn['query']) && ($dsn['query'] !== '')) {
parse_str($dsn['query'], $extra);

foreach ($extra as $key => $val) {
Expand Down
2 changes: 1 addition & 1 deletion system/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public function getHistory(string $group = 'default'): array
$builder = $this->db->table($this->table);

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

Expand Down
2 changes: 1 addition & 1 deletion system/Database/MySQLi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function connect(bool $persistent = false)
$ssl['cipher'] = $this->encrypt['ssl_cipher'];
}

if (! empty($ssl)) {
if ($ssl !== []) {
if (isset($this->encrypt['ssl_verify'])) {
if ($this->encrypt['ssl_verify']) {
if (defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT')) {
Expand Down
2 changes: 1 addition & 1 deletion system/Database/OCI8/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function _truncate(string $table): string
*/
public function delete($where = '', ?int $limit = null, bool $resetData = true)
{
if (! empty($limit)) {
if ($limit !== null && $limit !== 0) {
$this->QBLimit = $limit;
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
*/
public function delete($where = '', ?int $limit = null, bool $resetData = true)
{
if (! empty($limit) || ! empty($this->QBLimit)) {
if ($limit !== null && $limit !== 0 || ! empty($this->QBLimit)) {
throw new DatabaseException('PostgreSQL does not allow LIMITs on DELETE queries.');
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true)
return false; // @codeCoverageIgnore
}

if (! empty($limit)) {
if ($limit !== null && $limit !== 0) {
$this->QBLimit = $limit;
}

Expand Down
Loading
Loading