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

Custom exit/error functions #2118

Closed
bugreportuser opened this issue Nov 5, 2018 · 4 comments · Fixed by #4406
Closed

Custom exit/error functions #2118

bugreportuser opened this issue Nov 5, 2018 · 4 comments · Fixed by #4406
Assignees
Labels
enhancement This improves the quality of Phan's analysis of a codebase

Comments

@bugreportuser
Copy link
Contributor

Sometimes, it's helpful to use a custom function that always exits. Psalm now detects these automatically and allows them to be configured, but I couldn't find the same for Phan.

Example:

<?php
declare(strict_types=1);

function customExit1 () {
    throw new Exception();
}

function customExit2 () {
    trigger_error('msg', E_USER_ERROR);
}

function customExit3 () {
    exit;
}

$val = strpos('a', 'b');
if ($val === false) {
    customExit1();
    customExit2();
    customExit3();
}
var_dump(strlen($val));

Example output:

$ phan exit.php
exit.php:22 PhanTypeMismatchArgumentInternal Argument 1 (string) is false|int but \strlen() takes string
@TysonAndre
Copy link
Member

My current thoughts would be to support these checks with global functions and static method invocations (i.e. anything that can be inferred without knowing variable types and added to a more precise BlockExitStatusChecker)

@TysonAndre TysonAndre added the enhancement This improves the quality of Phan's analysis of a codebase label Nov 5, 2018
@bugreportuser
Copy link
Contributor Author

bugreportuser commented Nov 6, 2018

That's reasonable. It should be rare to have dynamic functions that always exit.

@TysonAndre
Copy link
Member

The other approach would be to manually set all of the flags at runtime (for instance flags), and recursively invalidate and recompute the flags for parent nodes until they stop changing or the end of the current function scope is reached

@TysonAndre TysonAndre self-assigned this Sep 2, 2019
b1rdex added a commit to b1rdex/fig-standards that referenced this issue Oct 10, 2019
This type should be used mainly as a return type to designate a function/method as a never returning back.
It shows that the function/method will always throw an exception or will call `exit()` or will call `trigger_error()` or will stop program execution in any other available way.

This return type is available in many languages/tools:
1. Kotlin — `Nothing` return type. See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-nothing.html
2. Hack lang — `noreturn` return type. See https://docs.hhvm.com/hack/built-in-types/noreturn (examples: https://github.com/facebookarchive/hack-langspec/blob/master/tests/Functions/noreturn.php)
3. Vimeo Psalm — `@return no-return` PhpDoc type. See vimeo/psalm#1155 (comment)
+ they have config option, but it's only for functions/static methods and is going to be deprecated/removed in the future.
4. Phpstan has @return never PhpDoc type. See phpstan/phpstan#1472
+ they have config option and it's possible to specify dynamic method calls there too.
5. Phan doesn't have this — see phan/phan#2118, but they plan to add it.
6. PhpStorm doesn't have this feature, but they plan to add it and the PhpDoc tag is not decided yet. See https://youtrack.jetbrains.com/issue/WI-10673
TysonAndre added a commit to TysonAndre/phan that referenced this issue May 15, 2021
Closes phan#2118
Closes phan#4405
Related to phan#3602

Allow using closures, global functions, and instance/static methods as
custom exit/error functions if they have a return type of never.
Note: This may result in false positives for control flow analysis of
unreachable types if object instances with different return types are
passed in, in different invocations of methods

Start using `no-return` in Phan itself for phpdoc
(in other tools, handling on `never` depends on the php version used)

Fix parsing of new hyphenated `@return no-return|never-returns?` type.
@bugreportuser
Copy link
Contributor Author

Thanks!

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants