Skip to content

setup:di:compile breaks on 0 indexed object arrays #8803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yariksheptykin opened this issue Mar 6, 2017 · 3 comments
Closed

setup:di:compile breaks on 0 indexed object arrays #8803

yariksheptykin opened this issue Mar 6, 2017 · 3 comments
Labels
bug report Fixed in 2.2.x The issue has been fixed in 2.2 release line Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release

Comments

@yariksheptykin
Copy link

Array arguments defined containing 0-indexed object items break setup:di:compile with the following exception:

  [Exception]                                                                                                                                                            
  Warning: ltrim() expects parameter 1 to be string, array given in setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/BackslashTrim.php on line 61

A sample di.xml breaking down:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="SomeType" type="\Magento\Framework\DataObject">
        <arguments>
            <argument name="data" xsi:type="array">
                <item name="0" xsi:type="object">\Magento\Framework\DataObject</item>
            </argument>
        </arguments>
    </virtualType>
</config>

NOTE the <item name="0" xsi:type="object">SomeInterface</item> which is a 0-indexed object item referring to an interface.

Normally the code should not be executed on array because lines 65-67 trigger a recursive call on array values. However ltrim can be called on array if the condition in_array($key, ['_i_', '_ins_']) evaluates to true and $value is array. This does evaluate to true for $key = 0, which can happen when resolving array parameters. Surprisingly expression in_array(0, ['_i_', '_ins_']) evaluates to true, because each value in array ['_i_', '_ins_'] will be converted to integer when the needle is of type integer. 0 is therefore always is in ['_i_', '_ins_'].

With the sample di.xml the code will eventually be called with:

>> print_r($argument);
Array
(
    [0] => Array
        (
            [_i_] => \Magento\Framework\DataObject
        )

)

>> print_r($key);
0

Which causes the crash.

Preconditions

  1. Magento 2.1.4
  2. PHP 7.0.15

Steps to reproduce

  1. Insert virtual attribute declaration from sample di.xml into any module.
  2. execute setup:di:compile

Expected result

  1. Dependencies are compiled

Actual result

  [Exception]                                                                                                                                                            
  Warning: ltrim() expects parameter 1 to be string, array given in setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/BackslashTrim.php on line 61
@Volvoxpl
Copy link

Volvoxpl commented Jul 4, 2017

I had this same problem:

<type name="VENDOR\MODULE\Model\Test">
    <arguments>
        <argument name="factories"  xsi:type="array">
            <item name="0" xsi:type="object">VENDOR\MODULE\Model\Somemodel</item>
            <item name="1" xsi:type="object">VENDOR\MODULE\Model\OtherModel</item>
        </argument>
    </arguments>
</type>

I needed to change enumeration on array starting from 1 instead of 0 to fix it.
Magento 2.1.6

@magento-engcom-team magento-engcom-team added 2.1.x bug report Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed and removed G1 Passed labels Sep 5, 2017
@okorshenko okorshenko removed the 2.1.x label Dec 14, 2017
@magento-engcom-team magento-engcom-team added Fixed in 2.2.x The issue has been fixed in 2.2 release line Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Mar 5, 2018
@magento-engcom-team
Copy link
Contributor

@yariksheptykin, thank you for your report.
The issue is already fixed in 2.2.3

@magento-engcom-team magento-engcom-team removed the Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release label Mar 5, 2018
@Ctucker9233
Copy link

@magento-engcom-team If you point me to the right commits I can create a backport for 2.1. Why do you close issues when they're not fixed in their original branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Fixed in 2.2.x The issue has been fixed in 2.2 release line Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release
Projects
None yet
Development

No branches or pull requests

6 participants