@@ -28,7 +28,7 @@ class Client
2828 * @param ?LoopInterface $loop
2929 * @param ?MulticastFactory $multicast
3030 */
31- public function __construct (LoopInterface $ loop = null , MulticastFactory $ multicast = null )
31+ public function __construct (? LoopInterface $ loop = null , ? MulticastFactory $ multicast = null )
3232 {
3333 $ this ->loop = $ loop ?: Loop::get ();
3434 $ this ->multicast = $ multicast ?: new MulticastFactory ($ this ->loop );
@@ -46,13 +46,18 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
4646 $ socket = $ this ->multicast ->createSender ();
4747 // TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
4848
49- $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred ) {
49+ // Initialize deferred first
50+ $ deferred = new Deferred ();
51+
52+ // Then use it in the timer
53+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , $ deferred ) {
5054 $ deferred ->resolve ();
5155 $ socket ->close ();
5256 });
5357
58+ // Redefine the deferred with the proper canceller now that timer exists
5459 $ loop = $ this ->loop ;
55- $ deferred = new Deferred (function () use ($ socket , & $ timer , $ loop ) {
60+ $ deferred = new Deferred (function () use ($ socket , $ timer , $ loop ) {
5661 // canceling resulting promise cancels timer and closes socket
5762 $ loop ->cancelTimer ($ timer );
5863 $ socket ->close ();
0 commit comments