Skip to content
Permalink
Browse files

Added WeakReference support (#2389)

  • Loading branch information
weirdan authored and muglug committed Nov 29, 2019
1 parent 33142e7 commit 377e47c8bdc43e897ff0b45a723c92cff4c210a6
Showing with 35 additions and 0 deletions.
  1. +19 −0 src/Psalm/Internal/Stubs/CoreGenericClasses.php
  2. +16 −0 tests/Template/ClassTemplateTest.php
@@ -1195,3 +1195,22 @@ public function newInstanceArgs(array $args) : object {}
*/
public function newInstanceWithoutConstructor() : object;
}
/**
* @template-covariant T as object
* @psalm-immutable
*/
final class WeakReference
{
// always fail
public function __construct();
/**
* @template TIn as object
* @param TIn $referent
* @return WeakReference<TIn>
*/
public static function create(object $referent): WeakReference;
/** @return ?T */
public function get(): ?object;
}
@@ -2067,6 +2067,22 @@ function takesInts(ArrayCollection $ints) :void {}
takesInts((new ArrayCollection([ "a", "bc" ]))->map("strlen"));'
],
'weakReferenceIsTyped' => [
'<?php
$e = new Exception;
$r = WeakReference::create($e);
$ex = $r->get();
',
[ '$ex' => 'Exception|null' ],
],
'weakReferenceIsCovariant' => [
'<?php
/** @param WeakReference<Throwable> $_ref */
function acceptsThrowableRef(WeakReference $_ref): void {}
acceptsThrowableRef(WeakReference::create(new Exception));
'
]
];
}

0 comments on commit 377e47c

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