mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Added WeakReference support (#2389)
This commit is contained in:
parent
33142e7637
commit
377e47c8bd
@ -1195,3 +1195,22 @@ class ReflectionClass implements Reflector {
|
||||
*/
|
||||
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 @@ class ClassTemplateTest extends TestCase
|
||||
|
||||
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));
|
||||
'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user