1

I am migrating from PHP version 7.4 to 8.2. Part of this upgrade is some notices are now warning. The project was set to ignore all the notices but warnings. Now after upgrading there are thousands of "undefined array key" warnings.

I know how to fix these warnings manually. I would like to automate the fixing by PHP rector rule.

I am working on this but finding it really hard to consider all the scenarios. Here are some examples of before and after the rector rule applied

Scenario 1

Before

final class ClassArrayNotDefiningTheKey
{
    public function __construct()
    {
        $options = [
            'key1' => 'key1Value',
        ];
        echo $options['key2'];
    }
}

After Rule applied

final class ClassArrayNotDefiningTheKey
{
    public function __construct()
    {
        $options = [
            'key1' => 'key1Value',
            'key2' => null,
        ];
        echo $options['key2'];
    }
}

Scenario 2

Before

final class ClassArrayNotDefiningTheKey
{
    public function someFunction($options = [])
    {
        $options += [
            'key1' => 'key1Value',
        ];
        echo $options['key2'];
    }
}

After

final class ClassArrayNotDefiningTheKey
{
    public function someFunction($options = [])
    {
        $options += [
            'key1' => 'key1Value',
            'key2' => null
        ];
        echo $options['key2'];
    }
}

Scenario 3

Before

final class ClassArrayNotDefiningTheKey
{
    public function someFunction($options = [])
    {
        if ($options['key2']) {
            echo $options['key2'];
        }
        echo 'Nothing';
    }
}

After

final class ClassArrayNotDefiningTheKey
{
    public function someFunction($options = [])
    {
        if ($options['key2'] ?? null) {
            echo $options['key2'];
        }
        echo 'Nothing';
    }
}

Scenario 4

Before

final class ClassArrayNotDefiningTheKey
{
    public function someFunction($arg1, $arg2, $options = [])
    {
        if (($arg1 && $options['key2']) || $arg2) {
            echo $options['key2'];
        }
        echo 'Nothing';
    }
}

After

final class ClassArrayNotDefiningTheKey
{
    public function someFunction($arg1, $arg2, $options = [])
    {
        if (($arg1 && $options['key2'] ?? null) || $arg2) {
            echo $options['key2'];
        }
        echo 'Nothing';
    }
}

Thank you for any help you can do.

Chirag Kalani
  • 368
  • 2
  • 9
  • I'm going to suggest its much better to check that the key exists before consuming rather than adding placeholders to existing data. – Scuzzy Apr 11 '23 at 23:50
  • We have so many functions with passing array arguments and those array arguments already have some defaults set using "+=". With this approach, I can see the code and know what keys function can accept as an array argument. – Chirag Kalani Apr 11 '23 at 23:59
  • I think it has been a really bad idea by the PHP dev team to make this issue a /warning rather than a /notice. It adds a huge burden to creating and checking dynamic content and one of the advantages of PHP (back in the day) was precisely that these checks can be dynamically done with a single check rather than having to be spelt out as required now, with `isset`, and/or `array_key_exists` neither of which inherently check the actual value of the variable being checked... – Martin Apr 12 '23 at 00:32

0 Answers0