mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Fix #224 - explicit string casts fail when no __toString present
This commit is contained in:
parent
483b4c75f3
commit
9774131876
@ -101,6 +101,7 @@
|
||||
<xs:element name="InvalidArgument" type="IssueHandlerType" minOccurs="0" />
|
||||
<xs:element name="InvalidArrayAccess" type="IssueHandlerType" minOccurs="0" />
|
||||
<xs:element name="InvalidArrayAssignment" type="IssueHandlerType" minOccurs="0" />
|
||||
<xs:element name="InvalidCast" type="IssueHandlerType" minOccurs="0" />
|
||||
<xs:element name="InvalidClass" type="IssueHandlerType" minOccurs="0" />
|
||||
<xs:element name="InvalidClone" type="IssueHandlerType" minOccurs="0" />
|
||||
<xs:element name="InvalidParamDefault" type="IssueHandlerType" minOccurs="0" />
|
||||
|
@ -19,6 +19,7 @@ use Psalm\CodeLocation;
|
||||
use Psalm\Config;
|
||||
use Psalm\Context;
|
||||
use Psalm\Issue\ForbiddenCode;
|
||||
use Psalm\Issue\InvalidCast;
|
||||
use Psalm\Issue\InvalidClone;
|
||||
use Psalm\Issue\InvalidOperand;
|
||||
use Psalm\Issue\InvalidScope;
|
||||
@ -312,7 +313,31 @@ class ExpressionChecker
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmt->inferredType = Type::getString();
|
||||
$container_type = Type::getString();
|
||||
|
||||
if ($stmt->expr->inferredType
|
||||
&& !$stmt->expr->inferredType->isMixed()
|
||||
&& !TypeChecker::isContainedBy(
|
||||
$statements_checker->getFileChecker()->project_checker,
|
||||
$stmt->expr->inferredType,
|
||||
$container_type,
|
||||
true,
|
||||
$has_scalar_match
|
||||
)
|
||||
&& !$has_scalar_match
|
||||
) {
|
||||
if (IssueBuffer::accepts(
|
||||
new InvalidCast(
|
||||
$stmt->expr->inferredType . ' cannot be cast to ' . $container_type,
|
||||
new CodeLocation($statements_checker->getSource(), $stmt)
|
||||
),
|
||||
$statements_checker->getSuppressedIssues()
|
||||
)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$stmt->inferredType = $container_type;
|
||||
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
||||
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
||||
return false;
|
||||
|
@ -66,6 +66,12 @@ class ToStringTest extends TestCase
|
||||
echo (new A);',
|
||||
'error_message' => 'InvalidArgument',
|
||||
],
|
||||
'echoCastClass' => [
|
||||
'<?php
|
||||
class A {}
|
||||
echo (string)(new A);',
|
||||
'error_message' => 'InvalidCast',
|
||||
],
|
||||
'invalidToStringReturnType' => [
|
||||
'<?php
|
||||
class A {
|
||||
|
Loading…
Reference in New Issue
Block a user