eclipse-pdt / pdt

PHP Development Tools project (PDT)
https://eclipse.org/pdt
Eclipse Public License 2.0
188 stars 46 forks source link

Missing error/warning on autovivification on union type if not guarded with IF-STATEMENT #203

Open the-liquid-metal opened 1 year ago

the-liquid-metal commented 1 year ago

Bug Description Missing error/warning on autovivification on union type if not guarded with IF-STATEMENT.

Eclipse environment Version: 2023-06 (4.28.0) Build id: 20230608-1333 PDT: 8.0.0.202306050832

System

To Reproduce Steps to reproduce the behavior: copy-paste this script to the IDE

<?php
declare(strict_types=1);

namespace ns1\ns2;

class Test27 {
    public ?int           $prop1;
    public int|float|null $prop2;
    public string|array   $prop3;
    public string|bool    $prop4;
}

$inst = new Test27;

$var1 = $inst->prop1;
$var2 = $inst->prop2;
$var3 = $inst->prop3;
$var4 = $inst->prop4;

$var1[] = 100; // this statement should trigger warning, var1 could be int
$var2[] = 100; // this statement should trigger warning, var2 could be int or float
$var3[] = 100; // this statement should trigger warning, var3 could be string
$var4[] = 100; // this statement must trigger error, var4 is not array nor null

if (is_null($var1)) {
    $var1[] = 100; // OK, var1 is guarded
    $var2[] = 100; // this statement should trigger warning, var2 is not guarded
    $var3[] = 100; // this statement should trigger warning, var3 is not guarded
    $var4[] = 100; // this statement must trigger error, var4 is not array nor null
}

if (is_array($var1)) {
    // this statement should trigger warning, var1 is never reach here.
    // i.e. warning message is not related with autovivification.
    $var1[] = 100;
}

if (is_null($var4)) {
    // this statement should trigger 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

// -------------------------------------------------------------------------

$var9 = 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
}

var_dump($var1, $var2, $var3, $var4, $var5, $var6, $var7, $var8, $var9);