Skip to content

Commit

Permalink
refactor: upgrade to PHP 8.0 by rector
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Sep 23, 2023
1 parent 44054dc commit 65e6f8b
Show file tree
Hide file tree
Showing 120 changed files with 435 additions and 656 deletions.
21 changes: 6 additions & 15 deletions app/Views/errors/cli/error_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// The main Exception
CLI::newLine();
CLI::write('[' . get_class($exception) . ']', 'light_gray', 'red');
CLI::write('[' . $exception::class . ']', 'light_gray', 'red');
CLI::newLine();
CLI::write($message);
CLI::newLine();
Expand Down Expand Up @@ -41,20 +41,11 @@
$function .= $padClass . $error['function'];
}

$args = implode(', ', array_map(static function ($value) {
switch (true) {
case is_object($value):
return 'Object(' . get_class($value) . ')';

case is_array($value):
return count($value) ? '[...]' : '[]';

case $value === null:
return 'null'; // return the lowercased version

default:
return var_export($value, true);
}
$args = implode(', ', array_map(static fn ($value) => match (true) {
is_object($value) => 'Object(' . $value::class . ')',
is_array($value) => count($value) ? '[...]' : '[]',
$value === null => 'null',
default => var_export($value, true),
}, array_values($error['args'] ?? [])));

$function .= '(' . $args . ')';
Expand Down
2 changes: 1 addition & 1 deletion app/Views/errors/html/error_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<?php
$params = null;
// Reflection by name is not available for closure function
if (substr($row['function'], -1) !== '}') {
if (!str_ends_with($row['function'], '}')) {
$mirror = isset($row['class']) ? new ReflectionMethod($row['class'], $row['function']) : new ReflectionFunction($row['function']);
$params = $mirror->getParameters();
}
Expand Down
6 changes: 3 additions & 3 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,15 @@ public function loadClass(string $class): void
*/
protected function loadInNamespace(string $class)
{
if (strpos($class, '\\') === false) {
if (! str_contains($class, '\\')) {
return false;
}

foreach ($this->prefixes as $namespace => $directories) {
foreach ($directories as $directory) {
$directory = rtrim($directory, '\\/');

if (strpos($class, $namespace) === 0) {
if (str_starts_with($class, $namespace)) {
$filePath = $directory . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen($namespace))) . '.php';
$filename = $this->includeFile($filePath);

Expand Down Expand Up @@ -412,7 +412,7 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa

foreach ($srcPaths as $path) {
foreach ($installPaths as $installPath) {
if ($installPath === substr($path, 0, strlen($installPath))) {
if (str_starts_with($path, $installPath)) {
$add = true;
break 2;
}
Expand Down
10 changes: 5 additions & 5 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ 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 (! empty($folder) && str_starts_with($file, $folder)) {
$file = substr($file, strlen($folder . '/'));
}

// Is not namespaced? Try the application folder.
if (strpos($file, '\\') === false) {
if (! str_contains($file, '\\')) {
return $this->legacyLocate($file, $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 (! empty($folder) && ! str_contains($path . $filename, '/' . $folder . '/')) {
$path .= trim($folder, '/') . '/';
}

Expand Down Expand Up @@ -184,7 +184,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =

if ($prioritizeApp) {
$foundPaths[] = $fullPath;
} elseif (strpos($fullPath, APPPATH) === 0) {
} elseif (str_starts_with($fullPath, APPPATH)) {
$appPaths[] = $fullPath;
} else {
$foundPaths[] = $fullPath;
Expand All @@ -208,7 +208,7 @@ protected function ensureExt(string $path, string $ext): string
if ($ext) {
$ext = '.' . $ext;

if (substr($path, -strlen($ext)) !== $ext) {
if (! str_ends_with($path, $ext)) {
$path .= $ext;
}
}
Expand Down
40 changes: 13 additions & 27 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ public function find($id = null)
*/
public function findColumn(string $columnName)
{
if (strpos($columnName, ',') !== false) {
if (str_contains($columnName, ',')) {
throw DataException::forFindColumnHaveMultipleColumns();
}

Expand Down Expand Up @@ -1317,19 +1317,12 @@ protected function setDate(?int $userData = null)
*/
protected function intToDate(int $value)
{
switch ($this->dateFormat) {
case 'int':
return $value;

case 'datetime':
return date('Y-m-d H:i:s', $value);

case 'date':
return date('Y-m-d', $value);

default:
throw ModelException::forNoDateFormat(static::class);
}
return match ($this->dateFormat) {
'int' => $value,
'datetime' => date('Y-m-d H:i:s', $value),
'date' => date('Y-m-d', $value),
default => throw ModelException::forNoDateFormat(static::class),
};
}

/**
Expand All @@ -1346,19 +1339,12 @@ protected function intToDate(int $value)
*/
protected function timeToDate(Time $value)
{
switch ($this->dateFormat) {
case 'datetime':
return $value->format('Y-m-d H:i:s');

case 'date':
return $value->format('Y-m-d');

case 'int':
return $value->getTimestamp();

default:
return (string) $value;
}
return match ($this->dateFormat) {
'datetime' => $value->format('Y-m-d H:i:s'),
'date' => $value->format('Y-m-d'),
'int' => $value->getTimestamp(),
default => (string) $value,
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public static function color(string $text, string $foreground, ?string $backgrou
$newText = '';

// Detect if color method was already in use with this text
if (strpos($text, "\033[0m") !== false) {
if (str_contains($text, "\033[0m")) {
$pattern = '/\\033\\[0;.+?\\033\\[0m/u';

preg_match_all($pattern, $text, $matches);
Expand Down
2 changes: 1 addition & 1 deletion system/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function getCommandAlternatives(string $name, array $collection): arra
foreach (array_keys($collection) as $commandName) {
$lev = levenshtein($name, $commandName);

if ($lev <= strlen($commandName) / 3 || strpos($commandName, $name) !== false) {
if ($lev <= strlen($commandName) / 3 || str_contains($commandName, $name)) {
$alternatives[$commandName] = $lev;
}
}
Expand Down
2 changes: 1 addition & 1 deletion system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ protected function qualifyClassName(): string
// Gets the namespace from input. Don't forget the ending backslash!
$namespace = trim(str_replace('/', '\\', $this->getOption('namespace') ?? APP_NAMESPACE), '\\') . '\\';

if (strncmp($class, $namespace, strlen($namespace)) === 0) {
if (str_starts_with($class, $namespace)) {
return $class; // @codeCoverageIgnore
}

Expand Down
21 changes: 5 additions & 16 deletions system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,11 @@ public function get(string $key)
return null;
}

switch ($data['__ci_type']) {
case 'array':
case 'object':
return unserialize($data['__ci_value']);

case 'boolean':
case 'integer':
case 'double': // Yes, 'double' is returned and NOT 'float'
case 'string':
case 'NULL':
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;

case 'resource':
default:
return null;
}
return match ($data['__ci_type']) {
'array', 'object' => unserialize($data['__ci_value']),
'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null,
default => null,
};
}

/**
Expand Down
21 changes: 5 additions & 16 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,11 @@ public function get(string $key)
return null;
}

switch ($data['__ci_type']) {
case 'array':
case 'object':
return unserialize($data['__ci_value']);

case 'boolean':
case 'integer':
case 'double': // Yes, 'double' is returned and NOT 'float'
case 'string':
case 'NULL':
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;

case 'resource':
default:
return null;
}
return match ($data['__ci_type']) {
'array', 'object' => unserialize($data['__ci_value']),
'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null,
default => null,
};
}

/**
Expand Down
4 changes: 2 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ protected function startController()
$this->benchmark->start('controller_constructor');

// Is it routed to a Closure?
if (is_object($this->controller) && (get_class($this->controller) === 'Closure')) {
if (is_object($this->controller) && ($this->controller::class === 'Closure')) {
$controller = $this->controller;

return $controller(...$this->router->params());
Expand Down Expand Up @@ -1040,7 +1040,7 @@ public function storePreviousURL($uri)
}

// Ignore non-HTML responses
if (strpos($this->response->getHeaderLine('Content-Type'), 'text/html') === false) {
if (! str_contains($this->response->getHeaderLine('Content-Type'), 'text/html')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Database/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function run(array $params)
$config->{$group}['database'] = $name;

if ($name !== ':memory:') {
$dbName = strpos($name, DIRECTORY_SEPARATOR) === false ? WRITEPATH . $name : $name;
$dbName = ! str_contains($name, DIRECTORY_SEPARATOR) ? WRITEPATH . $name : $name;

if (is_file($dbName)) {
CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red');
Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Encryption/GenerateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey):
$oldFileContents = (string) file_get_contents($envFile);
$replacementKey = "\nencryption.key = {$newKey}";

if (strpos($oldFileContents, 'encryption.key') === false) {
if (! str_contains($oldFileContents, 'encryption.key')) {
return file_put_contents($envFile, $replacementKey, FILE_APPEND) !== false;
}

Expand Down
4 changes: 2 additions & 2 deletions system/Commands/Utilities/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public function run(array $params)
foreach ($publishers as $publisher) {
if ($publisher->publish()) {
CLI::write(lang('Publisher.publishSuccess', [
get_class($publisher),
$publisher::class,
count($publisher->getPublished()),
$publisher->getDestination(),
]), 'green');
} else {
CLI::error(lang('Publisher.publishFailure', [
get_class($publisher),
$publisher::class,
$publisher->getDestination(),
]), 'light_gray', 'red');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function read(string $class, string $defaultController = 'Home', string $
$methodName = $method->getName();

foreach ($this->httpMethods as $httpVerb) {
if (strpos($methodName, $httpVerb) === 0) {
if (str_starts_with($methodName, $httpVerb)) {
// Remove HTTP verb prefix.
$methodInUri = lcfirst(substr($methodName, strlen($httpVerb)));

Expand Down
4 changes: 2 additions & 2 deletions system/Commands/Utilities/Routes/FilterFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public function find(string $uri): array
$this->filters->initialize($uri);

return $this->filters->getFilters();
} catch (RedirectException $e) {
} catch (RedirectException) {
return [
'before' => [],
'after' => [],
];
} catch (PageNotFoundException $e) {
} catch (PageNotFoundException) {
return [
'before' => ['<unknown>'],
'after' => ['<unknown>'],
Expand Down
Loading

0 comments on commit 65e6f8b

Please sign in to comment.