[PHP] Autovivification should produce warning on union type if not guarded with IF-STATEMENT
- Dominant language
- Java
- Stars
- 3.1k
- Forks
- 935
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 17
Description
### Description
This issue is related to #5070.
#5070 is only applied on simple value.
If a statement enconter:
- a function/method with nullable or array union return type,
- or a function/method with nullable or array union argument type,
- or object property with nullable or array union type.
there should be a warning message to prevent error, but this warning can be supressed by IF-STATEMENT. The IF condition must contains ```is_null``` or ```is_array``` as a guard.
### Use case/motivation
```php
prop1;
$var2 = $inst->prop2;
$var3 = $inst->prop3;
$var4 = $inst->prop4;
$var1[] = 100; // this statement should display warning, var1 could be int
$var2[] = 100; // this statement should display warning, var2 could be int or float
$var3[] = 100; // this statement should display warning, var3 could be string
$var4[] = 100; // this statement must display error, var4 is not array nor null
if (is_null($var1)) {
$var1[] = 100; // OK, var1 is guarded
$var2[] = 100; // this statement should display warning, var2 is not guarded
$var3[] = 100; // this statement should display warning, var3 is not guarded
$var4[] = 100; // this statement must display error, var4 is not array nor null
}
if (is_array($var1)) {
// this statement should display warning, var1 is never reach here.
// i.e. warning message is not related with autovivification.
$var1[] = 100;
}
if (is_null($var4)) {
// this statement should display warning, var4 is never reach here.
// i.e. warning message is not related with autovivification.
$var4[] = 100;
}
// -------------------------------------------------------------------------------------
function func1(): ?int {}
function func2(): int|float|null {}
function func3(): string|array {}
function func4(): string|bool {}
$var5 = func1();
$var6 = func2();
$var7 = func3();
$var8 = func4();
// do the same as object properties
// -------------------------------------------------------------------------------------
$myClosure = function() use ($var1, $var2, $var4, $var4) {
// do the same as object properties
}
// -------------------------------------------------------------------------------------
function func1 (
?int $arg1,
int|float|null $arg2,
string|array $arg3,
string|bool $arg4,
) {
// do the same as object properties
}
```
### Related issues
_No response_
### Are you willing to submit a pull request?
No
### Code of Conduct
Yes
Contributor guide
Research direction
No source files or tests are named; start by reviewing related issue #5070 and reproducing the PHP examples in the issue. Done means union-typed autovivification produces warnings unless guarded by is_null or is_array, while invalid non-array cases still produce errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100