psalm icon indicating copy to clipboard operation
psalm copied to clipboard

jsonSerialize triggers PossiblyUnusedMethod even when json_encode is called

Open josephwynn-sc opened this issue 10 months ago • 1 comments

https://psalm.dev/r/9a3c68649b

<?php

class JsonSerializableClass implements \JsonSerializable
{
    public function jsonSerialize(): array
    {
        return ["hello"];
    }
}

$x = new JsonSerializableClass();

echo json_encode($x);
echo json_encode([
    "nested" => "structure",
    "x" => $x,
]);

The psalm.dev link shows this as INFO but locally it's an ERROR (see below)

Psalm 5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4

ERROR: PossiblyUnusedMethod - test.php:5:21 - Cannot find any calls to method JsonSerializableClass::jsonSerialize (see https://psalm.dev/087)
    public function jsonSerialize(): array

Using the following config:

<?xml version="1.0"?>
<psalm errorLevel="3" findUnusedBaselineEntry="true" findUnusedCode="true">
    <projectFiles>
        <file name="test.php" />
    </projectFiles>
</psalm>

josephwynn-sc avatar Apr 09 '24 21:04 josephwynn-sc

I found these snippets:

https://psalm.dev/r/9a3c68649b
<?php

class JsonSerializableClass implements \JsonSerializable
{
    public function jsonSerialize(): array
    {
        return ["hello"];
    }
}

$x = new JsonSerializableClass();

echo json_encode($x);
echo json_encode([
    "nested" => "structure",
    "x" => $x,
]);
Psalm output (using commit ef3b018):

INFO: PossiblyUnusedMethod - 5:21 - Cannot find any calls to method JsonSerializableClass::jsonSerialize

psalm-github-bot[bot] avatar Apr 09 '24 21:04 psalm-github-bot[bot]