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

Add support for numeric type

This commit is contained in:
Matthew Brown 2016-07-13 11:18:07 -04:00
parent 00a6a54cf5
commit c7dc5dd9f9
2 changed files with 31 additions and 5 deletions

View File

@ -1672,7 +1672,7 @@ class StatementsChecker
if (!$return_type->isMixed()) { if (!$return_type->isMixed()) {
foreach ($return_type->types as &$type) { foreach ($return_type->types as &$type) {
if ($type->isScalar()) { if ($type->isScalarType()) {
if (IssueBuffer::accepts( if (IssueBuffer::accepts(
new InvalidArrayAssignment('Cannot assign value on variable $' . $var_id . ' of scalar type ' . $type->value, $this->_file_name, $stmt->getLine()) new InvalidArrayAssignment('Cannot assign value on variable $' . $var_id . ' of scalar type ' . $type->value, $this->_file_name, $stmt->getLine())
)) { )) {
@ -2686,8 +2686,12 @@ class StatementsChecker
$type_match_found = true; $type_match_found = true;
} }
if ($input_type_part->isScalar()) { if ($param_type_part->isNumeric() && $input_type_part->isNumericType()) {
if ($param_type_part->isScalar()) { $type_match_found = true;
}
if ($input_type_part->isScalarType()) {
if ($param_type_part->isScalarType()) {
$scalar_type_match_found = true; $scalar_type_match_found = true;
} }
} }

View File

@ -131,7 +131,7 @@ abstract class Type
public static function fixScalarTerms($type_string) public static function fixScalarTerms($type_string)
{ {
if (in_array(strtolower($type_string), ['numeric', 'int', 'float', 'string', 'bool', 'true', 'false', 'null', 'array', 'object', 'mixed'])) { if (in_array(strtolower($type_string), ['numeric', 'int', 'float', 'string', 'bool', 'true', 'false', 'null', 'array', 'object', 'mixed', 'resource'])) {
return strtolower($type_string); return strtolower($type_string);
} }
elseif ($type_string === 'boolean') { elseif ($type_string === 'boolean') {
@ -395,6 +395,17 @@ abstract class Type
} }
} }
public function isNumeric()
{
if ($this instanceof Atomic) {
return $this->value === 'numeric';
}
if ($this instanceof Union) {
return isset($this->types['numeric']);
}
}
public function isEmpty() public function isEmpty()
{ {
return $this->value === 'empty'; return $this->value === 'empty';
@ -439,7 +450,7 @@ abstract class Type
return $this instanceof Generic; return $this instanceof Generic;
} }
public function isScalar() public function isScalarType()
{ {
if ($this instanceof Atomic) { if ($this instanceof Atomic) {
return $this->value === 'int' || return $this->value === 'int' ||
@ -453,6 +464,17 @@ abstract class Type
return false; return false;
} }
public function isNumericType()
{
if ($this instanceof Atomic) {
return $this->value === 'int' ||
$this->value === 'double' ||
$this->value === 'float';
}
return false;
}
/** /**
* Combines two union types into one * Combines two union types into one
* @param Union $type_1 * @param Union $type_1