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

UnusedFunctionCall reported when using unserialize or serialize #8646

Closed
VincentLanglet opened this issue Nov 1, 2022 · 3 comments · Fixed by #8650
Closed

UnusedFunctionCall reported when using unserialize or serialize #8646

VincentLanglet opened this issue Nov 1, 2022 · 3 comments · Fixed by #8650
Labels
easy problems Issues that can be fixed without background knowledge of Psalm Help wanted internal stubs/callmap

Comments

@VincentLanglet
Copy link
Contributor

serialize and unserialize can throw exceptions.

I got the issue with a unit test

public function testCannotSerialize(): void
    {
        $this->expectException(BadMethodCallException::class);
        serialize(new Foo());
    }

but it can be reproduce this way https://psalm.dev/r/c501b39d6d
so psalm shouldn't report an error.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/c501b39d6d
<?php

class Foo
{
    public function __sleep(): array
    {
        throw new BadMethodCallException(sprintf('Cannot serialize %s.', self::class));
    }
    public function __wakeup(): void
    {
        throw new BadMethodCallException(sprintf('Cannot unserialize %s.', self::class));
    }
}

function test(): bool {
    try {
        serialize(new Foo());
    } catch (\Throwable) {
        return false;
    }
    
    return true;
}
Psalm output (using commit 7c83878):

ERROR: UnusedFunctionCall - 17:9 - The call to serialize is not used

@orklah
Copy link
Collaborator

orklah commented Nov 2, 2022

serialize is considered pure. This is very questionnable, as serialize will possibly call a bunch of code that can't be determined.

Given it's pure and we don't have a specific stub that documents the function can throw, we consider it useless.

So 2 ways to fix: changing serialize to impure here:

$impure_functions = [

or adding a stubs in CoreGenericFunctions to document the throw

@orklah orklah added easy problems Issues that can be fixed without background knowledge of Psalm internal stubs/callmap Help wanted labels Nov 2, 2022
@VincentLanglet
Copy link
Contributor Author

I tried #8650 @orklah.
The idea was to still mark serialize as pure if we're not passing an object, because I have a psalm error with the code base (see first commit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy problems Issues that can be fixed without background knowledge of Psalm Help wanted internal stubs/callmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants