From cdd078c5e5985474920f07edfc31841b06175779 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 13 Mar 2021 01:17:32 +0700 Subject: [PATCH] [Rector] Apply Rector: UnusedForeachValueToArrayKeysRector --- rector.php | 2 ++ system/BaseModel.php | 8 +++++--- system/CLI/BaseCommand.php | 2 +- system/CLI/Commands.php | 2 +- system/CodeIgniter.php | 2 +- system/Commands/Database/MigrateStatus.php | 2 +- system/Commands/ListCommands.php | 2 +- system/Config/BaseConfig.php | 2 +- system/Database/BaseUtils.php | 2 +- system/Email/Email.php | 2 +- system/HTTP/CURLRequest.php | 2 +- system/HTTP/Cookie/CookieStore.php | 2 +- system/HTTP/MessageTrait.php | 2 +- system/HTTP/ResponseTrait.php | 2 +- system/Helpers/form_helper.php | 10 +++++----- system/Model.php | 2 +- system/View/Cell.php | 2 +- tests/system/HTTP/RedirectResponseTest.php | 2 +- 18 files changed, 27 insertions(+), 23 deletions(-) diff --git a/rector.php b/rector.php index 12874c123c..d3eee90f6a 100644 --- a/rector.php +++ b/rector.php @@ -2,6 +2,7 @@ use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector; use Rector\CodeQuality\Rector\For_\ForToForeachRector; +use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector; use Rector\CodeQuality\Rector\If_\CombineIfRector; use Rector\CodeQuality\Rector\If_\ShortenElseIfRector; @@ -70,4 +71,5 @@ return static function (ContainerConfigurator $containerConfigurator): void { $services->set(ShortenElseIfRector::class); $services->set(RemoveUnusedForeachKeyRector::class); $services->set(SimplifyIfElseToTernaryRector::class); + $services->set(UnusedForeachValueToArrayKeysRector::class); }; diff --git a/system/BaseModel.php b/system/BaseModel.php index 2cbcc8fba4..4aeaa9048a 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -299,7 +299,9 @@ abstract class BaseModel $this->tempUseSoftDeletes = $this->useSoftDeletes; $this->tempAllowCallbacks = $this->allowCallbacks; - /** @var Validation $validation */ + /** + * @var Validation $validation + */ $validation = $validation ?? Services::validation(null, false); $this->validation = $validation; @@ -1177,7 +1179,7 @@ abstract class BaseModel throw DataException::forInvalidAllowedFields(get_class($this)); } - foreach ($data as $key => $val) + foreach (array_keys($data) as $key) { if (! in_array($key, $this->allowedFields, true)) { @@ -1447,7 +1449,7 @@ abstract class BaseModel return []; } - foreach ($rules as $field => $rule) + foreach (array_keys($rules) as $field) { if (! array_key_exists($field, $data)) { diff --git a/system/CLI/BaseCommand.php b/system/CLI/BaseCommand.php index 996f4b0ef8..d34a1b0f74 100644 --- a/system/CLI/BaseCommand.php +++ b/system/CLI/BaseCommand.php @@ -226,7 +226,7 @@ abstract class BaseCommand public function getPad(array $array, int $pad): int { $max = 0; - foreach ($array as $key => $value) + foreach (array_keys($array) as $key) { $max = max($max, strlen($key)); } diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index d3ce7f636e..2b55710a9b 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -200,7 +200,7 @@ class Commands { $alternatives = []; - foreach ($collection as $commandName => $attributes) + foreach (array_keys($collection) as $commandName) { $lev = levenshtein($name, $commandName); diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 9d58c4f0f9..39f3bc3c86 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -669,7 +669,7 @@ class CodeIgniter $output = $cachedResponse['output']; // Clear all default headers - foreach ($this->response->headers() as $key => $val) + foreach (array_keys($this->response->headers()) as $key) { $this->response->removeHeader($key); } diff --git a/system/Commands/Database/MigrateStatus.php b/system/Commands/Database/MigrateStatus.php index 89b5493ca9..e660d023a9 100644 --- a/system/Commands/Database/MigrateStatus.php +++ b/system/Commands/Database/MigrateStatus.php @@ -90,7 +90,7 @@ class MigrateStatus extends BaseCommand // Collection of migration status $status = []; - foreach ($namespaces as $namespace => $path) + foreach (array_keys($namespaces) as $namespace) { if (ENVIRONMENT !== 'testing') { diff --git a/system/Commands/ListCommands.php b/system/Commands/ListCommands.php index 8664b67b48..94b13de6e3 100644 --- a/system/Commands/ListCommands.php +++ b/system/Commands/ListCommands.php @@ -138,7 +138,7 @@ class ListCommands extends BaseCommand */ protected function listSimple(array $commands) { - foreach ($commands as $title => $command) + foreach (array_keys($commands) as $title) { CLI::write($title); } diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 0436fde774..9c73455584 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -102,7 +102,7 @@ class BaseConfig { if (is_array($property)) { - foreach ($property as $key => $val) + foreach (array_keys($property) as $key) { $this->initEnvValue($property[$key], "{$name}.{$key}", $prefix, $shortPrefix); } diff --git a/system/Database/BaseUtils.php b/system/Database/BaseUtils.php index a5b3c44065..8bc9eb8012 100644 --- a/system/Database/BaseUtils.php +++ b/system/Database/BaseUtils.php @@ -339,7 +339,7 @@ abstract class BaseUtils // Did the user submit any preferences? If so set them.... if (! empty($params)) { - foreach ($prefs as $key => $val) + foreach (array_keys($prefs) as $key) { if (isset($params[$key])) { diff --git a/system/Email/Email.php b/system/Email/Email.php index f2b998991b..a3c3b274da 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -410,7 +410,7 @@ class Email $config = get_object_vars($config); } - foreach (get_class_vars(get_class($this)) as $key => $value) + foreach (array_keys(get_class_vars(get_class($this))) as $key) { if (property_exists($this, $key) && isset($config[$key])) { diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index dbfacbe963..a85152d401 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -482,7 +482,7 @@ class CURLRequest extends Request $set = []; - foreach ($headers as $name => $value) + foreach (array_keys($headers) as $name) { $set[] = $name . ': ' . $this->getHeaderLine($name); } diff --git a/system/HTTP/Cookie/CookieStore.php b/system/HTTP/Cookie/CookieStore.php index 43b6571818..2e414c15e5 100644 --- a/system/HTTP/Cookie/CookieStore.php +++ b/system/HTTP/Cookie/CookieStore.php @@ -175,7 +175,7 @@ class CookieStore implements Countable, IteratorAggregate $store = clone $this; - foreach ($store->cookies as $index => $cookie) + foreach (array_keys($store->cookies) as $index) { if ($index === $id) { diff --git a/system/HTTP/MessageTrait.php b/system/HTTP/MessageTrait.php index 4bc66833d5..aea8fe2bab 100644 --- a/system/HTTP/MessageTrait.php +++ b/system/HTTP/MessageTrait.php @@ -86,7 +86,7 @@ trait MessageTrait } unset($contentType); - foreach ($_SERVER as $key => $val) + foreach (array_keys($_SERVER) as $key) { if (sscanf($key, 'HTTP_%s', $header) === 1) { diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index c2a917780f..3820bc5fed 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -494,7 +494,7 @@ trait ResponseTrait header(sprintf('HTTP/%s %s %s', $this->getProtocolVersion(), $this->getStatusCode(), $this->getReason()), true, $this->getStatusCode()); // Send all of our headers - foreach ($this->getHeaders() as $name => $values) + foreach (array_keys($this->getHeaders()) as $name) { header($name . ': ' . $this->getHeaderLine($name), false, $this->getStatusCode()); } diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 506c54dec8..431014636c 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -313,10 +313,10 @@ if (! function_exists('form_multiselect')) /** * Multi-select menu * - * @param mixed $name - * @param array $options - * @param array $selected - * @param mixed $extra + * @param mixed $name + * @param array $options + * @param array $selected + * @param mixed $extra * * @return string */ @@ -932,7 +932,7 @@ if (! function_exists('parse_form_attributes')) { if (is_array($attributes)) { - foreach ($default as $key => $val) + foreach (array_keys($default) as $key) { if (isset($attributes[$key])) { diff --git a/system/Model.php b/system/Model.php index bc206c7780..2dee3d246d 100644 --- a/system/Model.php +++ b/system/Model.php @@ -610,7 +610,7 @@ class Model extends BaseModel { $data = is_array($key) ? $key : [$key => $value]; - foreach ($data as $k => $v) + foreach (array_keys($data) as $k) { $this->tempData['escape'][$k] = $escape; } diff --git a/system/View/Cell.php b/system/View/Cell.php index bded0ff48b..acb58de777 100644 --- a/system/View/Cell.php +++ b/system/View/Cell.php @@ -141,7 +141,7 @@ class Cell } } - foreach ($paramArray as $key => $val) + foreach (array_keys($paramArray) as $key) { if (! isset($methodParams[$key])) { diff --git a/tests/system/HTTP/RedirectResponseTest.php b/tests/system/HTTP/RedirectResponseTest.php index 2ca441b5c6..3e5c211127 100644 --- a/tests/system/HTTP/RedirectResponseTest.php +++ b/tests/system/HTTP/RedirectResponseTest.php @@ -268,7 +268,7 @@ class RedirectResponseTest extends CIUnitTestCase $_SESSION = []; $baseResponse = service('response'); - foreach ($baseResponse->headers() as $key => $val) + foreach (array_keys($baseResponse->headers()) as $key) { $baseResponse->removeHeader($key); }