Skip to content
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

Add configuration option to disable @var parsing everywhere except for properties. #7434

Merged
merged 1 commit into from Feb 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions config.xsd
Expand Up @@ -109,6 +109,7 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="disableVarParsing" type="xs:boolean" default="false" />
<xs:attribute name="errorLevel" type="xs:integer" default="2" />
<xs:attribute name="reportMixedIssues" type="xs:boolean" default="true" />
<xs:attribute name="useDocblockTypes" type="xs:boolean" default="true" />
Expand Down
10 changes: 10 additions & 0 deletions docs/running_psalm/configuration.md
Expand Up @@ -116,6 +116,16 @@ The PHPDoc `@method` annotation normally only applies to classes with a `__call`
```
The PHPDoc `@property`, `@property-read` and `@property-write` annotations normally only apply to classes with `__get`/`__set` methods. Setting this to `true` allows you to use the `@property`, `@property-read` and `@property-write` annotations to override property existence checks and resulting property types. Defaults to `false`.

#### disableVarParsing

```xml
<psalm
disableVarParsing="[bool]"
/>
```

Disables parsing of `@var` PHPDocs everywhere except for properties. Setting this to `true` can remove many false positives due to outdated `@var` annotations, used before integrations of Psalm generics and proper typing, enforcing Single Source Of Truth principles. Defaults to `false`.

#### strictBinaryOperands

```xml
Expand Down
6 changes: 6 additions & 0 deletions src/Psalm/Config.php
Expand Up @@ -373,6 +373,11 @@ class Config
*/
public $add_param_default_to_docblock_type = false;

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

/**
* @var bool
*/
Expand Down Expand Up @@ -920,6 +925,7 @@ private static function fromXmlAndPaths(
'allowFileIncludes' => 'allow_includes',
'strictBinaryOperands' => 'strict_binary_operands',
'rememberPropertyAssignmentsAfterCall' => 'remember_property_assignments_after_call',
'disableVarParsing' => 'disable_var_parsing',
'allowPhpStormGenerics' => 'allow_phpstorm_generics',
'allowStringToStandInForClass' => 'allow_string_standin_for_class',
'disableSuppressAll' => 'disable_suppress_all',
Expand Down
Expand Up @@ -155,13 +155,15 @@ public static function analyze(
$template_type_map = $statements_analyzer->getTemplateTypeMap();

try {
$var_comments = CommentAnalyzer::getTypeFromComment(
$doc_comment,
$statements_analyzer->getSource(),
$statements_analyzer->getAliases(),
$template_type_map,
$file_storage->type_aliases
);
$var_comments = $codebase->config->disable_var_parsing
? []
: CommentAnalyzer::getTypeFromComment(
$doc_comment,
$statements_analyzer->getSource(),
$statements_analyzer->getAliases(),
$template_type_map,
$file_storage->type_aliases
);
} catch (IncorrectDocblockException $e) {
IssueBuffer::maybeAdd(
new MissingDocblockType(
Expand Down
18 changes: 10 additions & 8 deletions src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php
Expand Up @@ -74,14 +74,16 @@ public static function analyze(
$file_storage = $file_storage_provider->get($statements_analyzer->getFilePath());

try {
$var_comments = CommentAnalyzer::arrayToDocblocks(
$doc_comment,
$parsed_docblock,
$statements_analyzer->getSource(),
$statements_analyzer->getAliases(),
$statements_analyzer->getTemplateTypeMap(),
$file_storage->type_aliases
);
$var_comments = $codebase->config->disable_var_parsing
? []
: CommentAnalyzer::arrayToDocblocks(
$doc_comment,
$parsed_docblock,
$statements_analyzer->getSource(),
$statements_analyzer->getAliases(),
$statements_analyzer->getTemplateTypeMap(),
$file_storage->type_aliases
);
} catch (DocblockParseException $e) {
IssueBuffer::maybeAdd(
new InvalidDocblock(
Expand Down
16 changes: 9 additions & 7 deletions src/Psalm/Internal/Analyzer/Statements/StaticAnalyzer.php
Expand Up @@ -57,13 +57,15 @@ public static function analyze(
$var_comments = [];

try {
$var_comments = CommentAnalyzer::arrayToDocblocks(
$doc_comment,
$parsed_docblock,
$statements_analyzer->getSource(),
$statements_analyzer->getSource()->getAliases(),
$statements_analyzer->getSource()->getTemplateTypeMap()
);
$var_comments = $codebase->config->disable_var_parsing
? []
: CommentAnalyzer::arrayToDocblocks(
$doc_comment,
$parsed_docblock,
$statements_analyzer->getSource(),
$statements_analyzer->getSource()->getAliases(),
$statements_analyzer->getSource()->getTemplateTypeMap()
);
} catch (IncorrectDocblockException $e) {
IssueBuffer::maybeAdd(
new MissingDocblockType(
Expand Down
18 changes: 10 additions & 8 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Expand Up @@ -442,14 +442,16 @@ private static function analyzeStatement(
$var_comments = [];

try {
$var_comments = CommentAnalyzer::arrayToDocblocks(
$docblock,
$statements_analyzer->parsed_docblock,
$statements_analyzer->getSource(),
$statements_analyzer->getAliases(),
$template_type_map,
$file_storage->type_aliases
);
$var_comments = $codebase->config->disable_var_parsing
? []
: CommentAnalyzer::arrayToDocblocks(
$docblock,
$statements_analyzer->parsed_docblock,
$statements_analyzer->getSource(),
$statements_analyzer->getAliases(),
$template_type_map,
$file_storage->type_aliases
);
} catch (IncorrectDocblockException $e) {
IssueBuffer::maybeAdd(
new MissingDocblockType(
Expand Down