From e1d4cb4f80750a86ea8007f6d1fff9e72c527479 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 12 Aug 2026 14:07:17 +0800 Subject: [PATCH] refactor: fix `ternary.shortNotAllowed` errors --- system/CLI/CLI.php | 4 +- system/Commands/Utilities/Namespaces.php | 3 +- system/Common.php | 9 +- system/Cookie/Cookie.php | 8 +- system/Debug/Toolbar/Collectors/Database.php | 2 +- system/Files/File.php | 11 +- system/HTTP/CURLRequest.php | 3 +- system/HTTP/ResponseTrait.php | 6 +- system/Helpers/filesystem_helper.php | 8 +- system/I18n/TimeTrait.php | 2 +- system/Pager/Pager.php | 4 +- system/Router/AutoRouter.php | 3 +- system/Test/Mock/MockCache.php | 4 +- system/Validation/Validation.php | 3 +- system/View/View.php | 4 +- .../Commands/Generators/CellGeneratorTest.php | 2 +- .../Generators/CommandGeneratorTest.php | 2 +- .../Generators/ControllerGeneratorTest.php | 2 +- .../Generators/ModelGeneratorTest.php | 2 +- .../Generators/ScaffoldGeneratorTest.php | 2 +- .../system/Publisher/PublisherSupportTest.php | 3 +- utils/phpstan-baseline/loader.neon | 3 +- .../ternary.shortNotAllowed.neon | 113 ------------------ 23 files changed, 54 insertions(+), 149 deletions(-) delete mode 100644 utils/phpstan-baseline/ternary.shortNotAllowed.neon diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index aff27144aa39..037eaf878ebb 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -741,7 +741,7 @@ public static function getWidth(int $default = 80): int static::generateDimensions(); } - return static::$width ?: $default; + return (static::$width === null || static::$width === 0) ? $default : static::$width; } /** @@ -753,7 +753,7 @@ public static function getHeight(int $default = 32): int static::generateDimensions(); } - return static::$height ?: $default; + return (static::$height === null || static::$height === 0) ? $default : static::$height; } /** diff --git a/system/Commands/Utilities/Namespaces.php b/system/Commands/Utilities/Namespaces.php index 8dda8ce0732e..6469d1afc97f 100644 --- a/system/Commands/Utilities/Namespaces.php +++ b/system/Commands/Utilities/Namespaces.php @@ -145,7 +145,8 @@ private function outputCINamespaces(array $params): array $pathOutput = $this->truncate(clean_path($path), $maxLength); } - $path = realpath($path) ?: $path; + $realPath = realpath($path); + $path = $realPath === false ? $path : $realPath; $tbody[] = [ $ns, diff --git a/system/Common.php b/system/Common.php index d99b4ef3ca3c..32f66d1803ee 100644 --- a/system/Common.php +++ b/system/Common.php @@ -95,7 +95,8 @@ function clean_path(string $path): string { // Resolve relative paths try { - $path = realpath($path) ?: $path; + $realPath = realpath($path); + $path = $realPath === false ? $path : $realPath; } catch (ErrorException|ValueError) { $path = 'error file path: ' . urlencode($path); } @@ -1347,7 +1348,11 @@ function class_uses_recursive($class) */ function trait_uses_recursive($trait) { - $traits = class_uses($trait) ?: []; + $traits = class_uses($trait); + + if ($traits === false) { + return []; + } foreach ($traits as $trait) { $traits += trait_uses_recursive($trait); diff --git a/system/Cookie/Cookie.php b/system/Cookie/Cookie.php index 068f8772c71c..61de7d6ce672 100644 --- a/system/Cookie/Cookie.php +++ b/system/Cookie/Cookie.php @@ -254,11 +254,11 @@ final public function __construct(string $name, string $value = '', array $optio // to preserve backward compatibility with array-based cookies in previous CI versions $prefix = ($options['prefix'] === '') ? self::$defaults['prefix'] : $options['prefix']; - $path = $options['path'] ?: self::$defaults['path']; - $domain = $options['domain'] ?: self::$defaults['domain']; + $path = ($options['path'] === '') ? self::$defaults['path'] : $options['path']; + $domain = ($options['domain'] === '') ? self::$defaults['domain'] : $options['domain']; // empty string SameSite should use the default for browsers - $samesite = $options['samesite'] ?: self::$defaults['samesite']; + $samesite = ($options['samesite'] === '') ? self::$defaults['samesite'] : $options['samesite']; $raw = $options['raw']; $secure = $options['secure']; @@ -429,7 +429,7 @@ public function getOptions(): array 'domain' => $this->domain, 'secure' => $this->secure, 'httponly' => $this->httponly, - 'samesite' => $this->samesite ?: ucfirst(self::SAMESITE_LAX), + 'samesite' => ($this->samesite === '') ? ucfirst(self::SAMESITE_LAX) : $this->samesite, ]; } diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index fdf961d2f974..4d5a06f40486 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -88,7 +88,7 @@ public static function collect(Query $query) $config = config(Toolbar::class); // Provide default in case it's not set - $max = $config->maxQueries ?: 100; + $max = ($config->maxQueries === 0) ? 100 : $config->maxQueries; if (count(static::$queries) < $max) { $queryString = $query->getQuery(); diff --git a/system/Files/File.php b/system/Files/File.php index 8a06b29edb81..181a9976362e 100644 --- a/system/Files/File.php +++ b/system/Files/File.php @@ -111,7 +111,8 @@ public function getSizeByUnit(string $unit = 'b') public function guessExtension(): ?string { // naively get the path extension using pathinfo - $pathinfo = pathinfo($this->getRealPath() ?: $this->__toString()) + ['extension' => '']; + $realPath = $this->getRealPath(); + $pathinfo = pathinfo($realPath === false ? $this->__toString() : $realPath) + ['extension' => '']; $proposedExtension = $pathinfo['extension']; @@ -131,9 +132,10 @@ public function getMimeType(): string return $this->originalMimeType ?? 'application/octet-stream'; // @codeCoverageIgnore } - $finfo = finfo_open(FILEINFO_MIME_TYPE); + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $realPath = $this->getRealPath(); - return finfo_file($finfo, $this->getRealPath() ?: $this->__toString()); + return finfo_file($finfo, $realPath === false ? $this->__toString() : $realPath); } /** @@ -159,7 +161,8 @@ public function move(string $targetPath, ?string $name = null, bool $overwrite = $name ??= $this->getBasename(); $destination = $overwrite ? $targetPath . $name : $this->getDestination($targetPath . $name); - $oldName = $this->getRealPath() ?: $this->__toString(); + $realPath = $this->getRealPath(); + $oldName = $realPath === false ? $this->__toString() : $realPath; if (! @rename($oldName, $destination)) { $error = error_get_last(); diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index e1d9d3316b5e..b75c477ab9e1 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -587,7 +587,8 @@ private function applySslOptions(array $curlOptions, array $config): array // SSL Verification if (isset($config['verify'])) { if (is_string($config['verify'])) { - $file = realpath($config['verify']) ?: $config['verify']; + $realPath = realpath($config['verify']); + $file = $realPath === false ? $config['verify'] : $realPath; if (! is_file($file)) { throw HTTPException::forInvalidSSLKey($config['verify']); diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index 265a62fb0a1f..00f2dcc7ec40 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -195,7 +195,7 @@ public function getJSON() $body = service('format')->getFormatter('application/json')->format($body); } - return $body ?: null; + return ($body === null || $body === '') ? null : $body; } /** @@ -541,10 +541,12 @@ public function setCookie( if (is_numeric($expire)) { $expire = $expire > 0 ? Time::now()->getTimestamp() + $expire : 0; + } else { + $expire = 0; } $cookie = new Cookie($name, $value, [ - 'expires' => $expire ?: 0, + 'expires' => $expire, 'domain' => $domain, 'path' => $path, 'prefix' => $prefix, diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index b33ad49a2823..3f5ed25f8531 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -155,8 +155,9 @@ function write_file(string $path, string $data, string $mode = 'wb'): bool */ function delete_files(string $path, bool $delDir = false, bool $htdocs = false, bool $hidden = false): bool { - $path = realpath($path) ?: $path; - $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + $realPath = realpath($path); + $path = $realPath === false ? $path : $realPath; + $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; try { foreach (new RecursiveIteratorIterator( @@ -208,7 +209,8 @@ function get_filenames( ): array { $files = []; - $sourceDir = realpath($sourceDir) ?: $sourceDir; + $realPath = realpath($sourceDir); + $sourceDir = $realPath === false ? $sourceDir : $realPath; $sourceDir = rtrim($sourceDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; try { diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index 3c76f117f99e..3189c2076f0a 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -87,7 +87,7 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc } } - $timezone = $timezone ?: date_default_timezone_get(); + $timezone = ($timezone === null || $timezone === '') ? date_default_timezone_get() : $timezone; $this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); // If the time string was a relative string (i.e. 'next Tuesday') diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index 1a01de3f1aa9..cb151a603a40 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -216,7 +216,9 @@ public function getCurrentPage(string $group = 'default'): int { $this->ensureGroup($group); - return $this->groups[$group]['currentPage'] ?: 1; + $currentPage = $this->groups[$group]['currentPage']; + + return ($currentPage === 0) ? 1 : $currentPage; } /** diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index 3a81f3a7bc6b..1033478c9e30 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -86,7 +86,8 @@ public function getRoute(string $uri, string $httpVerb): array // If it doesn't, no biggie - the default method name // has already been set. if ($segments !== []) { - $this->method = array_shift($segments) ?: $this->method; + $method = array_shift($segments); + $this->method = $method === '' ? $this->method : $method; } // Prevent access to initController method diff --git a/system/Test/Mock/MockCache.php b/system/Test/Mock/MockCache.php index 27af8a1ad213..5b736954a536 100644 --- a/system/Test/Mock/MockCache.php +++ b/system/Test/Mock/MockCache.php @@ -144,7 +144,7 @@ public function deleteMatching(string $pattern): int public function increment(string $key, int $offset = 1): bool { $key = static::validateKey($key, $this->prefix); - $data = $this->cache[$key] ?: null; + $data = $this->cache[$key] ?? null; if ($data === null) { $data = 0; @@ -162,7 +162,7 @@ public function decrement(string $key, int $offset = 1): bool { $key = static::validateKey($key, $this->prefix); - $data = $this->cache[$key] ?: null; + $data = $this->cache[$key] ?? null; if ($data === null) { $data = 0; diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 38210fbdb705..ceaa557ea404 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -985,7 +985,8 @@ protected function splitRules(string $rules): array ) { // the pipe is inside the brackets causing the closing bracket to // not be included. so, we adjust the rule to include that portion. - $pos = strpos($string, '|', $cursor + strlen($rule) + 1) ?: $length; + $next = strpos($string, '|', $cursor + strlen($rule) + 1); + $pos = $next === false ? $length : $next; $rule = substr($string, $cursor, $pos - $cursor); } diff --git a/system/View/View.php b/system/View/View.php index 83b19a9313f6..e63de2bd6bb0 100644 --- a/system/View/View.php +++ b/system/View/View.php @@ -239,7 +239,7 @@ public function render(string $view, ?array $options = null, ?bool $saveData = n ob_start(); include $this->renderVars['file']; - return ob_get_clean() ?: ''; + return (string) ob_get_clean(); })(); // Get back current vars @@ -331,7 +331,7 @@ public function renderString(string $view, ?array $options = null, ?bool $saveDa ob_start(); eval('?>' . $view); - return ob_get_clean() ?: ''; + return (string) ob_get_clean(); })($view); $this->logPerformance($start, microtime(true), $this->excerpt($view)); diff --git a/tests/system/Commands/Generators/CellGeneratorTest.php b/tests/system/Commands/Generators/CellGeneratorTest.php index 3412246966cd..10247a34538d 100644 --- a/tests/system/Commands/Generators/CellGeneratorTest.php +++ b/tests/system/Commands/Generators/CellGeneratorTest.php @@ -45,7 +45,7 @@ protected function getFileContents(string $filepath): string return ''; } - return file_get_contents($filepath) ?: ''; + return (string) file_get_contents($filepath); } public function testGenerateCell(): void diff --git a/tests/system/Commands/Generators/CommandGeneratorTest.php b/tests/system/Commands/Generators/CommandGeneratorTest.php index ee94fa591474..83b5cf5407c1 100644 --- a/tests/system/Commands/Generators/CommandGeneratorTest.php +++ b/tests/system/Commands/Generators/CommandGeneratorTest.php @@ -51,7 +51,7 @@ protected function getFileContents(string $filepath): string return ''; } - return file_get_contents($filepath) ?: ''; + return (string) file_get_contents($filepath); } public function testGenerateCommand(): void diff --git a/tests/system/Commands/Generators/ControllerGeneratorTest.php b/tests/system/Commands/Generators/ControllerGeneratorTest.php index e1c77bcb6d5d..5382c05e70f4 100644 --- a/tests/system/Commands/Generators/ControllerGeneratorTest.php +++ b/tests/system/Commands/Generators/ControllerGeneratorTest.php @@ -40,7 +40,7 @@ protected function getFileContents(string $filepath): string return ''; } - return file_get_contents($filepath) ?: ''; + return (string) file_get_contents($filepath); } public function testGenerateController(): void diff --git a/tests/system/Commands/Generators/ModelGeneratorTest.php b/tests/system/Commands/Generators/ModelGeneratorTest.php index 8dab8b007f19..bdcfdeae7553 100644 --- a/tests/system/Commands/Generators/ModelGeneratorTest.php +++ b/tests/system/Commands/Generators/ModelGeneratorTest.php @@ -43,7 +43,7 @@ private function getFileContent(string $filepath): string return ''; } - return file_get_contents($filepath) ?: ''; + return (string) file_get_contents($filepath); } public function testGenerateModel(): void diff --git a/tests/system/Commands/Generators/ScaffoldGeneratorTest.php b/tests/system/Commands/Generators/ScaffoldGeneratorTest.php index 7f12e6b93450..6ea74bb3c18f 100644 --- a/tests/system/Commands/Generators/ScaffoldGeneratorTest.php +++ b/tests/system/Commands/Generators/ScaffoldGeneratorTest.php @@ -73,7 +73,7 @@ protected function getFileContents(string $filepath): string return ''; } - return file_get_contents($filepath) ?: ''; + return (string) file_get_contents($filepath); } public function testCreateComponentProducesManyFiles(): void diff --git a/tests/system/Publisher/PublisherSupportTest.php b/tests/system/Publisher/PublisherSupportTest.php index e432f9d21d6c..c63973e63d36 100644 --- a/tests/system/Publisher/PublisherSupportTest.php +++ b/tests/system/Publisher/PublisherSupportTest.php @@ -158,7 +158,8 @@ public function testWipe(): void { $directory = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . bin2hex(random_bytes(6)); mkdir($directory, 0700); - $directory = realpath($directory) ?: $directory; + $realPath = realpath($directory); + $directory = $realPath === false ? $directory : $realPath; $this->assertDirectoryExists($directory); config('Publisher')->restrictions[$directory] = ''; // Allow the directory diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index d69dc372a109..1c720f3cb0d4 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 1560 errors +# total 1527 errors includes: - argument.type.neon @@ -20,4 +20,3 @@ includes: - property.phpDocType.neon - return.type.neon - staticMethod.notFound.neon - - ternary.shortNotAllowed.neon diff --git a/utils/phpstan-baseline/ternary.shortNotAllowed.neon b/utils/phpstan-baseline/ternary.shortNotAllowed.neon deleted file mode 100644 index 25ab1dce60e0..000000000000 --- a/utils/phpstan-baseline/ternary.shortNotAllowed.neon +++ /dev/null @@ -1,113 +0,0 @@ -# total 33 errors - -parameters: - ignoreErrors: - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 2 - path: ../../system/CLI/CLI.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Commands/Utilities/Namespaces.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 2 - path: ../../system/Common.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 4 - path: ../../system/Cookie/Cookie.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Debug/Toolbar/Collectors/Database.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 3 - path: ../../system/Files/File.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/HTTP/CURLRequest.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 2 - path: ../../system/HTTP/Response.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 2 - path: ../../system/Helpers/filesystem_helper.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/I18n/Time.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/I18n/TimeLegacy.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Pager/Pager.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Router/AutoRouter.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 2 - path: ../../system/Test/Mock/MockCache.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Validation/Validation.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 2 - path: ../../system/View/View.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../tests/system/Commands/Generators/CellGeneratorTest.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../tests/system/Commands/Generators/CommandGeneratorTest.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../tests/system/Commands/Generators/ControllerGeneratorTest.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../tests/system/Commands/Generators/ModelGeneratorTest.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../tests/system/Commands/Generators/ScaffoldGeneratorTest.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../tests/system/Publisher/PublisherSupportTest.php