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

Disable ignoreInternalFunction(False|Null)Return by default #10211

Conversation

robchett
Copy link
Contributor

as per #9764

Breaking change for Psalm 6?

@robchett robchett changed the base branch from 5.x to master September 17, 2023 11:50
@orklah orklah added the release:feature The PR will be included in 'Features' section of the release notes label Sep 28, 2023
@orklah
Copy link
Collaborator

orklah commented Sep 28, 2023

Cool!

Seems like it makes a lot of tests fail

Could you look if those are legit failures (because Psalm didn't check possible false/null values where they could happen?). If so, I'd suggest re-enabling the config for Psalm instead of trying to fix every case

@robchett robchett force-pushed the disable_ignoreInternalFunctionFalseReturn_by_default branch from 23ddfd4 to 4371ee6 Compare October 6, 2023 09:48
@robchett
Copy link
Contributor Author

robchett commented Oct 6, 2023

@orklah
I've added a new config for specifying which functions to ignore null/false on, which allowed me to find out what functions were not being handled correctly.

<ignoredInternalFunctionFalseReturn>
        <referencedFunction name="getcwd"/>
        <referencedFunction name="ob_get_clean"/>
        <referencedFunction name="glob"/>
        <referencedFunction name="filemtime"/>
        <referencedFunction name="opendir"/>
        <referencedFunction name="scandir"/>
        <referencedFunction name="readdir"/>
        <referencedFunction name="tempnam"/>
        <referencedFunction name="phpversion"/>
        <referencedFunction name="file_get_contents"/>
        <referencedFunction name="reset"/>
        <referencedFunction name="end"/>
        <referencedFunction name="fwrite"/>
        <referencedFunction name="fread"/>
        <referencedFunction name="realpath"/>
        <referencedFunction name="readlink"/>
        <referencedFunction name="json_encode"/>
        <referencedFunction name="curl_init"/>
        <referencedFunction name="base64_decode"/>
        <referencedFunction name="igbinary_serialize"/>
        <referencedFunction name="strtotime"/>
        <referencedFunction name="preg_split"/>
        <referencedFunction name="ini_get"/>
        <referencedFunction name="getopt"/>
    </ignoredInternalFunctionFalseReturn>

    <ignoredInternalFunctionNullReturn>
        <referencedFunction name="preg_replace"/>
        <referencedFunction name="array_shift"/>
        <referencedFunction name="key"/>
    </ignoredInternalFunctionNullReturn>

Most seem safe, but there are a few file/io ones in there that I think I should remove the suppression for and actually add checks too.

Are you happy with this approach for a fine-tuned list of suppressed functions instead of a blanket suppression on all internal methods?

@robchett robchett force-pushed the disable_ignoreInternalFunctionFalseReturn_by_default branch from eca0969 to af3065b Compare October 6, 2023 14:57
@orklah
Copy link
Collaborator

orklah commented Oct 9, 2023

I'm not sure I understood what you meant

I was kinda expecting that the errors Psalm raised on itself were going to be places where a function can legitimately return false/null but we ignored it (probably because it can rarely happen but still). So the fix for that would have been either to fix each place in the code (pretty tedious) or just baseline those

What I understand is that you added a config so we can restore the old ignore for specific functions. I don't think this config will be used by a lot of users. They'll either don't want to bother with false/null to begin with and ignore everything or they'll want to have the cleanest code and fix what they can and baseline the rest in the meantime.

This seems like the kind of feature for power users that I would prefer to be solved with custom stubs if possible (to avoid cluttering the core too much)

@robchett robchett force-pushed the disable_ignoreInternalFunctionFalseReturn_by_default branch from af3065b to 6a455b2 Compare October 9, 2023 18:01
@robchett
Copy link
Contributor Author

robchett commented Oct 9, 2023

Sure, that's fine.

I'll review/fix the ones that look the most likely to return false and baseline the rest.

glob filemtime opendir scandir readdir tempnam file_get_contents reset end fwrite fread realpath readlink

@robchett robchett force-pushed the disable_ignoreInternalFunctionFalseReturn_by_default branch 2 times, most recently from c41efd8 to 0bd4f9b Compare October 10, 2023 20:33
@jack-worman
Copy link
Contributor

Casting false and null types to string is not the correct solution. This is just masking the issue. If an internal function returns null or false, it probably should throw an exception instead, since most of the time these are errors.

At my work, we use https://github.com/thecodingmachine/safe to handle these issues.

@orklah
Copy link
Collaborator

orklah commented Oct 16, 2023

Casting false and null types to string is not the correct solution

Indeed but every improvement to the codebase is welcome, even when it's incremental. Before this PR, they were completely ignored and now they're explicitly handled (through casting). If this itch someone the wrong way, a future PR could improve the situation :)

@orklah
Copy link
Collaborator

orklah commented Oct 16, 2023

Thanks!

@orklah orklah merged commit 8f9f8d0 into vimeo:master Oct 16, 2023
38 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants