1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Allow mixed property assignment when the user wants it

Fixes #652
This commit is contained in:
Matthew Brown 2018-04-07 12:13:30 -04:00
parent 3771ea206c
commit 0f0f405b9f
3 changed files with 58 additions and 8 deletions

View File

@ -15,6 +15,7 @@ use Psalm\Issue\ImplicitToStringCast;
use Psalm\Issue\InvalidPropertyAssignment;
use Psalm\Issue\InvalidPropertyAssignmentValue;
use Psalm\Issue\LoopInvalidation;
use Psalm\Issue\MixedAssignment;
use Psalm\Issue\MixedPropertyAssignment;
use Psalm\Issue\MixedTypeCoercion;
use Psalm\Issue\NoInterfaceProperties;
@ -381,6 +382,18 @@ class PropertyAssignmentChecker
$lhs_type_part->value,
$lhs_type_part->value
);
if (!$class_property_type->isMixed() && $assignment_value_type->isMixed()) {
if (IssueBuffer::accepts(
new MixedAssignment(
'Cannot assign ' . $var_id . ' to a mixed type',
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
}
}
$class_property_types[] = $class_property_type;

View File

@ -172,14 +172,16 @@ class AssignmentChecker
if ($assign_value_type->isMixed()) {
$codebase->analyzer->incrementMixedCount($statements_checker->getCheckedFilePath());
if (IssueBuffer::accepts(
new MixedAssignment(
'Cannot assign ' . $var_id . ' to a mixed type',
new CodeLocation($statements_checker->getSource(), $assign_var)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
if (!$assign_var instanceof PhpParser\Node\Expr\PropertyFetch) {
if (IssueBuffer::accepts(
new MixedAssignment(
'Cannot assign ' . $var_id . ' to a mixed type',
new CodeLocation($statements_checker->getSource(), $assign_var)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
}
} else {
$codebase->analyzer->incrementNonMixedCount($statements_checker->getCheckedFilePath());

View File

@ -828,6 +828,23 @@ class PropertyTypeTest extends TestCase
}
}',
],
'allowMixedAssignmetWhenDesired' => [
'<?php
class A {
/**
* @var mixed
*/
private $mixed;
/**
* @param mixed $value
*/
public function setMixed($value): void
{
$this->mixed = $value;
}
}',
],
];
}
@ -1270,6 +1287,24 @@ class PropertyTypeTest extends TestCase
}',
'error_message' => 'InvalidPropertyAssignmentValue',
],
'prohibitMixedAssignmentNormally' => [
'<?php
class A {
/**
* @var string
*/
private $mixed;
/**
* @param mixed $value
*/
public function setMixed($value): void
{
$this->mixed = $value;
}
}',
'error_message' => 'MixedAssignment',
],
];
}
}