Skip to content

Commit 6c5297e

Browse files
committed
refactor: upgrade to PHP 8.0 with rector
Applied rules: * 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 6c5297e

File tree

115 files changed

+378
-545
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

+378
-545
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
4444
$file = $this->ensureExt($file, $ext);
4545

4646
// Clears the folder name if it is at the beginning of the filename
47-
if (! empty($folder) && strpos($file, $folder) === 0) {
47+
if (! empty($folder) && str_starts_with($file, $folder)) {
4848
$file = substr($file, strlen($folder . '/'));
4949
}
5050

5151
// Is not namespaced? Try the application folder.
52-
if (strpos($file, '\\') === false) {
52+
if (! str_contains($file, '\\')) {
5353
return $this->legacyLocate($file, $folder);
5454
}
5555

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

7373
foreach (array_keys($namespaces) as $namespace) {
74-
if (substr($file, 0, strlen($namespace)) === $namespace) {
74+
if (str_starts_with($file, $namespace)) {
7575
// There may be sub-namespaces of the same vendor,
7676
// so overwrite them with namespaces found later.
7777
$paths = $namespaces[$namespace];
@@ -94,7 +94,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
9494
// If we have a folder name, then the calling function
9595
// expects this file to be within that folder, like 'Views',
9696
// or 'libraries'.
97-
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false) {
97+
if (! empty($folder) && ! str_contains($path . $filename, '/' . $folder . '/')) {
9898
$path .= trim($folder, '/') . '/';
9999
}
100100

@@ -177,7 +177,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
177177

178178
if ($prioritizeApp) {
179179
$foundPaths[] = $fullPath;
180-
} elseif (strpos($fullPath, APPPATH) === 0) {
180+
} elseif (str_starts_with($fullPath, APPPATH)) {
181181
$appPaths[] = $fullPath;
182182
} else {
183183
$foundPaths[] = $fullPath;
@@ -201,7 +201,7 @@ protected function ensureExt(string $path, string $ext): string
201201
if ($ext) {
202202
$ext = '.' . $ext;
203203

204-
if (substr($path, -strlen($ext)) !== $ext) {
204+
if (! str_ends_with($path, $ext)) {
205205
$path .= $ext;
206206
}
207207
}

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');

0 commit comments

Comments
 (0)