Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [BC] `ArrayObject` and `ArrayIterator` have the class contract `@psalm-capabilities read-props|write-this-props`, and a purity template, `TStorage` (default `write-props`), for what their mutators cost besides writing the object itself: their constructor binds it to `pure` when given an array and to `write-props` when given an object, so a pure function may mutate an `ArrayObject` it created from an array. `exchangeArray()` always needs `write-props`. A class extending them must now declare `@psalm-capabilities read-props|write-this-props` (or fewer) itself, otherwise Psalm reports `ImmutableDependency`, and its methods may not do more (no `io`, `write-props`, …). An override calling a parent mutator can declare `@psalm-purity-template TStorage(write-props) <= write-props`, extend `ArrayObject[TStorage]<TKey, TValue>` and use `@psalm-purity-from-template TStorage`.
- With `rememberPropertyAssignmentsAfterCall="false"`, property refinements now survive `new` and static calls of callees that cannot write properties, and static property refinements survive callees that cannot write globals (previously every `new`/static call, and only callees writing properties, forgot them).
- `@psalm-purity-template` accepts bounds written as a chain, `lower <= Name(default) <= upper`: an upper bound (`P <= write-props`) and, on classes, a lower bound (`write-this-props <= C`, which the methods depending on `C` may use unconditionally) and a default (`C(pure) <= write-props`); `Closure[_](...)` in a parameter's type declares an implicit purity template; `@psalm-capabilities` accepts type aliases (`@psalm-type`, `@psalm-import-type`). An unknown capability name is now reported as `InvalidDocblock` rather than `MissingDocblockType`.
- [BC] A `@psalm-capabilities` or `@psalm-impure` tag now wins over `@pure`, `@phpstan-pure` and `#[JetBrains\PhpStorm\Pure]` on the same function-like, and a class's `@psalm-` purity tag over `#[JetBrains\PhpStorm\Immutable]`; previously the generic tag or attribute made it `pure` (the class at most `read-props`) whatever the `@psalm-` tag said.
- PHP 8.6 partial function application is supported. Plugins see `f(2, ?)` as a `Psalm\Node\Expr\PartialApplication` node holding `f(...)` (`$call`) and the arguments as written (`$args`), never as a call with a `?` placeholder, so provider events only meet its first-class callable. `getCallArgs()` of `FunctionReturnTypeProviderEvent`, `MethodReturnTypeProviderEvent` and `MethodCapabilitiesProviderEvent`, and `getArgs()` of `DynamicFunctionStorageProviderEvent`, return `[]` for a first-class callable `f(...)`.
- Psalm requires `psalm/plugin-phpunit` `^0.21 || ^0.22 || ^1.0` (0.21 is the first release built on `psalm/psalm-plugin-api` 0.4): a project requiring an older version (`^0.19`, `^0.20`) must raise it.
- [BC] The refined return types of `count`, `sizeof`, `fgetcsv`, `get_called_class`, `get_parent_class`, `hrtime` and `mb_strtolower` now come from built-in return type providers, which, like those of other builtins, take precedence over plugin return type providers and over the return type of a stub declaring the function. A plugin or stub redefining the return type of one of these few functions is no longer used when Psalm infers one; Psalm's own types for them are usually more precise.
Expand Down
23 changes: 23 additions & 0 deletions docs/annotating_code/purity_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ Every purity annotation is a name for a capability set:
`@pure`, `@phpstan-pure`, `#[Psalm\Pure]` and `#[JetBrains\PhpStorm\Pure]` mean the same as
`@psalm-pure`; `#[Psalm\Immutable]` and `#[JetBrains\PhpStorm\Immutable]` the same as
`@psalm-immutable`; `#[Psalm\ExternalMutationFree]` the same as `@psalm-external-mutation-free`.
A `@psalm-capabilities` or `@psalm-impure` tag wins over `@pure`, `@phpstan-pure` and
`#[JetBrains\PhpStorm\Pure]` on the same function or method (and a class's purity tag over
`#[JetBrains\PhpStorm\Immutable]`), so a library can declare `pure` for other tools and
`read-props` for Psalm:

```php
<?php
interface Message {
/**
* @pure
* @psalm-capabilities read-props
*/
public function body(): string;
}

final class MyMessage implements Message {
public function __construct(private string $body) {}

public function body(): string {
return $this->body; // no issue
}
}
```

### Capability aliases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,8 @@ public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool
}
}

$docblock_has_mutations_annotation = $storage->has_mutations_annotation;

foreach ($node->attrGroups as $attr_group) {
foreach ($attr_group->attrs as $attr) {
$attribute = AttributeResolver::resolve(
Expand Down Expand Up @@ -1143,8 +1145,10 @@ public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool
$storage->internal = [NamespaceAnalyzer::getNameSpaceRoot($fq_classlike_name)];
}

// the PhpStorm attribute gives way to a purity tag of the docblock
if ($attribute->fq_class_name === 'Psalm\\Immutable'
|| $attribute->fq_class_name === 'JetBrains\\PhpStorm\\Immutable'
|| ($attribute->fq_class_name === 'JetBrains\\PhpStorm\\Immutable'
&& !$docblock_has_mutations_annotation)
) {
$storage->capabilities = Capabilities::MUTATION_FREE & $storage->capabilities;
$storage->has_mutations_annotation = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ public static function parse(
);

$info->variadic = isset($parsed_docblock->tags['psalm-variadic']);
if (isset($parsed_docblock->tags['psalm-pure'])
|| isset($parsed_docblock->tags['phpstan-pure'])
|| isset($parsed_docblock->tags['pure'])
) {
if (isset($parsed_docblock->tags['psalm-pure'])) {
$info->capabilities = Capabilities::NONE;
$info->has_mutations_annotation = true;
} elseif (isset($parsed_docblock->tags['psalm-mutation-free'])) {
Expand All @@ -360,6 +357,9 @@ public static function parse(
$info->capabilities_expressions[] = trim($capabilities_line);
}
}
} elseif (isset($parsed_docblock->tags['phpstan-pure']) || isset($parsed_docblock->tags['pure'])) {
$info->capabilities = Capabilities::NONE;
$info->has_mutations_annotation = true;
}

if (isset($parsed_docblock->tags['psalm-purity-from-template'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ public function start(
}
}

$docblock_has_mutations_annotation = $storage->has_mutations_annotation;

foreach ($stmt->getAttrGroups() as $attr_group) {
foreach ($attr_group->attrs as $attr) {
$attribute = AttributeResolver::resolve(
Expand All @@ -699,8 +701,10 @@ public function start(
$this->classlike_storage->name ?? null,
);

// like `@pure`, the PhpStorm attribute gives way to a purity tag of the docblock
if ($attribute->fq_class_name === 'Psalm\\Pure'
|| $attribute->fq_class_name === 'JetBrains\\PhpStorm\\Pure'
|| ($attribute->fq_class_name === 'JetBrains\\PhpStorm\\Pure'
&& !$docblock_has_mutations_annotation)
) {
$storage->specialize_call = true;
$storage->capabilities = Capabilities::NONE;
Expand Down
Loading
Loading