@@ -178,12 +178,12 @@ private function getFeatureRequester(string $sdkKey, array $options): FeatureReq
178178 * does not match any existing flag), `$defaultValue` is returned.
179179 *
180180 * @param string $key the unique key for the feature flag
181- * @param LDContext $context the evaluation context
181+ * @param LDContext|LDUser $context the evaluation context or user
182182 * @param mixed $defaultValue the default value of the flag
183- * @return mixed the variation for the given context, or `$defaultValue` if the flag cannot be evaluated
183+ * @return mixed The variation for the given context, or `$defaultValue` if the flag cannot be evaluated
184184 * @see \LaunchDarkly\LDClient::variationDetail()
185185 */
186- public function variation (string $ key , LDContext $ context , mixed $ defaultValue = false ): mixed
186+ public function variation (string $ key , LDContext | LDUser $ context , mixed $ defaultValue = false ): mixed
187187 {
188188 $ detail = $ this ->variationDetailInternal ($ key , $ context , $ defaultValue , $ this ->_eventFactoryDefault );
189189 return $ detail ->getValue ();
@@ -197,27 +197,28 @@ public function variation(string $key, LDContext $context, mixed $defaultValue =
197197 * detailed event data for this flag.
198198 *
199199 * @param string $key the unique key for the feature flag
200- * @param LDContext $context the evaluation context
200+ * @param LDContext|LDUser $context the evaluation context or user
201201 * @param mixed $defaultValue the default value of the flag
202202 *
203- * @return EvaluationDetail an EvaluationDetail object that includes the feature flag value
203+ * @return EvaluationDetail An EvaluationDetail object that includes the feature flag value
204204 * and evaluation reason
205205 */
206- public function variationDetail (string $ key , LDContext $ context , mixed $ defaultValue = false ): EvaluationDetail
206+ public function variationDetail (string $ key , LDContext | LDUser $ context , mixed $ defaultValue = false ): EvaluationDetail
207207 {
208208 return $ this ->variationDetailInternal ($ key , $ context , $ defaultValue , $ this ->_eventFactoryWithReasons );
209209 }
210210
211211 /**
212212 * @param string $key
213- * @param LDContext $context
213+ * @param LDContext|LDUser $contextOrUser
214214 * @param mixed $default
215215 * @param EventFactory $eventFactory
216216 *
217217 * @return EvaluationDetail
218218 */
219- private function variationDetailInternal (string $ key , LDContext $ context , mixed $ default , EventFactory $ eventFactory ): EvaluationDetail
219+ private function variationDetailInternal (string $ key , LDContext | LDUser $ contextOrUser , mixed $ default , EventFactory $ eventFactory ): EvaluationDetail
220220 {
221+ $ context = $ contextOrUser instanceof LDUser ? LDContext::fromUser ($ contextOrUser ) : $ contextOrUser ;
221222 $ default = $ this ->_get_default ($ key , $ default );
222223
223224 $ errorDetail = fn (string $ errorKind ): EvaluationDetail =>
@@ -294,17 +295,24 @@ public function isOffline(): bool
294295 }
295296
296297 /**
297- * Tracks that a user performed an event.
298+ * Tracks that an application-defined event occurred.
299+ *
300+ * This method creates a "custom" analytics event containing the specified event name (key)
301+ * and context properties. You may attach arbitrary data or a metric value to the event with the
302+ * optional `data` and `metricValue` parameters.
303+ *
304+ * Note that event delivery is asynchronous, so the event may not actually be sent until later;
305+ * see {@see \LaunchDarkly\LDClient::flush()}.
298306 *
299307 * @param string $eventName The name of the event
300- * @param LDContext $context The user that performed the event
308+ * @param LDContext|LDUser $context The evaluation context or user associated with the event
301309 * @param mixed $data Optional additional information to associate with the event
302310 * @param int|float|null $metricValue A numeric value used by the LaunchDarkly experimentation feature in
303- * numeric custom metrics. Can be omitted if this event is used by only non-numeric metrics. This
304- * field will also be returned as part of the custom event for Data Export.
311+ * numeric custom metrics; can be omitted if this event is used by only non-numeric metrics
305312 */
306- public function track (string $ eventName , LDContext $ context , mixed $ data = null , int |float |null $ metricValue = null ): void
313+ public function track (string $ eventName , LDContext | LDUser $ context , mixed $ data = null , int |float |null $ metricValue = null ): void
307314 {
315+ $ context = $ context instanceof LDUser ? LDContext::fromUser ($ context ) : $ context ;
308316 if (!$ context ->isValid ()) {
309317 $ this ->_logger ->warning ("Track called with null/empty user key! " );
310318 return ;
@@ -313,16 +321,22 @@ public function track(string $eventName, LDContext $context, mixed $data = null,
313321 }
314322
315323 /**
316- * Reports details about a user.
324+ * Reports details about an evaluation context or user.
325+ *
326+ * This method simply creates an analytics event containing the context properties, to
327+ * that LaunchDarkly will know about that context if it does not already.
317328 *
318- * This simply registers the given user properties with LaunchDarkly without evaluating a feature flag.
319- * This also happens automatically when you evaluate a flag.
329+ * Evaluating a flag, by calling {@see \LaunchDarkly\LDClient::variation()} or
330+ * {@see \LaunchDarkly\LDClient::variationDetail()} :func:`variation_detail()`, also sends
331+ * the context information to LaunchDarkly (if events are enabled), so you only need to use
332+ * identify() if you want to identify the context without evaluating a flag.
320333 *
321- * @param LDContext $context The user properties
334+ * @param LDContext|LDUser $context The context or user to register
322335 * @return void
323336 */
324- public function identify (LDContext $ context ): void
337+ public function identify (LDContext | LDUser $ context ): void
325338 {
339+ $ context = $ context instanceof LDUser ? LDContext::fromUser ($ context ) : $ context ;
326340 if (!$ context ->isValid ()) {
327341 $ this ->_logger ->warning ("Identify called with null/empty user key! " );
328342 return ;
@@ -339,7 +353,7 @@ public function identify(LDContext $context): void
339353 *
340354 * This method does not send analytics events back to LaunchDarkly.
341355 *
342- * @param LDContext $context the evalation context
356+ * @param LDContext|LDUser $context the evalation context or user
343357 * @param array $options Optional properties affecting how the state is computed:
344358 * - `clientSideOnly`: Set this to true to specify that only flags marked for client-side use
345359 * should be included; by default, all flags are included
@@ -351,8 +365,9 @@ public function identify(LDContext $context): void
351365 *
352366 * @return FeatureFlagsState a FeatureFlagsState object (will never be null)
353367 */
354- public function allFlagsState (LDContext $ context , array $ options = []): FeatureFlagsState
368+ public function allFlagsState (LDContext | LDUser $ context , array $ options = []): FeatureFlagsState
355369 {
370+ $ context = $ context instanceof LDUser ? LDContext::fromUser ($ context ) : $ context ;
356371 if (!$ context ->isValid ()) {
357372 $ error = $ context ->getError ();
358373 $ this ->_logger ->warning ("Invalid context for allFlagsState ( $ error); returning empty state " );
@@ -395,11 +410,12 @@ public function allFlagsState(LDContext $context, array $options = []): FeatureF
395410 *
396411 * See: [Secure mode](https://docs.launchdarkly.com/sdk/features/secure-mode)
397412 *
398- * @param LDContext $context the evaluation context
399- * @return string the hash value
413+ * @param LDContext|LDUser $context The evaluation context or user
414+ * @return string The hash value
400415 */
401- public function secureModeHash (LDContext $ context ): string
416+ public function secureModeHash (LDContext | LDUser $ context ): string
402417 {
418+ $ context = $ context instanceof LDUser ? LDContext::fromUser ($ context ) : $ context ;
403419 if (!$ context ->isValid ()) {
404420 return "" ;
405421 }
0 commit comments