1
0
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:
Matthew Brown 2017-10-07 11:27:54 -04:00
parent 483b4c75f3
commit 9774131876
3 changed files with 33 additions and 1 deletions

View File

@ -101,6 +101,7 @@
<xs:element name="InvalidArgument" type="IssueHandlerType" minOccurs="0" /> <xs:element name="InvalidArgument" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidArrayAccess" type="IssueHandlerType" minOccurs="0" /> <xs:element name="InvalidArrayAccess" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidArrayAssignment" 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="InvalidClass" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidClone" type="IssueHandlerType" minOccurs="0" /> <xs:element name="InvalidClone" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidParamDefault" type="IssueHandlerType" minOccurs="0" /> <xs:element name="InvalidParamDefault" type="IssueHandlerType" minOccurs="0" />

View File

@ -19,6 +19,7 @@ use Psalm\CodeLocation;
use Psalm\Config; use Psalm\Config;
use Psalm\Context; use Psalm\Context;
use Psalm\Issue\ForbiddenCode; use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\InvalidCast;
use Psalm\Issue\InvalidClone; use Psalm\Issue\InvalidClone;
use Psalm\Issue\InvalidOperand; use Psalm\Issue\InvalidOperand;
use Psalm\Issue\InvalidScope; use Psalm\Issue\InvalidScope;
@ -312,7 +313,31 @@ class ExpressionChecker
return false; 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_) { } elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
if (self::analyze($statements_checker, $stmt->expr, $context) === false) { if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
return false; return false;

View File

@ -66,6 +66,12 @@ class ToStringTest extends TestCase
echo (new A);', echo (new A);',
'error_message' => 'InvalidArgument', 'error_message' => 'InvalidArgument',
], ],
'echoCastClass' => [
'<?php
class A {}
echo (string)(new A);',
'error_message' => 'InvalidCast',
],
'invalidToStringReturnType' => [ 'invalidToStringReturnType' => [
'<?php '<?php
class A { class A {