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

Add NullOperand issue for easy ignoring

This commit is contained in:
Matthew Brown 2016-12-29 00:32:12 -05:00
parent 18e7c65430
commit 641ffc09c9
7 changed files with 47 additions and 13 deletions

View File

@ -12,6 +12,8 @@
</inspectFiles>
<issueHandler>
<NullOperand errorLevel="suppress" />
<MissingReturnType>
<excludeFiles>
<directory name="tests" />

View File

@ -565,7 +565,7 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc
$method_checker = self::$method_checkers[$method_id];
}
if (!$stmt->isAbstract()) {
if (!$stmt->isAbstract() && $class_context->self) {
MethodChecker::setDeclaringMethodId(
$class_context->self . '::' . $this->getMappedMethodName(strtolower($stmt->name)),
$method_id

View File

@ -169,7 +169,7 @@ class ForeachChecker
}
}
if ($stmt->keyVar && $stmt->keyVar instanceof PhpParser\Node\Expr\Variable) {
if ($stmt->keyVar && $stmt->keyVar instanceof PhpParser\Node\Expr\Variable && is_string($stmt->keyVar->name)) {
$foreach_context->vars_in_scope['$' . $stmt->keyVar->name] = $key_type ?: Type::getMixed();
$foreach_context->vars_possibly_in_scope['$' . $stmt->keyVar->name] = true;
$statements_checker->registerVariable('$' . $stmt->keyVar->name, $stmt->getLine());

View File

@ -525,7 +525,7 @@ class AssignmentChecker
if ($class_property_type === false) {
if (IssueBuffer::accepts(
new MissingPropertyType(
'Property ' . $lhs_type_part->value . '::$' . $stmt->name . ' does not have a declared ' .
'Property ' . $lhs_type_part->value . '::$' . $prop_name . ' does not have a declared ' .
'type',
new CodeLocation($statements_checker->getSource(), $stmt)
),
@ -650,6 +650,10 @@ class AssignmentChecker
$prop_name = $stmt->name;
if (!is_string($prop_name)) {
return;
}
if (!isset($class_properties[$prop_name])) {
$all_class_properties = null;

View File

@ -21,6 +21,7 @@ use Psalm\Issue\InvalidOperand;
use Psalm\Issue\InvalidScope;
use Psalm\Issue\InvalidStaticVariable;
use Psalm\Issue\MixedOperand;
use Psalm\Issue\NullOperand;
use Psalm\Issue\PossiblyUndefinedVariable;
use Psalm\Issue\UndefinedVariable;
use Psalm\Issue\UnrecognizedExpression;
@ -1071,28 +1072,48 @@ class ExpressionChecker
return;
}
if ($left_type->isNullable()) {
if (IssueBuffer::accepts(
new NullOperand(
'Cannot concatenate with a ' . $left_type,
new CodeLocation($statements_checker->getSource(), $left)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
}
if ($right_type->isNullable()) {
if (IssueBuffer::accepts(
new NullOperand(
'Cannot concatenate with a ' . $right_type,
new CodeLocation($statements_checker->getSource(), $right)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
}
$left_type_match = TypeChecker::isContainedBy(
$left_type,
Type::getString(),
false,
$left_has_scalar_match,
$left_type_coerced,
$left_to_string_cast
true,
$left_has_scalar_match
);
$right_type_match = TypeChecker::isContainedBy(
$right_type,
Type::getString(),
false,
$right_has_scalar_match,
$right_type_coerced,
$right_to_string_cast
true,
$right_has_scalar_match
);
if (!$left_type_match && (!$left_has_scalar_match || $config->strict_binary_operands)) {
if (IssueBuffer::accepts(
new InvalidOperand(
'Cannot concatenate a string and a ' . $left_type,
'Cannot concatenate with a ' . $left_type,
new CodeLocation($statements_checker->getSource(), $left)
),
$statements_checker->getSuppressedIssues()
@ -1104,7 +1125,7 @@ class ExpressionChecker
if (!$right_type_match && (!$right_has_scalar_match || $config->strict_binary_operands)) {
if (IssueBuffer::accepts(
new InvalidOperand(
'Cannot concatenate a string and a ' . $right_type,
'Cannot concatenate with a ' . $right_type,
new CodeLocation($statements_checker->getSource(), $right)
),
$statements_checker->getSuppressedIssues()

View File

@ -288,6 +288,7 @@ class TypeChecker
*/
public static function getTruthsFromFormula(array $clauses)
{
/** @var array<string, string> */
$truths = [];
if (empty($clauses)) {

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class NullOperand extends CodeError
{
}