Skip to content
Permalink
Browse files

Fix potential coercion gotcha

  • Loading branch information...
muglug committed Jul 10, 2019
1 parent 8e14946 commit 9aa0fd022cf8dc6e6724c42947f3da7191eaa559
Showing with 46 additions and 0 deletions.
  1. +4 −0 src/Psalm/Type.php
  2. +8 −0 src/Psalm/Type/Union.php
  3. +34 −0 tests/Template/ClassTemplateTest.php
@@ -1436,6 +1436,10 @@ public static function combineUnionTypes(
$combined_type->ignore_falsable_issues = true;
}
if ($type_1->had_template && $type_2->had_template) {
$combined_type->had_template = true;
}
if ($both_failed_reconciliation) {
$combined_type->failed_reconciliation = true;
}
@@ -1768,6 +1768,10 @@ public function replaceClassLike(string $old, string $new) : void
*/
public function equals(Union $other_type)
{
if ($other_type === $this) {
return true;
}
if ($other_type->id && $this->id && $other_type->id !== $this->id) {
return false;
}
@@ -1776,6 +1780,10 @@ public function equals(Union $other_type)
return false;
}
if ($this->had_template !== $other_type->had_template) {
return false;
}
if ($this->possibly_undefined_from_try !== $other_type->possibly_undefined_from_try) {
return false;
}
@@ -2029,6 +2029,40 @@ function returnFooBase() {
function takesFooDerived($foo): void {}',
'error_message' => 'InvalidReturnStatement'
],
'possiblySpecialiseTypeBeforeReturning' => [
'<?php
class Base {}
class Derived extends Base {}
/**
* @template T of Base
*/
class Foo {
/**
* @param T $t
*/
public function __construct ($t) {}
}
/**
* @return Foo<Base>
*/
function returnFooBase() {
$f = new Foo(new Derived());
if (rand(0, 1)) {
takesFooDerived($f);
}
return $f;
}
/**
* @param Foo<Derived> $foo
*/
function takesFooDerived($foo): void {}',
'error_message' => 'InvalidReturnStatement'
],
'specializeTypeInPropertyAssignment' => [
'<?php
/** @template T */

0 comments on commit 9aa0fd0

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