Skip to content

Commit c4487b6

Browse files
committed
refactor: upgrade to PHP 8.0 with rector
Applied rules: * TokenGetAllToObjectRector (https://wiki.php.net/rfc/token_as_object) * StrEndsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562 php/php-src#5179)
1 parent c482999 commit c4487b6

File tree

115 files changed

+384
-549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+384
-549
lines changed

system/Autoloader/Autoloader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,15 @@ public function loadClass(string $class)
275275
*/
276276
protected function loadInNamespace(string $class)
277277
{
278-
if (strpos($class, '\\') === false) {
278+
if (! str_contains($class, '\\')) {
279279
return false;
280280
}
281281

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

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

@@ -400,7 +400,7 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
400400

401401
foreach ($srcPaths as $path) {
402402
foreach ($installPaths as $installPath) {
403-
if ($installPath === substr($path, 0, strlen($installPath))) {
403+
if (str_starts_with($path, $installPath)) {
404404
$add = true;
405405
break 2;
406406
}

system/Autoloader/FileLocator.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace CodeIgniter\Autoloader;
1313

14+
use PhpToken;
15+
1416
/**
1517
* Allows loading non-class files in a namespaced manner.
1618
* Works with Helpers, Views, etc.
@@ -44,12 +46,12 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
4446
$file = $this->ensureExt($file, $ext);
4547

4648
// Clears the folder name if it is at the beginning of the filename
47-
if (! empty($folder) && strpos($file, $folder) === 0) {
49+
if (! empty($folder) && str_starts_with($file, $folder)) {
4850
$file = substr($file, strlen($folder . '/'));
4951
}
5052

5153
// Is not namespaced? Try the application folder.
52-
if (strpos($file, '\\') === false) {
54+
if (! str_contains($file, '\\')) {
5355
return $this->legacyLocate($file, $folder);
5456
}
5557

@@ -71,7 +73,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
7173
$namespaces = $this->autoloader->getNamespace();
7274

7375
foreach (array_keys($namespaces) as $namespace) {
74-
if (substr($file, 0, strlen($namespace)) === $namespace) {
76+
if (str_starts_with($file, $namespace)) {
7577
// There may be sub-namespaces of the same vendor,
7678
// so overwrite them with namespaces found later.
7779
$paths = $namespaces[$namespace];
@@ -94,7 +96,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
9496
// If we have a folder name, then the calling function
9597
// expects this file to be within that folder, like 'Views',
9698
// or 'libraries'.
97-
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false) {
99+
if (! empty($folder) && ! str_contains($path . $filename, '/' . $folder . '/')) {
98100
$path .= trim($folder, '/') . '/';
99101
}
100102

@@ -113,7 +115,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
113115
public function getClassname(string $file): string
114116
{
115117
$php = file_get_contents($file);
116-
$tokens = token_get_all($php);
118+
$tokens = PhpToken::tokenize($php);
117119
$dlm = false;
118120
$namespace = '';
119121
$className = '';
@@ -123,7 +125,7 @@ public function getClassname(string $file): string
123125
continue;
124126
}
125127

126-
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)) {
128+
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))) {
127129
if (! $dlm) {
128130
$namespace = 0;
129131
}
@@ -137,8 +139,8 @@ public function getClassname(string $file): string
137139

138140
if (($tokens[$i - 2][0] === T_CLASS || (isset($tokens[$i - 2][1]) && $tokens[$i - 2][1] === 'phpclass'))
139141
&& $tokens[$i - 1][0] === T_WHITESPACE
140-
&& $token[0] === T_STRING) {
141-
$className = $token[1];
142+
&& $token->is(T_STRING)) {
143+
$className = $token->text;
142144
break;
143145
}
144146
}
@@ -177,7 +179,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
177179

178180
if ($prioritizeApp) {
179181
$foundPaths[] = $fullPath;
180-
} elseif (strpos($fullPath, APPPATH) === 0) {
182+
} elseif (str_starts_with($fullPath, APPPATH)) {
181183
$appPaths[] = $fullPath;
182184
} else {
183185
$foundPaths[] = $fullPath;
@@ -201,7 +203,7 @@ protected function ensureExt(string $path, string $ext): string
201203
if ($ext) {
202204
$ext = '.' . $ext;
203205

204-
if (substr($path, -strlen($ext)) !== $ext) {
206+
if (! str_ends_with($path, $ext)) {
205207
$path .= $ext;
206208
}
207209
}

system/BaseModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public function find($id = null)
581581
*/
582582
public function findColumn(string $columnName)
583583
{
584-
if (strpos($columnName, ',') !== false) {
584+
if (str_contains($columnName, ',')) {
585585
throw DataException::forFindColumnHaveMultipleColumns();
586586
}
587587

system/CLI/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public static function color(string $text, string $foreground, ?string $backgrou
579579
$newText = '';
580580

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

585585
preg_match_all($pattern, $text, $matches);

system/CLI/Commands.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected function getCommandAlternatives(string $name, array $collection): arra
171171
foreach (array_keys($collection) as $commandName) {
172172
$lev = levenshtein($name, $commandName);
173173

174-
if ($lev <= strlen($commandName) / 3 || strpos($commandName, $name) !== false) {
174+
if ($lev <= strlen($commandName) / 3 || str_contains($commandName, $name)) {
175175
$alternatives[$commandName] = $lev;
176176
}
177177
}

system/CLI/GeneratorTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ protected function qualifyClassName(): string
253253
// Gets the namespace from input. Don't forget the ending backslash!
254254
$namespace = trim(str_replace('/', '\\', $this->getOption('namespace') ?? APP_NAMESPACE), '\\') . '\\';
255255

256-
if (strncmp($class, $namespace, strlen($namespace)) === 0) {
256+
if (str_starts_with($class, $namespace)) {
257257
return $class; // @codeCoverageIgnore
258258
}
259259

system/Cache/Handlers/PredisHandler.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,11 @@ public function get(string $key)
8181
return null;
8282
}
8383

84-
switch ($data['__ci_type']) {
85-
case 'array':
86-
case 'object':
87-
return unserialize($data['__ci_value']);
88-
89-
case 'boolean':
90-
case 'integer':
91-
case 'double': // Yes, 'double' is returned and NOT 'float'
92-
case 'string':
93-
case 'NULL':
94-
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;
95-
96-
case 'resource':
97-
default:
98-
return null;
99-
}
84+
return match ($data['__ci_type']) {
85+
'array', 'object' => unserialize($data['__ci_value']),
86+
'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null,
87+
default => null,
88+
};
10089
}
10190

10291
/**

system/Cache/Handlers/RedisHandler.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,11 @@ public function get(string $key)
107107
return null;
108108
}
109109

110-
switch ($data['__ci_type']) {
111-
case 'array':
112-
case 'object':
113-
return unserialize($data['__ci_value']);
114-
115-
case 'boolean':
116-
case 'integer':
117-
case 'double': // Yes, 'double' is returned and NOT 'float'
118-
case 'string':
119-
case 'NULL':
120-
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;
121-
122-
case 'resource':
123-
default:
124-
return null;
125-
}
110+
return match ($data['__ci_type']) {
111+
'array', 'object' => unserialize($data['__ci_value']),
112+
'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null,
113+
default => null,
114+
};
126115
}
127116

128117
/**

system/CodeIgniter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ protected function startController()
877877
$this->benchmark->start('controller_constructor');
878878

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

883883
return $controller(...$this->router->params());
@@ -1069,7 +1069,7 @@ public function storePreviousURL($uri)
10691069
}
10701070

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

system/Commands/Database/CreateDatabase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function run(array $params)
110110
$config->{$group}['database'] = $name;
111111

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

115115
if (is_file($dbName)) {
116116
CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red');

system/Commands/Encryption/GenerateKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey):
166166
$oldFileContents = (string) file_get_contents($envFile);
167167
$replacementKey = "\nencryption.key = {$newKey}";
168168

169-
if (strpos($oldFileContents, 'encryption.key') === false) {
169+
if (! str_contains($oldFileContents, 'encryption.key')) {
170170
return file_put_contents($envFile, $replacementKey, FILE_APPEND) !== false;
171171
}
172172

system/Commands/Generators/CellGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function run(array $params)
9090

9191
$view = array_pop($segments);
9292
$view = str_replace('Cell', '', decamelize($view));
93-
if (strpos($view, '_cell') === false) {
93+
if (! str_contains($view, '_cell')) {
9494
$view .= '_cell';
9595
}
9696
$segments[] = $view;

system/Commands/Utilities/Publish.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ public function run(array $params)
8383
foreach ($publishers as $publisher) {
8484
if ($publisher->publish()) {
8585
CLI::write(lang('Publisher.publishSuccess', [
86-
get_class($publisher),
86+
$publisher::class,
8787
count($publisher->getPublished()),
8888
$publisher->getDestination(),
8989
]), 'green');
9090
} else {
9191
CLI::error(lang('Publisher.publishFailure', [
92-
get_class($publisher),
92+
$publisher::class,
9393
$publisher->getDestination(),
9494
]), 'light_gray', 'red');
9595

system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function read(string $class, string $defaultController = 'Home', string $
6161
$methodName = $method->getName();
6262

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

system/Commands/Utilities/Routes/FilterFinder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public function find(string $uri): array
6363
$this->filters->initialize($uri);
6464

6565
return $this->filters->getFilters();
66-
} catch (RedirectException $e) {
66+
} catch (RedirectException) {
6767
return [
6868
'before' => [],
6969
'after' => [],
7070
];
71-
} catch (PageNotFoundException $e) {
71+
} catch (PageNotFoundException) {
7272
return [
7373
'before' => ['<unknown>'],
7474
'after' => ['<unknown>'],

0 commit comments

Comments
 (0)