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

Support https://wiki.php.net/rfc/throw_expression if merged #3849

Closed
TysonAndre opened this issue Apr 11, 2020 · 0 comments · Fixed by #3852
Closed

Support https://wiki.php.net/rfc/throw_expression if merged #3849

TysonAndre opened this issue Apr 11, 2020 · 0 comments · Fixed by #3852
Labels
enhancement This improves the quality of Phan's analysis of a codebase php8 Tracks changes to prepare for analyzing php 8 code

Comments

@TysonAndre
Copy link
Member

TysonAndre commented Apr 11, 2020

  • Warn about using throw as the left hand side of a binary operator, or for a unary operator
  • If possible, make this affect type inference in statements or conditions (is_string($x) || throw new RuntimeException())
  • Warn about using the return value $x = throw new Exception(). Until never/no-return is supported, void is probably the best option.

-(throw new Exception())

$condition || throw new Exception('$condition must be truthy')
  && $condition2 || throw new Exception('$condition2 must be truthy');
// Evaluated as
$condition || (throw new Exception('$condition must be truthy') && $condition2 || (throw new Exception('$condition2 must be truthy')));

Other examples from the RFC:

// This was previously not possible since arrow functions only accept a single expression while throw was a statement.
$callable = fn() => throw new Exception();
 
// $value is non-nullable.
$value = $nullableValue ?? throw new InvalidArgumentException();
 
// $value is truthy.
$value = $falsableValue ?: throw new InvalidArgumentException();
 
// $value is only set if the array is not empty.
$value = !empty($array)
    ? reset($array)
    : throw new InvalidArgumentException();

it would help to check if an expression unconditionally throws in UnionTypeVisitor->visitConditional() for the ternary operator and ??

@TysonAndre TysonAndre added enhancement This improves the quality of Phan's analysis of a codebase php8 Tracks changes to prepare for analyzing php 8 code labels Apr 11, 2020
TysonAndre added a commit to TysonAndre/phan that referenced this issue Apr 11, 2020
To be merged if the RFC gets merged into php 8.0-dev.

Fixes phan#3849
TysonAndre added a commit to TysonAndre/phan that referenced this issue Apr 11, 2020
To be merged if the RFC gets merged into php 8.0-dev.

Fixes phan#3849
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This improves the quality of Phan's analysis of a codebase php8 Tracks changes to prepare for analyzing php 8 code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant