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

Object shapes in assertions do not work anymore #8981

Closed
boesing opened this issue Dec 22, 2022 · 2 comments · Fixed by #9656
Closed

Object shapes in assertions do not work anymore #8981

boesing opened this issue Dec 22, 2022 · 2 comments · Fixed by #9656
Labels

Comments

@boesing
Copy link
Contributor

boesing commented Dec 22, 2022

Hey there,

I've recently upgrade psalm to v5 in another project where we use object shapes to determine properties from json_decode objects. So we have an assertion method which then should tell psalm that there are properties with specific types.

This does not properly seem to work, at least in my example here:
https://psalm.dev/r/eb1bed0bf9

Is that something which is related to psalm v5 or am I oversee something?

@psalm-github-bot
Copy link

I found these snippets:

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

final class Foo
{
    public const STATUS_OK = 'ok';
    public const STATUS_FAIL = 'fail';
}

$foo = new stdClass();

/** @psalm-assert object{status: Foo::STATUS_*} $bar */
function assertObjectShape(object $bar): void {
}

assertObjectShape($foo);
$status = $foo->status;
/** @psalm-trace $status */
Psalm output (using commit 390da64):

INFO: UnusedParam - 12:35 - Param $bar is never referenced in this method

ERROR: RedundantCondition - 15:1 - Type stdClass for $foo is always object{status:Foo::STATUS_*}

INFO: MixedAssignment - 16:1 - Unable to determine the type that $status is being assigned to

INFO: Trace - 17:28 - $status: mixed

INFO: UnusedVariable - 16:1 - $status is never referenced or the value is not used

@danog danog added the bug label Dec 22, 2022
@danog
Copy link
Collaborator

danog commented Dec 22, 2022

While this is still technically a bug, I recommend using valinor DTOs, instead.

@boesing boesing changed the title Object shape does not work anymore Object shapes in assertions do not work anymore Apr 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants