1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Check for to-string casts in strict operands mode

This commit is contained in:
Matthew Brown 2018-03-18 14:42:23 -04:00
parent f5ee373244
commit 33be97b1f8
2 changed files with 49 additions and 2 deletions

View File

@ -11,6 +11,7 @@ use Psalm\Checker\TypeChecker;
use Psalm\CodeLocation;
use Psalm\Config;
use Psalm\Context;
use Psalm\Issue\ImplicitToStringCast;
use Psalm\Issue\InvalidOperand;
use Psalm\Issue\MixedOperand;
use Psalm\Issue\NullOperand;
@ -916,7 +917,10 @@ class BinaryOpChecker
Type::getString(),
true,
false,
$left_has_scalar_match
$left_has_scalar_match,
$left_type_coerced,
$left_type_coerced_from_mixed,
$left_type_to_string_cast
);
$right_type_match = TypeChecker::isContainedBy(
@ -925,9 +929,38 @@ class BinaryOpChecker
Type::getString(),
true,
false,
$right_has_scalar_match
$right_has_scalar_match,
$right_type_coerced,
$right_type_coerced_from_mixed,
$right_type_to_string_cast
);
if ($left_type_to_string_cast && $config->strict_binary_operands) {
if (IssueBuffer::accepts(
new ImplicitToStringCast(
'Left side of concat op expects string, '
. '\'' . $left_type . '\' provided with a __toString method',
new CodeLocation($statements_checker->getSource(), $left)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
}
if ($right_type_to_string_cast && $config->strict_binary_operands) {
if (IssueBuffer::accepts(
new ImplicitToStringCast(
'Right side of concat op expects string, '
. '\'' . $right_type . '\' provided with a __toString method',
new CodeLocation($statements_checker->getSource(), $right)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
}
if (!$left_type_match && (!$left_has_scalar_match || $config->strict_binary_operands)) {
if (IssueBuffer::accepts(
new InvalidOperand(

View File

@ -118,6 +118,20 @@ class ToStringTest extends TestCase
}',
'error_message' => 'ImplicitToStringCast',
],
'implicitConcatenation' => [
'<?php
interface I {
public function __toString();
}
function takesI(I $i): void
{
$a = $i . "hello";
}',
'error_message' => 'ImplicitToStringCast',
[],
true
],
];
}
}