phpinspectionsea
phpinspectionsea copied to clipboard
False-positive "not matching the resolved parameter type" with $_POST
Subject | Details |
---|---|
Plugin | Php Inspections (EA Extended) |
Language level | PHP 8.3 |
Current behaviour
Code like
public function setAvatar(string $fileid = ''): array
{
$fileid = $_POST['avatar'] ?? $fileid ?? '';
return [];
}
results in [EA] New value type (array) is not matching the resolved parameter type and might introduce types-related false-positives.
on line 3.
Adding is_string
check for $_POST['avatar'] before this does not help, either (now on line 4)
public function setAvatar(string $fileid = ''): array
{
if (is_string($_POST['avatar'])) {
$fileid = $_POST['avatar'];
}
$fileid = $fileid ?? '';
return [];
}
The only way to "fix" this is to change type of $fileid
by allowing array:
public function setAvatar(string|array $fileid = ''): array
{
$fileid = $_POST['avatar'] ?? $fileid ?? '';
return [];
}
Which suggests that $_POST['avatar']
is interpreted as array regardless, even though, technically, it should be mixed
.
Expected behaviour
While it may be a valid point to mark the first example as potential issue, second example with type check should not be marked, at all. This may be related to #1930, but creating as a new bug, since description seem to suggest a different set of expectations, which may be specific to example there.
Environment details
PhpStorm 2023.3.6
Build #PS-233.15026.12, built on March 21, 2024
Licensed to simbiat.ru / Dmitry Kustov
Subscription is active until May 11, 2024.
For non-commercial open source development only.
Runtime version: 17.0.10+1-b1087.23 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11.0
GC: G1 Young Generation, G1 Old Generation
Memory: 5120M
Cores: 16
Registry:
debugger.new.tool.window.layout=true
run.processes.with.pty=TRUE
ide.experimental.ui=true
Non-Bundled Plugins:
com.jetbrains.space (233.15026.16)
com.kalessil.phpStorm.phpInspectionsEA (5.0.0.0)
i wonder where the inspection is getting array
as a possible assignment type... Is it because you're returning []
, which is never assigned to the variable in the first place?
What happens if you change the return type to string
and return '';
?
Strange behavior.
This is not related to return type (and changing it will not do anything), but to the fact, that function defines type for $fileid
, and I am then assigning to it something, that can be not a string. $_POST['avatar']
is treated as mixed
, but array
is probably taken from $_POST
, which is, indeed, an array.