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.