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 c4487b6
Show file tree
Hide file tree
Showing 115 changed files with 384 additions and 549 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
22 changes: 12 additions & 10 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace CodeIgniter\Autoloader;

use PhpToken;

/**
* Allows loading non-class files in a namespaced manner.
* Works with Helpers, Views, etc.
Expand Down Expand Up @@ -44,12 +46,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 +73,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 +96,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 All @@ -113,7 +115,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
public function getClassname(string $file): string
{
$php = file_get_contents($file);
$tokens = token_get_all($php);
$tokens = PhpToken::tokenize($php);
$dlm = false;
$namespace = '';
$className = '';
Expand All @@ -123,7 +125,7 @@ public function getClassname(string $file): string
continue;
}

if ((isset($tokens[$i - 2][1]) && ($tokens[$i - 2][1] === 'phpnamespace' || $tokens[$i - 2][1] === 'namespace')) || ($dlm && $tokens[$i - 1][0] === T_NS_SEPARATOR && $token[0] === T_STRING)) {
if ((isset($tokens[$i - 2][1]) && ($tokens[$i - 2][1] === 'phpnamespace' || $tokens[$i - 2][1] === 'namespace')) || ($dlm && $tokens[$i - 1][0] === T_NS_SEPARATOR && $token->is(T_STRING))) {
if (! $dlm) {
$namespace = 0;
}
Expand All @@ -137,8 +139,8 @@ public function getClassname(string $file): string

if (($tokens[$i - 2][0] === T_CLASS || (isset($tokens[$i - 2][1]) && $tokens[$i - 2][1] === 'phpclass'))
&& $tokens[$i - 1][0] === T_WHITESPACE
&& $token[0] === T_STRING) {
$className = $token[1];
&& $token->is(T_STRING)) {
$className = $token->text;
break;
}
}
Expand Down Expand Up @@ -177,7 +179,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 +203,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
Loading

0 comments on commit c4487b6

Please sign in to comment.