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

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>'],

system/Common.php

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,14 @@ function clean_path(string $path): string
9191
// Resolve relative paths
9292
$path = realpath($path) ?: $path;
9393

94-
switch (true) {
95-
case strpos($path, APPPATH) === 0:
96-
return 'APPPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(APPPATH));
97-
98-
case strpos($path, SYSTEMPATH) === 0:
99-
return 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(SYSTEMPATH));
100-
101-
case strpos($path, FCPATH) === 0:
102-
return 'FCPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(FCPATH));
103-
104-
case defined('VENDORPATH') && strpos($path, VENDORPATH) === 0:
105-
return 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(VENDORPATH));
106-
107-
case strpos($path, ROOTPATH) === 0:
108-
return 'ROOTPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(ROOTPATH));
109-
110-
default:
111-
return $path;
112-
}
94+
return match (true) {
95+
str_starts_with($path, APPPATH) => 'APPPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(APPPATH)),
96+
str_starts_with($path, SYSTEMPATH) => 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(SYSTEMPATH)),
97+
str_starts_with($path, FCPATH) => 'FCPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(FCPATH)),
98+
defined('VENDORPATH') && str_starts_with($path, VENDORPATH) => 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(VENDORPATH)),
99+
str_starts_with($path, ROOTPATH) => 'ROOTPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(ROOTPATH)),
100+
default => $path,
101+
};
113102
}
114103
}
115104

@@ -372,22 +361,13 @@ function env(string $key, $default = null)
372361
return $default;
373362
}
374363

375-
// Handle any boolean values
376-
switch (strtolower($value)) {
377-
case 'true':
378-
return true;
379-
380-
case 'false':
381-
return false;
382-
383-
case 'empty':
384-
return '';
385-
386-
case 'null':
387-
return null;
388-
}
389-
390-
return $value;
364+
return match (strtolower($value)) {
365+
'true' => true,
366+
'false' => false,
367+
'empty' => '',
368+
'null' => null,
369+
default => $value,
370+
};
391371
}
392372
}
393373

@@ -492,9 +472,9 @@ function force_https(int $duration = 31_536_000, ?RequestInterface $request = nu
492472

493473
$baseURL = config('App')->baseURL;
494474

495-
if (strpos($baseURL, 'https://') === 0) {
475+
if (str_starts_with($baseURL, 'https://')) {
496476
$authority = substr($baseURL, strlen('https://'));
497-
} elseif (strpos($baseURL, 'http://') === 0) {
477+
} elseif (str_starts_with($baseURL, 'http://')) {
498478
$authority = substr($baseURL, strlen('http://'));
499479
} else {
500480
$authority = $baseURL;
@@ -597,7 +577,7 @@ function helper($filenames)
597577
$appHelper = null;
598578
$localIncludes = [];
599579

600-
if (strpos($filename, '_helper') === false) {
580+
if (! str_contains($filename, '_helper')) {
601581
$filename .= '_helper';
602582
}
603583

@@ -608,7 +588,7 @@ function helper($filenames)
608588

609589
// If the file is namespaced, we'll just grab that
610590
// file and not search for any others
611-
if (strpos($filename, '\\') !== false) {
591+
if (str_contains($filename, '\\')) {
612592
$path = $loader->locateFile($filename, 'Helpers');
613593

614594
if (empty($path)) {
@@ -622,9 +602,9 @@ function helper($filenames)
622602
$paths = $loader->search('Helpers/' . $filename);
623603

624604
foreach ($paths as $path) {
625-
if (strpos($path, APPPATH . 'Helpers' . DIRECTORY_SEPARATOR) === 0) {
605+
if (str_starts_with($path, APPPATH . 'Helpers' . DIRECTORY_SEPARATOR)) {
626606
$appHelper = $path;
627-
} elseif (strpos($path, SYSTEMPATH . 'Helpers' . DIRECTORY_SEPARATOR) === 0) {
607+
} elseif (str_starts_with($path, SYSTEMPATH . 'Helpers' . DIRECTORY_SEPARATOR)) {
628608
$systemHelper = $path;
629609
} else {
630610
$localIncludes[] = $path;
@@ -1211,7 +1191,7 @@ function view_cell(string $library, $params = null, int $ttl = 0, ?string $cache
12111191
*/
12121192
function class_basename($class)
12131193
{
1214-
$class = is_object($class) ? get_class($class) : $class;
1194+
$class = is_object($class) ? $class::class : $class;
12151195

12161196
return basename(str_replace('\\', '/', $class));
12171197
}
@@ -1230,7 +1210,7 @@ function class_basename($class)
12301210
function class_uses_recursive($class)
12311211
{
12321212
if (is_object($class)) {
1233-
$class = get_class($class);
1213+
$class = $class::class;
12341214
}
12351215

12361216
$results = [];

system/ComposerScripts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function postUpdate()
7171

7272
foreach (self::$dependencies as $key => $dependency) {
7373
// Kint may be removed.
74-
if (! is_dir($dependency['from']) && strpos($key, 'kint') === 0) {
74+
if (! is_dir($dependency['from']) && str_starts_with($key, 'kint')) {
7575
continue;
7676
}
7777

0 commit comments

Comments
 (0)