Skip to content

Commit

Permalink
fix: remove unneeded conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 18, 2023
1 parent c6142f9 commit c0194fb
Show file tree
Hide file tree
Showing 42 changed files with 139 additions and 100 deletions.
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 = $status === null || $status === 0 ? 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 ($folder !== null && $folder !== '' && $folder !== '0' && 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 ($folder !== null && $folder !== '' && $folder !== '0' && 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 ($className === '' || $className === '0') {
if ($className === '') {
return '';
}

Expand Down Expand Up @@ -305,7 +305,7 @@ public function findQualifiedNameFromPath(string $path)
*/
public function listFiles(string $path): array
{
if ($path === '' || $path === '0') {
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 ($path === '' || $path === '0' || ($prefix === '' || $prefix === '0')) {
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 . ($folder === null || $folder === '' || $folder === '0' ? $file : $folder . '/' . $file);
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
$path = realpath($path) ?: $path;

if (is_file($path)) {
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 ($string === null || $string === '' || $string === '0') {
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 = $handler !== null && $handler !== '' && $handler !== '0' ? $handler : $config->handler;
$backup = $backup !== null && $backup !== '' && $backup !== '0' ? $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
17 changes: 11 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"' . ($id !== null && $id !== '' && $id !== '0' ? ' 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' . ($id !== null && $id !== '' && $id !== '0' ? ' 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 ($route !== null && $route !== '' && $route !== '0') {
if ($route !== null) {
return $response->route($route);
}

Expand Down Expand Up @@ -1121,15 +1125,16 @@ 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
*/
function timer(?string $name = null, ?callable $callable = null)
{
$timer = Services::timer();

if ($name === null || $name === '' || $name === '0') {
if ($name === null) {
return $timer;
}

Expand Down
15 changes: 8 additions & 7 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
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 ($key === null || $key === '' || $key === '0' || ! 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 ($orderBy === '' || $orderBy === '0') {
if ($orderBy === '') {
return $this;
}

Expand Down Expand Up @@ -1505,7 +1506,7 @@ public function limit(?int $value = null, ?int $offset = 0)
public function offset(int $offset)
{
if ($offset !== 0) {
$this->QBOffset = (int) $offset;
$this->QBOffset = $offset;
}

return $this;
Expand Down Expand Up @@ -3265,10 +3266,10 @@ protected function isLiteral(string $str): bool
{
$str = trim($str);

if ($str === '' || $str === '0'
|| ctype_digit($str)
|| (string) (float) $str === $str
|| in_array(strtoupper($str), ['TRUE', 'FALSE'], true)
if ($str === ''
|| ctype_digit($str)
|| (string) (float) $str === $str
|| in_array(strtoupper($str), ['TRUE', 'FALSE'], true)
) {
return 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 (isset($dsn['query']) && ($dsn['query'] !== '' && $dsn['query'] !== '0')) {
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 ($group !== '' && $group !== '0') {
if ($group !== '') {
$builder->where('group', $group);
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLSRV/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public function affectedRows(): int
*/
public function setDatabase(?string $databaseName = null)
{
if ($databaseName === null || $databaseName === '' || $databaseName === '0') {
if ($databaseName === null || $databaseName === '') {
$databaseName = $this->database;
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct(Database $config, ?BaseConnection $db = null)
{
$this->seedPath = $config->filesPath ?? APPPATH . 'Database/';

if ($this->seedPath === '' || $this->seedPath === '0') {
if ($this->seedPath === '') {
throw new InvalidArgumentException('Invalid filesPath set in the Config\Database.');
}

Expand Down
2 changes: 1 addition & 1 deletion system/Debug/BaseExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected static function describeMemory(int $bytes): string
*/
protected static function highlightFile(string $file, int $lineNumber, int $lines = 15)
{
if ($file === '' || $file === '0' || ! is_readable($file)) {
if ($file === '' || ! is_readable($file)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public static function describeMemory(int $bytes): string
*/
public static function highlightFile(string $file, int $lineNumber, int $lines = 15)
{
if ($file === '' || $file === '0' || ! is_readable($file)) {
if ($file === '' || ! is_readable($file)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion system/Debug/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function has(string $name): bool
* @param string $name The name of the timer
* @param callable(): mixed $callable callable to be executed
*
* @return array|bool|float|int|object|resource|string|null
* @return mixed
*/
public function record(string $name, callable $callable)
{
Expand Down
2 changes: 1 addition & 1 deletion system/HTTP/CLIRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getPath(): string
{
$path = implode('/', $this->segments);

return $path === '' || $path === '0' ? '' : $path;
return ($path === '') ? '' : $path;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ protected function addToHeader(string $name, $values = null)
$reportSources = [];

foreach ($values as $value => $reportOnly) {
if (is_numeric($value) && is_string($reportOnly) && ($reportOnly !== '' && $reportOnly !== '0')) {
if (is_numeric($value) && is_string($reportOnly) && ($reportOnly !== '')) {
$value = $reportOnly;
$reportOnly = $this->reportOnly;
}
Expand Down
2 changes: 1 addition & 1 deletion system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ protected function detectURI(string $protocol, string $baseURL)
*/
public function detectPath(string $protocol = ''): string
{
if ($protocol === '' || $protocol === '0') {
if ($protocol === '') {
$protocol = 'REQUEST_URI';
}

Expand Down
4 changes: 2 additions & 2 deletions system/HTTP/Negotiate.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function charset(array $supported): string

// If no charset is shown as a match, ignore the directive
// as allowed by the RFC, and tell it a default value.
if ($match === '' || $match === '0') {
if ($match === '') {
return 'utf-8';
}

Expand Down Expand Up @@ -158,7 +158,7 @@ protected function getBestMatch(
throw HTTPException::forEmptySupportedNegotiations();
}

if ($header === null || $header === '' || $header === '0') {
if ($header === null || $header === '') {
return $strictMatch ? '' : $supported[0];
}

Expand Down
6 changes: 3 additions & 3 deletions system/HTTP/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ public function setStatusCode(int $code, string $reason = '')
}

// Unknown and no message?
if (! array_key_exists($code, static::$statusCodes) && ($reason === '' || $reason === '0')) {
if (! array_key_exists($code, static::$statusCodes) && ($reason === '')) {
throw HTTPException::forUnkownStatusCode($code);
}

$this->statusCode = $code;

$this->reason = $reason !== '' && $reason !== '0' ? $reason : static::$statusCodes[$code];
$this->reason = ($reason !== '') ? $reason : static::$statusCodes[$code];

return $this;
}
Expand Down Expand Up @@ -226,7 +226,7 @@ public function setLink(PagerInterface $pager)
public function setContentType(string $mime, string $charset = 'UTF-8')
{
// add charset attribute if not already there and provided as parm
if ((strpos($mime, 'charset=') < 1) && ($charset !== '' && $charset !== '0')) {
if ((strpos($mime, 'charset=') < 1) && ($charset !== '')) {
$mime .= '; charset=' . $charset;
}

Expand Down
Loading

0 comments on commit c0194fb

Please sign in to comment.