Skip to content
Permalink
Browse files

Fix #1704 - improve type coercion complaints

  • Loading branch information...
muglug committed May 29, 2019
1 parent f336720 commit e85794038f99528956008ae498abad9922238867
@@ -2347,6 +2347,7 @@ public static function checkFunctionArgumentType(
&& !$input_type->hasArray()
&& !$param_type->from_docblock
&& $cased_method_id !== 'echo'
&& $cased_method_id !== 'sprintf'
) {
$scalar_type_match_found = false;
@@ -97,6 +97,12 @@ public static function isContainedBy(
$scalar_type_match_found = false;
$all_to_string_cast = true;
$all_type_coerced = null;
$all_type_coerced_from_mixed = null;
$some_type_coerced = false;
$some_type_coerced_from_mixed = false;
if ($input_type_part instanceof TArrayKey
&& ($container_type->hasInt() && $container_type->hasString())
) {
@@ -119,6 +125,8 @@ public static function isContainedBy(
}
$atomic_to_string_cast = false;
$atomic_type_coerced = false;
$atomic_type_coerced_from_mixed = false;
$is_atomic_contained_by = self::isAtomicContainedBy(
$codebase,
@@ -127,8 +135,8 @@ public static function isContainedBy(
$allow_interface_equality,
true,
$scalar_type_match_found,
$type_coerced,
$type_coerced_from_mixed,
$atomic_type_coerced,
$atomic_type_coerced_from_mixed,
$atomic_to_string_cast,
$type_coerced_from_scalar
);
@@ -142,12 +150,35 @@ public static function isContainedBy(
$is_atomic_contained_by = true;
}
if ($atomic_type_coerced) {
$some_type_coerced = true;
}
if ($atomic_type_coerced_from_mixed) {
$some_type_coerced_from_mixed = true;
}
if ($atomic_type_coerced !== true || $all_type_coerced === false) {
$all_type_coerced = false;
} else {
$all_type_coerced = true;
}
if ($atomic_type_coerced_from_mixed !== true || $all_type_coerced_from_mixed === false) {
$all_type_coerced_from_mixed = false;
} else {
$all_type_coerced_from_mixed = true;
}
if ($is_atomic_contained_by) {
$type_match_found = true;
}
if ($atomic_to_string_cast !== true && $type_match_found) {
$all_to_string_cast = false;
if ($atomic_to_string_cast !== true) {
$all_to_string_cast = false;
}
$all_type_coerced_from_mixed = false;
$all_type_coerced = false;
}
}
@@ -158,7 +189,23 @@ public static function isContainedBy(
$to_string_cast = true;
}
if ($all_type_coerced) {
$type_coerced = true;
}
if ($all_type_coerced_from_mixed) {
$type_coerced_from_mixed = true;
}
if (!$type_match_found) {
if ($some_type_coerced) {
$type_coerced = true;
}
if ($some_type_coerced_from_mixed) {
$type_coerced_from_mixed = true;
}
if (!$scalar_type_match_found) {
$has_scalar_match = false;
}
@@ -604,6 +604,20 @@ function iBarFirst($i) : C {
return $i->foo();
}',
],
'noTypeCoercionWhenIntersectionMatches' => [
'<?php
interface I1 {}
interface I2 {}
class A implements I1 {}
/** @param A|I2 $i */
function foo($i) : void {}
/** @param I1&I2 $i */
function bar($i) : void {
foo($i);
}'
],
];
}
@@ -683,7 +683,7 @@ function loopI(IteratorAggregate $coll): void
'MixedAssignment', 'UndefinedThisPropertyAssignment',
],
],
'SKIPPED-intersectionIterator' => [
'intersectionIterator' => [
'<?php
/**
* @param \Traversable<int, int>&\Countable $object
@@ -138,6 +138,19 @@ function fooFoo(string $b): void {}
fooFoo(new A());',
'error_message' => 'InvalidArgument',
],
'implicitCastWithStrictTypesToEchoOrSprintf' => [
'<?php declare(strict_types=1);
class A {
public function __toString(): string
{
return "hello";
}
}
echo(new A());
sprintf("hello *", new A());',
'error_message' => 'ImplicitToStringCast',
],
'implicitCast' => [
'<?php
class A {
@@ -151,6 +164,20 @@ function fooFoo(string $b): void {}
fooFoo(new A());',
'error_message' => 'ImplicitToStringCast',
],
'implicitCastToUnion' => [
'<?php
class A {
public function __toString(): string
{
return "hello";
}
}
/** @param string|int $b */
function fooFoo($b): void {}
fooFoo(new A());',
'error_message' => 'ImplicitToStringCast',
],
'implicitCastFromInterface' => [
'<?php
interface I {
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<file name="Bar.php" />
<file name="Bat.php" />
</projectFiles>
</psalm>

0 comments on commit e857940

Please sign in to comment.
You can’t perform that action at this time.