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:
parent
f5ee373244
commit
33be97b1f8
@ -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(
|
||||
|
@ -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
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user