Skip to content

Commit 435782d

Browse files
committed
refactor: run rector
1 parent aafd3fc commit 435782d

File tree

145 files changed

+542
-860
lines changed

Some content is hidden

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

145 files changed

+542
-860
lines changed

app/Views/errors/cli/error_exception.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use CodeIgniter\CLI\CLI;
44

55
// The main Exception
6-
CLI::write('[' . get_class($exception) . ']', 'light_gray', 'red');
6+
CLI::write('[' . $exception::class . ']', 'light_gray', 'red');
77
CLI::write($message);
88
CLI::write('at ' . CLI::color(clean_path($exception->getFile()) . ':' . $exception->getLine(), 'green'));
99
CLI::newLine();
@@ -14,7 +14,7 @@
1414
$last = $prevException;
1515

1616
CLI::write(' Caused by:');
17-
CLI::write(' [' . get_class($prevException) . ']', 'red');
17+
CLI::write(' [' . $prevException::class . ']', 'red');
1818
CLI::write(' ' . $prevException->getMessage());
1919
CLI::write(' at ' . CLI::color(clean_path($prevException->getFile()) . ':' . $prevException->getLine(), 'green'));
2020
CLI::newLine();
@@ -50,20 +50,11 @@
5050
$function .= $padClass . $error['function'];
5151
}
5252

53-
$args = implode(', ', array_map(static function ($value) {
54-
switch (true) {
55-
case is_object($value):
56-
return 'Object(' . get_class($value) . ')';
57-
58-
case is_array($value):
59-
return count($value) ? '[...]' : '[]';
60-
61-
case $value === null:
62-
return 'null'; // return the lowercased version
63-
64-
default:
65-
return var_export($value, true);
66-
}
53+
$args = implode(', ', array_map(static fn ($value) => match (true) {
54+
is_object($value) => 'Object(' . $value::class . ')',
55+
is_array($value) => count($value) ? '[...]' : '[]',
56+
$value === null => 'null',
57+
default => var_export($value, true),
6758
}, array_values($error['args'] ?? [])));
6859

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

app/Views/errors/html/error_exception.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555

5656
<pre>
5757
Caused by:
58-
<?= esc(get_class($prevException)), esc($prevException->getCode() ? ' #' . $prevException->getCode() : '') ?>
58+
<?= esc($prevException::class), esc($prevException->getCode() ? ' #' . $prevException->getCode() : '') ?>
5959

6060
<?= nl2br(esc($prevException->getMessage())) ?>
61-
<a href="https://www.duckduckgo.com/?q=<?= urlencode(get_class($prevException) . ' ' . preg_replace('#\'.*\'|".*"#Us', '', $prevException->getMessage())) ?>"
61+
<a href="https://www.duckduckgo.com/?q=<?= urlencode($prevException::class . ' ' . preg_replace('#\'.*\'|".*"#Us', '', $prevException->getMessage())) ?>"
6262
rel="noreferrer" target="_blank">search &rarr;</a>
6363
<?= esc(clean_path($prevException->getFile()) . ':' . $prevException->getLine()) ?>
6464
</pre>
@@ -115,7 +115,7 @@
115115
<?php
116116
$params = null;
117117
// Reflection by name is not available for closure function
118-
if (substr($row['function'], -1) !== '}') {
118+
if (!str_ends_with($row['function'], '}')) {
119119
$mirror = isset($row['class']) ? new ReflectionMethod($row['class'], $row['function']) : new ReflectionFunction($row['function']);
120120
$params = $mirror->getParameters();
121121
}

system/Autoloader/Autoloader.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ private function loadComposerAutoloader(Modules $modules): void
158158
public function register()
159159
{
160160
// Register classmap loader for the files in our class map.
161-
spl_autoload_register([$this, 'loadClassmap'], true);
161+
spl_autoload_register($this->loadClassmap(...), true);
162162

163163
// Register the PSR-4 autoloader.
164-
spl_autoload_register([$this, 'loadClass'], true);
164+
spl_autoload_register($this->loadClass(...), true);
165165

166166
// Load our non-class files
167167
foreach ($this->files as $file) {
@@ -176,8 +176,8 @@ public function register()
176176
*/
177177
public function unregister(): void
178178
{
179-
spl_autoload_unregister([$this, 'loadClass']);
180-
spl_autoload_unregister([$this, 'loadClassmap']);
179+
spl_autoload_unregister($this->loadClass(...));
180+
spl_autoload_unregister($this->loadClassmap(...));
181181
}
182182

183183
/**
@@ -276,12 +276,12 @@ public function loadClass(string $class): void
276276
*/
277277
protected function loadInNamespace(string $class)
278278
{
279-
if (strpos($class, '\\') === false) {
279+
if (! str_contains($class, '\\')) {
280280
return false;
281281
}
282282

283283
foreach ($this->prefixes as $namespace => $directories) {
284-
if (strpos($class, $namespace) === 0) {
284+
if (str_starts_with($class, $namespace)) {
285285
$relativeClassPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen($namespace)));
286286

287287
foreach ($directories as $directory) {
@@ -420,7 +420,7 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
420420

421421
foreach ($srcPaths as $path) {
422422
foreach ($installPaths as $installPath) {
423-
if ($installPath === substr($path, 0, strlen($installPath))) {
423+
if (str_starts_with($path, $installPath)) {
424424
$add = true;
425425
break 2;
426426
}

system/Autoloader/FileLocator.php

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

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

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

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

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

193193
if ($prioritizeApp) {
194194
$foundPaths[] = $fullPath;
195-
} elseif (strpos($fullPath, APPPATH) === 0) {
195+
} elseif (str_starts_with($fullPath, APPPATH)) {
196196
$appPaths[] = $fullPath;
197197
} else {
198198
$foundPaths[] = $fullPath;
@@ -216,7 +216,7 @@ protected function ensureExt(string $path, string $ext): string
216216
if ($ext !== '') {
217217
$ext = '.' . $ext;
218218

219-
if (substr($path, -strlen($ext)) !== $ext) {
219+
if (! str_ends_with($path, $ext)) {
220220
$path .= $ext;
221221
}
222222
}

system/Autoloader/FileLocatorCached.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
final class FileLocatorCached implements FileLocatorInterface
2525
{
26-
private FileLocator $locator;
27-
2826
/**
2927
* @var CacheInterface|FileVarExportHandler
3028
*/
@@ -51,10 +49,9 @@ final class FileLocatorCached implements FileLocatorInterface
5149
/**
5250
* @param CacheInterface|FileVarExportHandler|null $cache
5351
*/
54-
public function __construct(FileLocator $locator, $cache = null)
52+
public function __construct(private readonly FileLocator $locator, $cache = null)
5553
{
5654
$this->cacheHandler = $cache ?? new FileVarExportHandler();
57-
$this->locator = $locator;
5855

5956
$this->loadCache();
6057
}

system/BaseModel.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public function find($id = null)
631631
*/
632632
public function findColumn(string $columnName)
633633
{
634-
if (strpos($columnName, ',') !== false) {
634+
if (str_contains($columnName, ',')) {
635635
throw DataException::forFindColumnHaveMultipleColumns();
636636
}
637637

@@ -1393,19 +1393,12 @@ protected function setDate(?int $userData = null)
13931393
*/
13941394
protected function intToDate(int $value)
13951395
{
1396-
switch ($this->dateFormat) {
1397-
case 'int':
1398-
return $value;
1399-
1400-
case 'datetime':
1401-
return date($this->db->dateFormat['datetime'], $value);
1402-
1403-
case 'date':
1404-
return date($this->db->dateFormat['date'], $value);
1405-
1406-
default:
1407-
throw ModelException::forNoDateFormat(static::class);
1408-
}
1396+
return match ($this->dateFormat) {
1397+
'int' => $value,
1398+
'datetime' => date($this->db->dateFormat['datetime'], $value),
1399+
'date' => date($this->db->dateFormat['date'], $value),
1400+
default => throw ModelException::forNoDateFormat(static::class),
1401+
};
14091402
}
14101403

14111404
/**
@@ -1422,19 +1415,12 @@ protected function intToDate(int $value)
14221415
*/
14231416
protected function timeToDate(Time $value)
14241417
{
1425-
switch ($this->dateFormat) {
1426-
case 'datetime':
1427-
return $value->format($this->db->dateFormat['datetime']);
1428-
1429-
case 'date':
1430-
return $value->format($this->db->dateFormat['date']);
1431-
1432-
case 'int':
1433-
return $value->getTimestamp();
1434-
1435-
default:
1436-
return (string) $value;
1437-
}
1418+
return match ($this->dateFormat) {
1419+
'datetime' => $value->format($this->db->dateFormat['datetime']),
1420+
'date' => $value->format($this->db->dateFormat['date']),
1421+
'int' => $value->getTimestamp(),
1422+
default => (string) $value,
1423+
};
14381424
}
14391425

14401426
/**

system/CLI/CLI.php

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

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

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

system/CLI/Commands.php

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

186-
if ($lev <= strlen($commandName) / 3 || strpos($commandName, $name) !== false) {
186+
if ($lev <= strlen($commandName) / 3 || str_contains($commandName, $name)) {
187187
$alternatives[$commandName] = $lev;
188188
}
189189
}

system/CLI/GeneratorTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected function qualifyClassName(): string
269269
// Gets the namespace from input. Don't forget the ending backslash!
270270
$namespace = $this->getNamespace() . '\\';
271271

272-
if (strncmp($class, $namespace, strlen($namespace)) === 0) {
272+
if (str_starts_with($class, $namespace)) {
273273
return $class; // @codeCoverageIgnore
274274
}
275275

system/CLI/InputOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class InputOutput
2121
/**
2222
* Is the readline library on the system?
2323
*/
24-
private bool $readlineSupport;
24+
private readonly bool $readlineSupport;
2525

2626
public function __construct()
2727
{

system/Cache/Handlers/BaseHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ public function remember(string $key, int $ttl, Closure $callback)
102102
*
103103
* @param string $pattern Cache items glob-style pattern
104104
*
105-
* @return int|never
106-
*
107105
* @throws Exception
108106
*/
109-
public function deleteMatching(string $pattern)
107+
public function deleteMatching(string $pattern): never
110108
{
111109
throw new Exception('The deleteMatching method is not implemented.');
112110
}

system/Cache/Handlers/PredisHandler.php

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

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

10998
/**

system/Cache/Handlers/RedisHandler.php

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

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

135124
/**

system/Cache/ResponseCache.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ final class ResponseCache
4949
*/
5050
private int $ttl = 0;
5151

52-
private CacheInterface $cache;
53-
54-
public function __construct(CacheConfig $config, CacheInterface $cache)
52+
public function __construct(CacheConfig $config, private readonly CacheInterface $cache)
5553
{
5654
$this->cacheQueryString = $config->cacheQueryString;
57-
$this->cache = $cache;
5855
}
5956

6057
/**

system/CodeIgniter.php

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

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

873873
return $controller(...$this->router->params());
@@ -1045,7 +1045,7 @@ public function storePreviousURL($uri)
10451045
}
10461046

10471047
// Ignore non-HTML responses
1048-
if (strpos($this->response->getHeaderLine('Content-Type'), 'text/html') === false) {
1048+
if (! str_contains($this->response->getHeaderLine('Content-Type'), 'text/html')) {
10491049
return;
10501050
}
10511051

system/Commands/Database/CreateDatabase.php

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

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

114114
if (is_file($dbName)) {
115115
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
@@ -168,7 +168,7 @@ protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey):
168168
$oldFileContents = (string) file_get_contents($envFile);
169169
$replacementKey = "\nencryption.key = {$newKey}";
170170

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

0 commit comments

Comments
 (0)