vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Wrong variable type inference in try..catch block

Open AnrDaemon opened this issue 1 year ago • 1 comments

Type: Bug

<?php

try {
    something();
} catch (\RuntimeException $e) {
    $e = new $e("Something failed miserable", $e->getCode(), $e);
    if ($e->getCode()) {
        throw $e;
    }

    error_log($e);
}

$e in error_log is highlighted as being of wrong type (object vs. string). You'll have to save this snippet in order to trigger the bug.

Extension version: 1.10.2 VS Code version: Code 1.86.2 (903b1e9d8990623e3d7da1df3d33db3e42d80eda, 2024-02-13T19:40:56.878Z) OS version: Windows_NT x64 10.0.19045 Modes:

System Info
Item Value
CPUs Intel(R) Core(TM) i3-8145U CPU @ 2.10GHz (4 x 2304)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 7.86GB (0.16GB free)
Process Argv --folder-uri file:///d%3A/Profiles/anrdaemon/Documents/hk/invapi-php
Screen Reader no
VM 0%

AnrDaemon avatar Feb 16 '24 13:02 AnrDaemon

$e is an object, isn't it? And error_log accepts a string https://www.php.net/manual/en/function.error-log.php . I don't understand the issue.

bmewburn avatar Feb 17 '24 06:02 bmewburn

In a catch block, the type of $e is already known. If it is inherited from Stringable class (Throwable is Stringable), it can be used in string context, no issue.

And you can not throw non-throwable classes.

AnrDaemon avatar Feb 19 '24 10:02 AnrDaemon