Skip to content

Commit

Permalink
refactor: upgrade to PHP 8.0 with rector
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 29, 2022
1 parent c482999 commit 6c5297e
Show file tree
Hide file tree
Showing 115 changed files with 378 additions and 545 deletions.
6 changes: 3 additions & 3 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,15 @@ public function loadClass(string $class)
*/
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 @@ -400,7 +400,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
12 changes: 6 additions & 6 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,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 All @@ -71,7 +71,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
$namespaces = $this->autoloader->getNamespace();

foreach (array_keys($namespaces) as $namespace) {
if (substr($file, 0, strlen($namespace)) === $namespace) {
if (str_starts_with($file, $namespace)) {
// There may be sub-namespaces of the same vendor,
// so overwrite them with namespaces found later.
$paths = $namespaces[$namespace];
Expand All @@ -94,7 +94,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 @@ -177,7 +177,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 @@ -201,7 +201,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
2 changes: 1 addition & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public function find($id = null)
*/
public function findColumn(string $columnName)
{
if (strpos($columnName, ',') !== false) {
if (str_contains($columnName, ',')) {
throw DataException::forFindColumnHaveMultipleColumns();
}

Expand Down
2 changes: 1 addition & 1 deletion system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,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 @@ -171,7 +171,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 @@ -253,7 +253,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 @@ -81,22 +81,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 @@ -107,22 +107,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 @@ -877,7 +877,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 @@ -1069,7 +1069,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 @@ -110,7 +110,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
2 changes: 1 addition & 1 deletion system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function run(array $params)

$view = array_pop($segments);
$view = str_replace('Cell', '', decamelize($view));
if (strpos($view, '_cell') === false) {
if (! str_contains($view, '_cell')) {
$view .= '_cell';
}
$segments[] = $view;
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 @@ -61,7 +61,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 @@ -63,12 +63,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
66 changes: 23 additions & 43 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,14 @@ function clean_path(string $path): string
// Resolve relative paths
$path = realpath($path) ?: $path;

switch (true) {
case strpos($path, APPPATH) === 0:
return 'APPPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(APPPATH));

case strpos($path, SYSTEMPATH) === 0:
return 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(SYSTEMPATH));

case strpos($path, FCPATH) === 0:
return 'FCPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(FCPATH));

case defined('VENDORPATH') && strpos($path, VENDORPATH) === 0:
return 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(VENDORPATH));

case strpos($path, ROOTPATH) === 0:
return 'ROOTPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(ROOTPATH));

default:
return $path;
}
return match (true) {
str_starts_with($path, APPPATH) => 'APPPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(APPPATH)),
str_starts_with($path, SYSTEMPATH) => 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(SYSTEMPATH)),
str_starts_with($path, FCPATH) => 'FCPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(FCPATH)),
defined('VENDORPATH') && str_starts_with($path, VENDORPATH) => 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(VENDORPATH)),
str_starts_with($path, ROOTPATH) => 'ROOTPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(ROOTPATH)),
default => $path,
};
}
}

Expand Down Expand Up @@ -372,22 +361,13 @@ function env(string $key, $default = null)
return $default;
}

// Handle any boolean values
switch (strtolower($value)) {
case 'true':
return true;

case 'false':
return false;

case 'empty':
return '';

case 'null':
return null;
}

return $value;
return match (strtolower($value)) {
'true' => true,
'false' => false,
'empty' => '',
'null' => null,
default => $value,
};
}
}

Expand Down Expand Up @@ -492,9 +472,9 @@ function force_https(int $duration = 31_536_000, ?RequestInterface $request = nu

$baseURL = config('App')->baseURL;

if (strpos($baseURL, 'https://') === 0) {
if (str_starts_with($baseURL, 'https://')) {
$authority = substr($baseURL, strlen('https://'));
} elseif (strpos($baseURL, 'http://') === 0) {
} elseif (str_starts_with($baseURL, 'http://')) {
$authority = substr($baseURL, strlen('http://'));
} else {
$authority = $baseURL;
Expand Down Expand Up @@ -597,7 +577,7 @@ function helper($filenames)
$appHelper = null;
$localIncludes = [];

if (strpos($filename, '_helper') === false) {
if (! str_contains($filename, '_helper')) {
$filename .= '_helper';
}

Expand All @@ -608,7 +588,7 @@ function helper($filenames)

// If the file is namespaced, we'll just grab that
// file and not search for any others
if (strpos($filename, '\\') !== false) {
if (str_contains($filename, '\\')) {
$path = $loader->locateFile($filename, 'Helpers');

if (empty($path)) {
Expand All @@ -622,9 +602,9 @@ function helper($filenames)
$paths = $loader->search('Helpers/' . $filename);

foreach ($paths as $path) {
if (strpos($path, APPPATH . 'Helpers' . DIRECTORY_SEPARATOR) === 0) {
if (str_starts_with($path, APPPATH . 'Helpers' . DIRECTORY_SEPARATOR)) {
$appHelper = $path;
} elseif (strpos($path, SYSTEMPATH . 'Helpers' . DIRECTORY_SEPARATOR) === 0) {
} elseif (str_starts_with($path, SYSTEMPATH . 'Helpers' . DIRECTORY_SEPARATOR)) {
$systemHelper = $path;
} else {
$localIncludes[] = $path;
Expand Down Expand Up @@ -1211,7 +1191,7 @@ function view_cell(string $library, $params = null, int $ttl = 0, ?string $cache
*/
function class_basename($class)
{
$class = is_object($class) ? get_class($class) : $class;
$class = is_object($class) ? $class::class : $class;

return basename(str_replace('\\', '/', $class));
}
Expand All @@ -1230,7 +1210,7 @@ function class_basename($class)
function class_uses_recursive($class)
{
if (is_object($class)) {
$class = get_class($class);
$class = $class::class;
}

$results = [];
Expand Down
2 changes: 1 addition & 1 deletion system/ComposerScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function postUpdate()

foreach (self::$dependencies as $key => $dependency) {
// Kint may be removed.
if (! is_dir($dependency['from']) && strpos($key, 'kint') === 0) {
if (! is_dir($dependency['from']) && str_starts_with($key, 'kint')) {
continue;
}

Expand Down
Loading

0 comments on commit 6c5297e

Please sign in to comment.