mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Fix unaryplus/minus type inference
This commit is contained in:
parent
4fa0055092
commit
f03807e7b7
@ -121,17 +121,28 @@ class ExpressionChecker
|
||||
$stmt->inferredType = Type::getInt();
|
||||
} elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
|
||||
$stmt->inferredType = Type::getFloat();
|
||||
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus) {
|
||||
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
||||
return false;
|
||||
}
|
||||
$stmt->inferredType = isset($stmt->expr->inferredType) ? $stmt->expr->inferredType : null;
|
||||
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
|
||||
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus ||
|
||||
$stmt instanceof PhpParser\Node\Expr\UnaryPlus
|
||||
) {
|
||||
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmt->inferredType = isset($stmt->expr->inferredType) ? $stmt->expr->inferredType : null;
|
||||
if (!isset($stmt->expr->inferredType)) {
|
||||
$stmt->inferredType = new Type\Union([new TInt, new TFloat]);
|
||||
} else {
|
||||
if ($stmt->expr->inferredType->hasInt() || $stmt->expr->inferredType->hasFloat()) {
|
||||
if (!$stmt->expr->inferredType->hasFloat()) {
|
||||
$stmt->inferredType = Type::getInt();
|
||||
} elseif (!$stmt->expr->inferredType->hasInt()) {
|
||||
$stmt->inferredType = Type::getFloat();
|
||||
} else {
|
||||
$stmt->inferredType = new Type\Union([new TInt, new TFloat]);
|
||||
}
|
||||
} else {
|
||||
$stmt->inferredType = new Type\Union([new TInt, new TFloat]);
|
||||
}
|
||||
}
|
||||
} elseif ($stmt instanceof PhpParser\Node\Expr\Isset_) {
|
||||
self::analyzeIsset($statements_checker, $stmt, $context);
|
||||
$stmt->inferredType = Type::getBool();
|
||||
|
@ -1924,4 +1924,20 @@ class TypeTest extends PHPUnit_Framework_TestCase
|
||||
$context = new Context();
|
||||
$file_checker->visitAndAnalyzeMethods($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testIsIntOnUnaryPlus()
|
||||
{
|
||||
$stmts = self::$parser->parse('<?php
|
||||
$a = +"5";
|
||||
if (!is_int($a)) {
|
||||
}
|
||||
');
|
||||
|
||||
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
||||
$context = new Context();
|
||||
$file_checker->visitAndAnalyzeMethods($context);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user