Skip to content
Permalink
Browse files

Add a way to ignore variable method & property calls

  • Loading branch information...
muglug committed Apr 20, 2019
1 parent ec65197 commit 9be8013633c22d479f1a8f4cceeb4ee1bc250c4a
@@ -276,6 +276,16 @@ class Context
*/
public $inside_negation = false;
/**
* @var bool
*/
public $ignore_variable_property = false;
/**
* @var bool
*/
public $ignore_variable_method = false;
/**
* @param string|null $self
*/
@@ -132,7 +132,8 @@ public static function parse($docblock, $line_number = null, $preserve_format =
'assert', 'assert-if-true', 'assert-if-false', 'suppress',
'ignore-nullable-return', 'override-property-visibility',
'override-method-visibility', 'seal-properties', 'seal-methods',
'generator-return', 'ignore-falsable-return', 'variadic'
'generator-return', 'ignore-falsable-return', 'variadic',
'ignore-variable-method', 'ignore-variable-property',
]
)) {
throw new DocblockParseException('Unrecognised annotation @psalm-' . $special_key);
@@ -830,7 +830,7 @@ public static function analyzeStatic(
$prop_name = $stmt->name;
if (!$prop_name instanceof PhpParser\Node\Identifier) {
if ($fq_class_name) {
if ($fq_class_name && !$context->ignore_variable_property) {
$codebase->analyzer->addMixedMemberName(strtolower($fq_class_name) . '::$');
}
@@ -566,7 +566,7 @@ public static function analyze(
return false;
}
if (isset($assign_var->var->inferredType)) {
if (isset($assign_var->var->inferredType) && !$context->ignore_variable_property) {
$stmt_var_type = $assign_var->var->inferredType;
if ($stmt_var_type->hasObjectType()) {
@@ -560,7 +560,9 @@ private static function analyzeAtomicCall(
}
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
$codebase->analyzer->addMixedMemberName(strtolower($fq_class_name) . '::');
if (!$context->ignore_variable_method) {
$codebase->analyzer->addMixedMemberName(strtolower($fq_class_name) . '::');
}
$return_type = Type::getMixed();
return true;
@@ -840,7 +840,9 @@ function (Assertion $assertion) use ($found_generic_params) : Assertion {
}
}
} else {
$codebase->analyzer->addMixedMemberName(strtolower($fq_class_name) . '::');
if (!$context->ignore_variable_method) {
$codebase->analyzer->addMixedMemberName(strtolower($fq_class_name) . '::');
}
if (self::checkFunctionArguments(
$statements_analyzer,
@@ -292,7 +292,7 @@ public static function analyzeInstance(
}
if (!$prop_name) {
if ($stmt_var_type->hasObjectType()) {
if ($stmt_var_type->hasObjectType() && !$context->ignore_variable_property) {
foreach ($stmt_var_type->getTypes() as $type) {
if ($type instanceof Type\Atomic\TNamedObject) {
$codebase->analyzer->addMixedMemberName(strtolower($type->value) . '::$');
@@ -172,6 +172,9 @@ public function analyze(
$plugin_classes = $codebase->config->after_statement_checks;
foreach ($stmts as $stmt) {
$ignore_variable_property = false;
$ignore_variable_method = false;
if ($has_returned && !($stmt instanceof PhpParser\Node\Stmt\Nop) &&
!($stmt instanceof PhpParser\Node\Stmt\InlineHTML)
) {
@@ -216,6 +219,7 @@ public function analyze(
$comments = [];
}
if (isset($comments['specials']['psalm-suppress'])) {
$suppressed = array_filter(
array_map(
@@ -237,6 +241,14 @@ function ($line) {
$this->addSuppressedIssues($new_issues);
}
}
if (isset($comments['specials']['psalm-ignore-variable-method'])) {
$context->ignore_variable_method = $ignore_variable_method = true;
}
if (isset($comments['specials']['psalm-ignore-variable-property'])) {
$context->ignore_variable_property = $ignore_variable_property = true;
}
}
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
@@ -716,6 +728,16 @@ function ($line) {
/** @psalm-suppress MixedTypeCoercion */
$this->removeSuppressedIssues($new_issues);
}
if ($ignore_variable_property) {
$context->ignore_variable_property = false;
$ignore_variable_property = false;
}
if ($ignore_variable_method) {
$context->ignore_variable_method = false;
$ignore_variable_method = false;
}
}
if ($root_scope
@@ -1771,6 +1771,29 @@ function foo(A $a, string $var) {
['PossiblyUnusedMethod'],
true,
],
'removePossiblyUnusedMethodWithVariableCall' => [
'<?php
class A {
public function foo() : void {}
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-method */
echo $a->$var();
}',
'<?php
class A {
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-method */
echo $a->$var();
}',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removePossiblyUnusedPropertyWithDocblock' => [
'<?php
class A {
@@ -1850,6 +1873,29 @@ function foo(A $a, string $var) {
['PossiblyUnusedProperty'],
true,
],
'removePossiblyUnusedPropertyWithVariableFetch' => [
'<?php
class A {
public $foo = "hello";
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-property */
echo $a->$var;
}',
'<?php
class A {
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-property */
echo $a->$var;
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableFetchInParent' => [
'<?php
class A {

0 comments on commit 9be8013

Please sign in to comment.
You can’t perform that action at this time.