1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Add InvalidScalarArgument issue to warn about bad casts

This commit is contained in:
Matthew Brown 2016-07-11 19:58:37 -04:00
parent 24300cc8f6
commit f2a53fa463
3 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,7 @@
<?php
namespace CodeInspector\Issue;
class InvalidScalarArgument extends CodeError
{
}

View File

@ -16,6 +16,7 @@ use CodeInspector\Issue\PossiblyUndefinedVariable;
use CodeInspector\Issue\InvalidArrayAssignment;
use CodeInspector\Issue\InvalidArrayAccess;
use CodeInspector\Issue\InvalidPropertyAssignment;
use CodeInspector\Issue\InvalidScalarArgument;
use CodeInspector\Issue\InvalidScope;
use CodeInspector\Issue\InvalidStaticInvocation;
use CodeInspector\Issue\InvalidStaticVariable;
@ -2646,6 +2647,7 @@ class StatementsChecker
}
$type_match_found = false;
$scalar_type_match_found = false;
foreach ($param_type->types as $param_type_part) {
if ($param_type_part->isNull()) {
@ -2664,15 +2666,35 @@ class StatementsChecker
$type_match_found = true;
}
if ($input_type_part->value === 'int' && $param_type_part->value === 'float') {
$type_match_found = true;
}
if ($input_type_part->isScalar() && $param_type_part->isScalar()) {
$scalar_type_match_found = true;
}
if (is_subclass_of($param_type_part->value, $input_type_part->value)) {
// @todo handle coercion
$type_match_found = true;
break;
}
}
if (!$type_match_found) {
if (IssueBuffer::accepts(
if ($scalar_type_match_found) {
if (IssueBuffer::accepts(
new InvalidScalarArgument(
'Argument ' . ($argument_offset + 1) . ' of ' . $method_id . ' expects ' . $param_type . ', ' . $input_type . ' provided',
$file_name,
$line_number
)
)) {
return false;
}
}
else if (IssueBuffer::accepts(
new InvalidArgument(
'Argument ' . ($argument_offset + 1) . ' of ' . $method_id . ' expects ' . $param_type . ', ' . $input_type . ' provided',
$file_name,

View File

@ -339,6 +339,21 @@ abstract class Type
return false;
}
public function hasGeneric()
{
if ($this instanceof Union) {
foreach ($this->types as $type) {
if ($type instanceof Generic) {
return true;
}
}
return false;
}
return $this instanceof Generic;
}
public function isScalar()
{
if ($this instanceof Atomic) {
@ -349,6 +364,8 @@ abstract class Type
$this->value === 'bool' ||
$this->value === 'false';
}
return false;
}
/**