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
Support @psalm-private-mutate annotation #2729
Comments
Hey @christian-kolb, can you reproduce the issue on https://psalm.dev ? |
Have you read https://psalm.dev/articles/immutability-and-beyond? |
@muglug Yes, but thanks for the reminder (was a while ago). This might be a good example to help me explain what I want.
So basically I can use a public property as getter, not as setter, but modify it through its class methods. As far as I'm aware this behaviour can only made available with getter methods but not with psalm annotations. Basically I want to get rid of getters, because I have many instances where I only have them to get this result. And at the moment this means I have "clean" classes without getters and ones cluttered with them. It would be amazing if I could solve this issue with psalm 🙂 |
I think you're asking for |
@muglug Kind of, but this only applies to methods, doesn't it? I would need it to be applied for classes and properties. I tried with psalm.dev, but it doesn't raise any issues: https://psalm.dev/r/69691fb410 It would describe the behaviour I want 🙂 |
I found these snippets: https://psalm.dev/r/69691fb410<?php
/** @psalm-external-mutation-free */
class Test
{
/** @psalm-external-mutation-free */
public int $count;
public function __construct() {
$this->count = 0;
}
}
function foo() : void {
$test = new Test();
$test->count = 10; // Should raise an issue here
echo $test->count;
}
|
OK, I understand now – if you contribute an implementation and/or documentation, I'll do whatever's left. I suggest a per-property annotation of |
Awesome, I have no idea how I would start an implementation for it. But I'm sure I can write documentation for it. |
With PHP 7.4 using public properties in a type safe way is finally possible. In some languages you have the concept of marking a property to be able to be read from outside the class, but not modified. Basically public get, but private set.
Psalm already supports
@psalm-readonly
which prevents the property by being modified anywhere. It would be awesome to have a similar annotation which allows to modify the property with class methods. With this the public get, private set paradigm would be available to PHP users 🙂Please let me know when there is already concept within psalm with which I could get the same result, I haven't found one yet.
The text was updated successfully, but these errors were encountered: