Skip to content

Commit 65e6f8b

Browse files
committed
refactor: upgrade to PHP 8.0 by rector
1 parent 44054dc commit 65e6f8b

File tree

120 files changed

+435
-656
lines changed

Some content is hidden

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

120 files changed

+435
-656
lines changed

app/Views/errors/cli/error_exception.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// The main Exception
66
CLI::newLine();
7-
CLI::write('[' . get_class($exception) . ']', 'light_gray', 'red');
7+
CLI::write('[' . $exception::class . ']', 'light_gray', 'red');
88
CLI::newLine();
99
CLI::write($message);
1010
CLI::newLine();
@@ -41,20 +41,11 @@
4141
$function .= $padClass . $error['function'];
4242
}
4343

44-
$args = implode(', ', array_map(static function ($value) {
45-
switch (true) {
46-
case is_object($value):
47-
return 'Object(' . get_class($value) . ')';
48-
49-
case is_array($value):
50-
return count($value) ? '[...]' : '[]';
51-
52-
case $value === null:
53-
return 'null'; // return the lowercased version
54-
55-
default:
56-
return var_export($value, true);
57-
}
44+
$args = implode(', ', array_map(static fn ($value) => match (true) {
45+
is_object($value) => 'Object(' . $value::class . ')',
46+
is_array($value) => count($value) ? '[...]' : '[]',
47+
$value === null => 'null',
48+
default => var_export($value, true),
5849
}, array_values($error['args'] ?? [])));
5950

6051
$function .= '(' . $args . ')';

app/Views/errors/html/error_exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<?php
9191
$params = null;
9292
// Reflection by name is not available for closure function
93-
if (substr($row['function'], -1) !== '}') {
93+
if (!str_ends_with($row['function'], '}')) {
9494
$mirror = isset($row['class']) ? new ReflectionMethod($row['class'], $row['function']) : new ReflectionFunction($row['function']);
9595
$params = $mirror->getParameters();
9696
}

system/Autoloader/Autoloader.php

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

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

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

@@ -412,7 +412,7 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
412412

413413
foreach ($srcPaths as $path) {
414414
foreach ($installPaths as $installPath) {
415-
if ($installPath === substr($path, 0, strlen($installPath))) {
415+
if (str_starts_with($path, $installPath)) {
416416
$add = true;
417417
break 2;
418418
}

system/Autoloader/FileLocator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
5151
$file = $this->ensureExt($file, $ext);
5252

5353
// Clears the folder name if it is at the beginning of the filename
54-
if (! empty($folder) && strpos($file, $folder) === 0) {
54+
if (! empty($folder) && str_starts_with($file, $folder)) {
5555
$file = substr($file, strlen($folder . '/'));
5656
}
5757

5858
// Is not namespaced? Try the application folder.
59-
if (strpos($file, '\\') === false) {
59+
if (! str_contains($file, '\\')) {
6060
return $this->legacyLocate($file, $folder);
6161
}
6262

@@ -101,7 +101,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
101101
// If we have a folder name, then the calling function
102102
// expects this file to be within that folder, like 'Views',
103103
// or 'libraries'.
104-
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false) {
104+
if (! empty($folder) && ! str_contains($path . $filename, '/' . $folder . '/')) {
105105
$path .= trim($folder, '/') . '/';
106106
}
107107

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

185185
if ($prioritizeApp) {
186186
$foundPaths[] = $fullPath;
187-
} elseif (strpos($fullPath, APPPATH) === 0) {
187+
} elseif (str_starts_with($fullPath, APPPATH)) {
188188
$appPaths[] = $fullPath;
189189
} else {
190190
$foundPaths[] = $fullPath;
@@ -208,7 +208,7 @@ protected function ensureExt(string $path, string $ext): string
208208
if ($ext) {
209209
$ext = '.' . $ext;
210210

211-
if (substr($path, -strlen($ext)) !== $ext) {
211+
if (! str_ends_with($path, $ext)) {
212212
$path .= $ext;
213213
}
214214
}

system/BaseModel.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ public function find($id = null)
587587
*/
588588
public function findColumn(string $columnName)
589589
{
590-
if (strpos($columnName, ',') !== false) {
590+
if (str_contains($columnName, ',')) {
591591
throw DataException::forFindColumnHaveMultipleColumns();
592592
}
593593

@@ -1317,19 +1317,12 @@ protected function setDate(?int $userData = null)
13171317
*/
13181318
protected function intToDate(int $value)
13191319
{
1320-
switch ($this->dateFormat) {
1321-
case 'int':
1322-
return $value;
1323-
1324-
case 'datetime':
1325-
return date('Y-m-d H:i:s', $value);
1326-
1327-
case 'date':
1328-
return date('Y-m-d', $value);
1329-
1330-
default:
1331-
throw ModelException::forNoDateFormat(static::class);
1332-
}
1320+
return match ($this->dateFormat) {
1321+
'int' => $value,
1322+
'datetime' => date('Y-m-d H:i:s', $value),
1323+
'date' => date('Y-m-d', $value),
1324+
default => throw ModelException::forNoDateFormat(static::class),
1325+
};
13331326
}
13341327

13351328
/**
@@ -1346,19 +1339,12 @@ protected function intToDate(int $value)
13461339
*/
13471340
protected function timeToDate(Time $value)
13481341
{
1349-
switch ($this->dateFormat) {
1350-
case 'datetime':
1351-
return $value->format('Y-m-d H:i:s');
1352-
1353-
case 'date':
1354-
return $value->format('Y-m-d');
1355-
1356-
case 'int':
1357-
return $value->getTimestamp();
1358-
1359-
default:
1360-
return (string) $value;
1361-
}
1342+
return match ($this->dateFormat) {
1343+
'datetime' => $value->format('Y-m-d H:i:s'),
1344+
'date' => $value->format('Y-m-d'),
1345+
'int' => $value->getTimestamp(),
1346+
default => (string) $value,
1347+
};
13621348
}
13631349

13641350
/**

system/CLI/CLI.php

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

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

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

system/CLI/Commands.php

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

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

system/CLI/GeneratorTrait.php

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

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

system/Cache/Handlers/PredisHandler.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,11 @@ public function get(string $key)
8686
return null;
8787
}
8888

89-
switch ($data['__ci_type']) {
90-
case 'array':
91-
case 'object':
92-
return unserialize($data['__ci_value']);
93-
94-
case 'boolean':
95-
case 'integer':
96-
case 'double': // Yes, 'double' is returned and NOT 'float'
97-
case 'string':
98-
case 'NULL':
99-
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;
100-
101-
case 'resource':
102-
default:
103-
return null;
104-
}
89+
return match ($data['__ci_type']) {
90+
'array', 'object' => unserialize($data['__ci_value']),
91+
'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null,
92+
default => null,
93+
};
10594
}
10695

10796
/**

system/Cache/Handlers/RedisHandler.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,11 @@ public function get(string $key)
112112
return null;
113113
}
114114

115-
switch ($data['__ci_type']) {
116-
case 'array':
117-
case 'object':
118-
return unserialize($data['__ci_value']);
119-
120-
case 'boolean':
121-
case 'integer':
122-
case 'double': // Yes, 'double' is returned and NOT 'float'
123-
case 'string':
124-
case 'NULL':
125-
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;
126-
127-
case 'resource':
128-
default:
129-
return null;
130-
}
115+
return match ($data['__ci_type']) {
116+
'array', 'object' => unserialize($data['__ci_value']),
117+
'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null,
118+
default => null,
119+
};
131120
}
132121

133122
/**

system/CodeIgniter.php

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

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

874874
return $controller(...$this->router->params());
@@ -1040,7 +1040,7 @@ public function storePreviousURL($uri)
10401040
}
10411041

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

system/Commands/Database/CreateDatabase.php

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

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

112112
if (is_file($dbName)) {
113113
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/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
@@ -67,7 +67,7 @@ public function read(string $class, string $defaultController = 'Home', string $
6767
$methodName = $method->getName();
6868

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

system/Commands/Utilities/Routes/FilterFinder.php

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

6060
return $this->filters->getFilters();
61-
} catch (RedirectException $e) {
61+
} catch (RedirectException) {
6262
return [
6363
'before' => [],
6464
'after' => [],
6565
];
66-
} catch (PageNotFoundException $e) {
66+
} catch (PageNotFoundException) {
6767
return [
6868
'before' => ['<unknown>'],
6969
'after' => ['<unknown>'],

0 commit comments

Comments
 (0)