mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Add documentation for new @psalm-private-set annotation
This commit is contained in:
parent
ba8eb264d4
commit
143756800f
@ -324,6 +324,27 @@ echo Arithmetic::addCumulative(3); // outputs 3
|
||||
echo Arithmetic::addCumulative(3); // outputs 6
|
||||
```
|
||||
|
||||
### `@psalm-private-set`
|
||||
|
||||
Used to annotate properties which can only be set in a private context. With this, public properties can be read from another class but only be modified within a method of its own class.
|
||||
|
||||
```php
|
||||
class Counter {
|
||||
/** @psalm-private-set */
|
||||
public int $count = 0;
|
||||
|
||||
public function increment() : void {
|
||||
$this->count++;
|
||||
}
|
||||
}
|
||||
|
||||
$counter = new Counter();
|
||||
echo $counter->count; // outputs 0
|
||||
$counter->increment(); // Method can modify property
|
||||
echo $counter->count; // outputs 1
|
||||
$counter->count = 5; // This will fail as it's modifying a property directly
|
||||
```
|
||||
|
||||
## Type Syntax
|
||||
|
||||
Psalm supports PHPDoc’s [type syntax](https://docs.phpdoc.org/guides/types.html), and also the [proposed PHPDoc PSR type syntax](https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#appendix-a-types).
|
||||
|
Loading…
Reference in New Issue
Block a user