-
Notifications
You must be signed in to change notification settings - Fork 668
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
$this
cannot be referenced in immutable enums?
#7629
Comments
I found these snippets: https://psalm.dev/r/83ff12ab7a<?php
/** @psalm-immutable */
enum MyEnum
{
case FOO;
case BAR;
/** @psalm-pure */
public function someSwitch(): int
{
return match ($this) {
self::FOO => 1,
self::BAR => 2,
};
}
}
|
I think I remember an discussion about this where @weirdan told that an immutable class can contain a mutable object (as a property) So if this is the starting point, the mutable object in the class could change between calls, making the function potentially impure by virtue of receiving something that might change: https://psalm.dev/r/ac96697cf2 I may be wrong on that though However, not sure how exactly we could abuse your example with an enum. I didn't tweak with that enough |
I found these snippets: https://psalm.dev/r/ac96697cf2<?php
/** @psalm-immutable */
class A{
public function __construct(public stdClass $b){}
}
/**
* @psalm-pure
*/
function pure(A $a): stdClass{
return $a->b;
}
$a = new A(new stdClass());
$a->b->test = 12;
echo pure($a)->test;
$a->b->test = 15;
echo pure($a)->test;
|
Uhhh, that seems extremely wrong upfront :| Perhaps 5.0 should make this a bit more "safe", and prevent non- |
I couldn't find the discussion so maybe I imagined it... :p Found that though, which would seem to contradict the current behaviour: #4126 (comment) |
Seems wrong, since Perhaps a migration to a new annotation, at this point? |
Discussed in #6881 |
I'm in favor of I actually wouldn't mind allowing the tag on class consts as well, I'm not really a fan of PHP's decision to allow mutable objects as consts, but tagging the const as immutable would allow users to use const objects while still requiring immutability. On the other hand, maybe it's not really worth it since you could just mark the class you're assigning the const to as immutable-strict and call it good. Now I'm thinking about adding a config option to require const objects to be immutable-strict. |
Isn't the problem in the given example the |
I found these snippets: https://psalm.dev/r/24faca0cfb<?php
/**
* @psalm-immutable
*/
enum MyEnum
{
case FOO;
case BAR;
public function someSwitch(): int
{
echo 'foo';
return match ($this) {
self::FOO => 1,
self::BAR => 2,
};
}
}
|
@Baptouuuu if the |
This seems to be another variation of Annotating the class as |
I found these snippets: https://psalm.dev/r/07285c1b10<?php
/** @psalm-immutable */
enum MyEnum
{
case FOO;
case BAR;
public function someSwitch(): int
{
return match ($this) {
self::FOO => 1,
self::BAR => 2,
};
}
}
|
Perhaps a misunderstanding question, but https://psalm.dev/r/83ff12ab7a should probably work:
Shouldn't an immutable object be allowed in a pure function? 🤔
Related: #7438
/cc @Lucian-Olariu @mitzaalex
The text was updated successfully, but these errors were encountered: