1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Added WeakReference support (#2389)

This commit is contained in:
Bruce Weirdan 2019-11-29 08:21:38 +02:00 committed by Matthew Brown
parent 33142e7637
commit 377e47c8bd
2 changed files with 35 additions and 0 deletions

View File

@ -1195,3 +1195,22 @@ class ReflectionClass implements Reflector {
*/ */
public function newInstanceWithoutConstructor() : 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;
}

View File

@ -2067,6 +2067,22 @@ class ClassTemplateTest extends TestCase
takesInts((new ArrayCollection([ "a", "bc" ]))->map("strlen"));' 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));
'
]
]; ];
} }