bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.57k stars 93 forks source link

erroneous expect type 'string' found 'void' #2866

Closed corretge closed 1 month ago

corretge commented 2 months ago

I get an error: Expected type 'string'. Found 'void'. intelephense(P1006)

while the result of __toString() is typed as string but intelephense thinks that it is void.

$txt = $output->__toString();
$this->assertStringContainsString(
  'This is the help message', 
  (string) $txt
);

look the capture

To Reproduce It is not happening in all the places that I assertStringContainsString with the result of __toString().

I fixed it typing:

$this->assertStringContainsString(
  'This is the help message', 
  (string) $txt
);

Expected behavior As happens in other parts of the code

Screenshots

image image

Platform and version Mac OS Sonoma 14.4.1

intelephense v1.10.4

pjylkka commented 2 months ago

Maybe this \Marshal\Http\Output\Raw() has __toString() method in there - but it's something like

public function __toString() {
    if(!is_null($this->rawData)) {
        return $this->rawData
    }

    // If the condition above doesn't match, there is no backup return clause,
    // potentially resulting in a return of :void.

}

If \Marshal\Http\Output\Raw() would implement \Stringable interface, intelephense would have pointed out earlier that __toString() potentially returns :void

..this was just a wild guess. Can't tell as I haven't seen the code, but hopefully this helps tho ^^

$this->assertStringContainsString(
  'This is the help message', 
  (string) $txt // This turns even void or null to empty string so this is where I based my guess
);

So adding backup return to __toString()

public function __toString() {
    if(!is_null($this->rawData)) {
        return $this->rawData
    }

   return 'There were nothing to turn to string..!';
}

Would fix the issue..!

bmewburn commented 2 months ago

@corretge if you navigate to the __toString definition, does it have an incorrect @return annotation?