getClosureThis()) { return $closure; } $scope = $reflection->getClosureScopeClass(); $name = $reflection->getShortName(); if (!str_starts_with($name, '{closure')) { /** @psalm-suppress InvalidScope @phpstan-ignore-next-line @phan-suppress-next-line PhanUndeclaredThis */ $closure = fn (...$args) => $this->$name(...$args); if ($scope !== null) { $closure = $closure->bindTo(null, $scope->name); } } static $placeholder; $placeholder ??= new stdClass(); /** @psalm-suppress PossiblyNullReference */ $closure = $closure->bindTo($placeholder); $ref = WeakReference::create($target); /** * @psalm-suppress PossiblyNullReference,PossiblyNullFunctionCall */ return $scope && $target::class === $scope->name && !$scope->isInternal() ? static fn (...$args) => ($obj = $ref->get()) ? $closure->call($obj, ...$args) : null : static fn (...$args) => ($obj = $ref->get()) ? $closure->bindTo($obj)(...$args) : null; }