From a0d693d7a9e7643cd840c3b4ec1351d5596d3c81 Mon Sep 17 00:00:00 2001 From: "Cristian O. Balan" Date: Sat, 2 Dec 2023 02:12:07 +0000 Subject: [PATCH] Update CategoriesCommand.php Fix the following when attempting to import a CSV from CLI: ``` There is an error in app/code/Macopedia/CategoryImporter/Console/Command/CategoriesCommand.php at line: 159 array_key_exists(): Argument #2 ($array) must be of type array, null given#0 app/code/Macopedia/CategoryImporter/Console/Command/CategoriesCommand.php(159): array_key_exists() #1 vendor/symfony/console/Command/Command.php(298): Macopedia\CategoryImporter\Console\Command\CategoriesCommand->execute() #2 vendor/magento/framework/Interception/Interceptor.php(58): Symfony\Component\Console\Command\Command->run() #3 vendor/magento/framework/Interception/Interceptor.php(138): Macopedia\CategoryImporter\Console\Command\CategoriesCommand\Interceptor->___callParent() #4 vendor/magento/framework/Interception/Interceptor.php(153): Macopedia\CategoryImporter\Console\Command\CategoriesCommand\Interceptor->Magento\Framework\Interception\{closure}() #5 generated/code/Macopedia/CategoryImporter/Console/Command/CategoriesCommand/Interceptor.php(23): Macopedia\CategoryImporter\Console\Command\CategoriesCommand\Interceptor->___callPlugins() #6 vendor/symfony/console/Application.php(1040): Macopedia\CategoryImporter\Console\Command\CategoriesCommand\Interceptor->run() #7 vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand() #8 vendor/magento/framework/Console/Cli.php(116): Symfony\Component\Console\Application->doRun() #9 vendor/symfony/console/Application.php(171): Magento\Framework\Console\Cli->doRun() #10 bin/magento(23): Symfony\Component\Console\Application->run() #11 {main} ``` According to ChatGPT :) The error is due to the `array_key_exists` function being called on a null array. Specifically, the issue is in the `mapHeaders` method on line 159. This method is supposed to map headers from the file to row keys, but there seems to be an issue with the way it's implemented. To fix this issue, we can modify the `mapHeaders` method to handle the case where `$item` is not found in the `$headers` array. In this version, I replaced the nested loop with a call to `in_array` to check if the current `$item` exists in the `$headers` array. If it does, the corresponding key is set in the `$headersMap` array. --- Console/Command/CategoriesCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Console/Command/CategoriesCommand.php b/Console/Command/CategoriesCommand.php index c757cff..7aa1289 100644 --- a/Console/Command/CategoriesCommand.php +++ b/Console/Command/CategoriesCommand.php @@ -306,11 +306,10 @@ protected function addOrUpdateCategory($data, $isParent = false) protected function mapHeaders($row) { $headers = array_merge($this->requiredHeaders, $this->optionalHeaders, $this->additionalHeaders); + foreach ($row as $key => $item) { - foreach ($headers as $header) { - if($item == $header) { - $this->headersMap[$header] = $key; - } + if (in_array($item, $headers)) { + $this->headersMap[$item] = $key; } } }