psalm: Immutability/purity is not enforced in inheritance/contracts
Following example (https://psalm.dev/r/e5b578def2) is a clear violation of purity rules:
<?php
/** @psalm-immutable */
interface SomethingImmutable {
public function someInteger() : int;
}
class MutableImplementation implements SomethingImmutable {
private int $counter = 0;
public function someInteger() : int {
return ++$this->counter;
}
}
/** @psalm-pure */
function useSomething(SomethingImmutable $something) : int
{
return $something->someInteger();
}
$instance = new MutableImplementation;
echo useSomething($instance);
echo useSomething($instance);
Psalm does not find errors, but it should probably implicitly propagate @psalm-immutable
to MutableImplementation
, and flag it as an error.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 16 (9 by maintainers)
Commits related to this issue
- Fix #2138 - complain when inheriting from immutable interfaces without annotation — committed to 2e3s/psalm by muglug 5 years ago
Probably it should be limited to methods covered by
SomethingImmutable
only.