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

Exception throw from callable are not reported #10142

Closed
amenophis opened this issue Nov 17, 2023 · 6 comments
Closed

Exception throw from callable are not reported #10142

amenophis opened this issue Nov 17, 2023 · 6 comments

Comments

@amenophis
Copy link

amenophis commented Nov 17, 2023

Bug report

Hello,

Exceptions thrown in array_map callback are not reported.
I think this is due to array_map function didn't describe exceptions

I tried to reproduce on the playground, but the exception configuration is enabled (https://phpstan.org/r/87bc2b85-0d85-4ce9-8b77-c071536b9785)

How can we make phpstan knows that function that use callback can throws the same exceptions as callback describes ones ?

Thanks

Configuration

parameters:
    level: max
    paths:
        - src
    exceptions:
        check:
            missingCheckedExceptionInThrows: true

Code sample

<?php

/**
 * @throws \Exception
 */
function iCanThrow(): void
{
    if (rand(0, 1) === 1) {
        throw new \Exception();
    }
}

function main(): void
{
    iCanThrow();

    array_map(function ($i) {
        iCanThrow();
    }, ['1']);
}

Code snippet that reproduces the problem

No response

Expected output

 ------ --------------------------------------------------------------------------------------------------
  Line   src/phpstan.php
 ------ --------------------------------------------------------------------------------------------------
  15     Function main() throws checked exception Exception but it's missing from the PHPDoc @throws tag.
  18     Function main() throws checked exception Exception but it's missing from the PHPDoc @throws tag.
 ------ --------------------------------------------------------------------------------------------------

Did PHPStan help you today? Did it make you happy in any way?

No response

Copy link

mergeable bot commented Nov 17, 2023

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@staabm
Copy link
Contributor

staabm commented Nov 17, 2023

if you need additional configuration for reproduction, it would be great if you could provide a small repro repository

@ondrejmirtes
Copy link
Member

Repository is not needed, this is a known behaviour.

@ondrejmirtes
Copy link
Member

This is a duplicate of: #5001

You can solve it with https://github.com/shipmonk-rnd/phpstan-rules#forbidcheckedexceptionincallable.

@amenophis
Copy link
Author

@ondrejmirtes Thank you for your quick reply and for the workaround 🙏

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants