magento / data-migration-tool

Magento Data Migration Tool
https://devdocs.magento.com/guides/v2.3/migration/bk-migration-guide.html
Open Software License 3.0
334 stars 200 forks source link

Migration\Handler\SetValue not compatible with PHP 8.1 #906

Open rk-mxp opened 1 year ago

rk-mxp commented 1 year ago

Preconditions

  1. Magento 1 with tier prices
  2. Migration to Magento 2.4.5 (opensource to opensource)
  3. PHP 8.1

Steps to reproduce

  1. Migrate Magento 1 to Magento 2

Expected result

  1. Migration completes successfully

Actual result

Error in the OrderGrids Step:

[2022-12-05T08:22:49.790153+00:00][DEBUG][mode: data][stage: data migration][step: Tier Price Step][table: catalog_product_entity_tier_price]: migrating
0% [>---------------------------] Remaining Time: < 1 sec
In ErrorHandler.php line 62:

  [Exception]                                                                  
  Deprecated Functionality: substr(): Passing null to parameter #1 ($string)   
  of type string is deprecated in /var/www/html/magento/vendor/magento/data-m  
  igration-tool/src/Migration/Handler/SetValue.php on line 35                  

Exception trace:
  at /var/www/html/magento/vendor/magento/framework/App/ErrorHandler.php:62
 Magento\Framework\App\ErrorHandler->handler() at n/a:n/a
 substr() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/Handler/SetValue.php:35
 Migration\Handler\SetValue->handle() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/RecordTransformer.php:126
 Migration\RecordTransformer->applyHandlers() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/RecordTransformer.php:74
 Migration\RecordTransformer->transform() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/Step/TierPrice/Data.php:139
 Migration\Step\TierPrice\Data->perform() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/Mode/AbstractMode.php:82
 Migration\Mode\AbstractMode->runStage() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/Mode/Data.php:124
 Migration\Mode\Data->runData() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/Mode/Data.php:69
 Migration\Mode\Data->run() at /var/www/html/magento/vendor/magento/data-migration-tool/src/Migration/Console/MigrateDataCommand.php:59
 Migration\Console\MigrateDataCommand->execute() at /var/www/html/magento/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /var/www/html/magento/vendor/magento/framework/Interception/Interceptor.php:58
 Migration\Console\MigrateDataCommand\Interceptor->___callParent() at /var/www/html/magento/vendor/magento/framework/Interception/Interceptor.php:138
 Migration\Console\MigrateDataCommand\Interceptor->Magento\Framework\Interception\{closure}() at /var/www/html/magento/vendor/magento/framework/Interception/Interceptor.php:153
 Migration\Console\MigrateDataCommand\Interceptor->___callPlugins() at /var/www/html/magento/generated/code/Migration/Console/MigrateDataCommand/Interceptor.php:77
 Migration\Console\MigrateDataCommand\Interceptor->run() at /var/www/html/magento/vendor/symfony/console/Application.php:1021
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/magento/vendor/symfony/console/Application.php:275
 Symfony\Component\Console\Application->doRun() at /var/www/html/magento/vendor/magento/framework/Console/Cli.php:116
 Magento\Framework\Console\Cli->doRun() at /var/www/html/magento/vendor/symfony/console/Application.php:149
 Symfony\Component\Console\Application->run() at /var/www/html/magento/bin/magento:23

Additional notes

The problem is triggered by the map-tier-price.xml.dist configuration

            <transform>
                <field>catalog_product_entity_group_price.value_id</field>
                <handler class="\Migration\Handler\SetValue">
                    <param name="value" value="NULL" />
                </handler>
            </transform>
            <transform>
                <field>catalog_product_entity_tier_price.value_id</field>
                <handler class="\Migration\Handler\SetValue">
                    <param name="value" value="NULL" />
                </handler>
            </transform>

because the Migration\Handler\SetValue class throws an error if NULL should be set:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Migration\Handler;

use Migration\ResourceModel\Record;

/**
 * Handler to set constant value to the field
 */
class SetValue extends AbstractHandler implements HandlerInterface
{
    /**
     * @var string
     */
    protected $value;

    /**
     * @param string $value
     */
    public function __construct($value)
    {
        $this->value = (strtoupper($value) === 'NULL') ? null : $value;
    }

    /**
     * @inheritdoc
     */
    public function handle(Record $recordToHandle, Record $oppositeRecord)
    {
        $this->validate($recordToHandle);
        $valueStored = $recordToHandle->getValue($this->field);
        $operator = substr($this->value, 0, 1);
        $value = substr($this->value, 1);
        switch ($operator) {
            case '+':
                $value = $valueStored + $value;
                break;
            case '-';
                $value = $valueStored - $value;
                break;
            default:
                $value = $this->value;
        }
        $recordToHandle->setValue($this->field, $value);
    }
}

The problem are the lines

        $operator = substr($this->value, 0, 1);
        $value = substr($this->value, 1);

if $this->value is null, since null musn't be used anymore in substr.

As a workaround I changed the lines to

        $operator = substr($this->value ?? '', 0, 1);
        $value = substr($this->value ?? '', 1);

and could finish the migration.

m2-assistant[bot] commented 1 year ago

Hi @Barakur. Thank you for your report. To speed up processing of this issue, make sure that you provided sufficient information.

Add a comment to assign the issue: @magento I am working on this


cljk commented 1 year ago

Thanks - this fix saved my migration ... because other parts of data-migration-tool are not compatible with PHP 7.4 - so have to use 8.1

OnyXxL commented 1 year ago

I had the same problem and your solution fixed it, thank you @Barakur If the migration fails at that step, the products won't be linked to new stock model.