Bogus "Operands have incompatible types"

This warning is not quite right, is it?

<?php

$payload = $expectedValues = [];
foreach ([...$payload, ...$expectedValues] as $field => $value) {
    $isStartDate = $field === 'startDate';
}
0
4 comments
Hello,

With PhpStorm analysis, while $field "might" be int, while 'startDate' is a string, the warning is thrown.

In case however, this inspection hits a code where variables are defined, feel free to share this.
0

Nothing changes, same warning is still shown:

$payload = [
    'foo' => true,
    'bar' => 33,
    'startDate' => '2025-06-01',
];
$expectedValues = [
    'bar' => '33',
    'startDate' => new \DateTime('2025-06-01'),
];
foreach ([...$payload, ...$expectedValues] as $field => $value) {
    $isStartDate = $field === 'startDate';
}

I don't see why a strict comparison between int and string can be wrong. That's the whole point of `===`.

0

Php Inspections (EA Extended) plugin agrees with me 😁

0

Hello,

Thank you for providing the full snippet. 

Indeed, here $field is always a string. Therefore, https://youtrack.jetbrains.com/issue/WI-81951 was filed. 

You can star and follow this one to get informed once it's resolved.

0

Please sign in to leave a comment.