Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call to __toString may return Exception #2752

Closed
orklah opened this issue Feb 5, 2020 · 2 comments
Closed

call to __toString may return Exception #2752

orklah opened this issue Feb 5, 2020 · 2 comments
Labels

Comments

@orklah
Copy link
Collaborator

orklah commented Feb 5, 2020

Hi,

I'm not sure it will be worth fixing but I'll report it anyway.

While working on phpDocumentor/phpDocumentor#2263

I noticed Psalm suggested to delete a cast to string that seemed not useful: https://github.com/phpDocumentor/phpDocumentor/pull/2263/files#diff-5844ff81b5eb5ff658aabe38d06279fdL70

I reproduced something similar in psalm.dev:
https://psalm.dev/r/d50c69dac0

We have an exception that could be thrown in __toString (note that this is valid from PHP 7.4 only).

The cast to (string) on a class that implements __toString can actually be valid code that may throw and that could be catched and handled.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/d50c69dac0
<?php
  
class maybe_stringable{
 /** @throws Exception */
  public function __toString(){
     if(rand(0,1)){
      	throw new exception(); 
     }
     return "";
  }
}

$a = new maybe_stringable();
try{
  (string) $a;
}
catch(Exception $e){
  //this class is not stringable
}
Psalm output (using commit b486bd4):

No issues!

@muglug muglug added the bug label Feb 7, 2020
@muglug muglug closed this as completed in e567f8c Feb 7, 2020
@muglug
Copy link
Collaborator

muglug commented Feb 7, 2020

I'm not sure it will be worth fixing but I'll report it anyway.

Anything that deletes working code is a bug that's worth fixing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants