Skip to content

Commit

Permalink
@Property annotations: allow *not* implying @psalm-seal-properties
Browse files Browse the repository at this point in the history
Add a setting that allows usage of `@property` to *augment* classes that
use __get() and __set(). Previously, using `@property` once would force
you to exhaustively list all possible properties. This didn't use to be
the case, but was changed in df33405

This was really unexpected for our team and for a while we thought it
was a psalm bug until I found the above commit.

We are using `__get()` for ORM objects and we want to use `@property` to
explicitly document some of columns without being forced to document
every column.
  • Loading branch information
danielbeardsley committed Mar 25, 2023
1 parent ef9a06c commit d5e464d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/running_psalm/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ Whether or not to use types as defined in docblocks. Defaults to `true`.
```
If not using all docblock types, you can still use docblock property types. Defaults to `false` (though only relevant if `useDocblockTypes` is `false`).

#### docblockPropertyTypesSealProperties

```xml
<psalm
docblockPropertyTypesSealProperties="[bool]"
>
```
Whether using @property in class docblocks should imply @psalm-seal-properties

#### usePhpDocMethodsWithoutMagicCall

```xml
Expand Down
13 changes: 13 additions & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ class Config
*/
public $use_docblock_property_types = false;

/**
* Whether using @property in docblocks should implicitly seal properties
*
* @var bool
*/
public $docblock_property_types_seal_properties = true;

/**
* Whether or not to throw an exception on first error
*
Expand Down Expand Up @@ -359,6 +366,11 @@ class Config
*/
public $seal_all_properties = false;

/**
* @var bool
*/
public $seal_all_properties = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -1049,6 +1061,7 @@ private static function fromXmlAndPaths(
$booleanAttributes = [
'useDocblockTypes' => 'use_docblock_types',
'useDocblockPropertyTypes' => 'use_docblock_property_types',
'docblockPropertyTypesSealProperties' => 'docblock_property_types_seal_properties',
'throwExceptionOnError' => 'throw_exception',
'hideExternalErrors' => 'hide_external_errors',
'hideAllErrorsExceptPassedFiles' => 'hide_all_errors_except_passed_files',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,9 @@ public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool
}
}

$storage->sealed_properties = true;
if ($config->docblock_property_types_seal_properties) {
$storage->sealed_properties = true;
}
}

foreach ($docblock_info->methods as $method) {
Expand Down

0 comments on commit d5e464d

Please sign in to comment.