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

Add DateInterval extension #520

Merged
merged 1 commit into from May 17, 2021

Conversation

VincentLanglet
Copy link
Contributor

Follow up of the discussion 181f75c#r50600099

I try to add some ThrowTypeExtension. This one seems easy.

The others in https://github.com/pepakriz/phpstan-exception-rules/tree/master/src/Extension are

  • DOMDocumentExtension: This one is half-wrong. It makes the code believe method can throw an ErrorException but I discover it only throw a Warning and the error handler is transforming this to ErrorException when using APP_DEBUG=true with Symfony. Still, empty string will throw an error in PHP8.
    http://sandbox.onlinephpfunctions.com/code/9a26ff8a86809b4e9848d9635ec0debeee36e355.

  • IntdivExtension

  • JsonEncodeDecodeExtension

  • ReflectionExtension

  • SimpleXMLElementExtension

  • SplFileObjectExtension

I'll try to add more after this PR.
First I'd like to understand something @ondrejmirtes. When I'm returning

return $methodReflection->getThrowType();

I assume this rely on the fact that the exception which can be thrown by the method is well documented. Where can I check if it is ? Do you rely on Phpstorm stubs ?

Does it means for instance that intdiv is not considered as throwing an exception because the phpdoc does not contains throws tag ?

/**
 * Integer division
 * @link https://php.net/manual/en/function.intdiv.php
 * @param int $num1 <p>Number to be divided.</p>
 * @param int $num2 <p>Number which divides the <b><i>dividend</i></b></p>
 * @return int <p>
 * If divisor is 0, a {@link DivisionByZeroError} exception is thrown.
 * If the <b><i>dividend</i></b> is <b>PHP_INT_MIN</b> and the <b><i>divisor</i></b> is -1,
 * then an {@link ArithmeticError} exception is thrown.
 * </p>
 * @since 7.0
 */
#[Pure]
function intdiv ($num1, $num2) {}

@ondrejmirtes
Copy link
Member

return $methodReflection->getThrowType();

This will return the type from @throws above method declaration. Yes, if phpstorm-stubs have it then it will returned. Please note that stub files from /stubs have priority over phpstorm-stubs.

Does it means for instance that intdiv is not considered as throwing an exception because

Missing @throws can either mean that it will not throw anything, or that it can throw anything. See: https://phpstan.org/blog/bring-your-exceptions-under-control#what-does-absent-%40throws-above-a-function-mean%3F

See also this commit: 09d3488

@ondrejmirtes ondrejmirtes merged commit 84ea0e1 into phpstan:master May 17, 2021
@ondrejmirtes
Copy link
Member

Thank you!

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