1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Emit more specific operand issues

This commit is contained in:
Matt Brown 2017-09-20 11:22:06 -04:00
parent e42e590442
commit 6f911c7386
2 changed files with 62 additions and 8 deletions

View File

@ -505,6 +505,14 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc
$property = $property_class_storage->properties[$property_name];
if ($property->has_default || $property->is_static || !$property->type) {
continue;
}
if ($property->type->isMixed() || $property->type->isNullable()) {
continue;
}
$constructor_class_storage = null;
if (isset($storage->methods['__construct'])) {
@ -526,14 +534,6 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc
continue;
}
if ($property->has_default || $property->is_static || !$property->type) {
continue;
}
if ($property->type->isMixed() || $property->type->isNullable()) {
continue;
}
$uninitialized_variables[] = '$this->' . $property_name;
$uninitialized_properties[$property_name] = $property;
}

View File

@ -39,6 +39,7 @@ use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TString;
@ -1186,8 +1187,61 @@ class ExpressionChecker
}
if ($left_type && $right_type) {
if ($left_type->isNullable()) {
if (IssueBuffer::accepts(
new PossiblyNullOperand(
'Left operand cannot be nullable, got ' . $left_type,
new CodeLocation($statements_source, $left)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
} elseif ($left_type->isNull()) {
if (IssueBuffer::accepts(
new NullOperand(
'Left operand cannot be null',
new CodeLocation($statements_source, $left)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
return;
}
if ($right_type->isNullable()) {
if (IssueBuffer::accepts(
new PossiblyNullOperand(
'Left operand cannot be nullable, got ' . $right_type,
new CodeLocation($statements_source, $right)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
} elseif ($right_type->isNull()) {
if (IssueBuffer::accepts(
new NullOperand(
'Left operand cannot be null',
new CodeLocation($statements_source, $right)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
return;
}
foreach ($left_type->types as $left_type_part) {
foreach ($right_type->types as $right_type_part) {
if ($left_type_part instanceof TNull) {
// null case is handled above
continue;
}
if ($left_type_part instanceof TMixed || $right_type_part instanceof TMixed) {
if ($left_type_part instanceof TMixed) {
if (IssueBuffer::accepts(