Skip to content

Commit 98e55f1

Browse files
author
Dan Richelson
committed
add better check for blank user key
1 parent f3fe42a commit 98e55f1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/LaunchDarkly/LDClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function variation($key, $user, $default = false) {
116116
$this->_logger->warn("Variation called with null user or null user key! Returning default value");
117117
return $default;
118118
}
119-
if (strlen($user->getKey()) === 0) {
119+
if ($user->isKeyBlank()) {
120120
$this->_logger->warn("User key is blank. Flag evaluation will proceed, but the user will not be stored in LaunchDarkly.");
121121
}
122122
$flag = $this->_featureRequester->get($key);
@@ -177,7 +177,7 @@ public function track($eventName, $user, $data) {
177177
if ($this->isOffline()) {
178178
return;
179179
}
180-
if (is_null($user) || strlen($user->getKey()) === 0) {
180+
if (is_null($user) || $user->isKeyBlank()) {
181181
$this->_logger->warn("Track called with null user or null/empty user key!");
182182
}
183183

@@ -199,7 +199,7 @@ public function identify($user) {
199199
if ($this->isOffline()) {
200200
return;
201201
}
202-
if (is_null($user) || strlen($user->getKey()) === 0) {
202+
if (is_null($user) || $user->isKeyBlank()) {
203203
$this->_logger->warn("Track called with null user or null/empty user key!");
204204
}
205205

src/LaunchDarkly/LDUser.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class LDUser {
3333
* @param array|null $custom Other custom attributes that can be used to create custom rules
3434
*/
3535
public function __construct($key, $secondary = null, $ip = null, $country = null, $email = null, $name = null, $avatar = null, $firstName = null, $lastName = null, $anonymous = null, $custom = array()) {
36-
$this->_key = strval($key);
36+
if ($key !== null) {
37+
$this->_key = strval($key);
38+
}
3739
$this->_secondary = $secondary;
3840
$this->_ip = $ip;
3941
$this->_country = $country;
@@ -92,6 +94,9 @@ public function getIP() {
9294
return $this->_ip;
9395
}
9496

97+
/**
98+
* @return null|string
99+
*/
95100
public function getKey() {
96101
return $this->_key;
97102
}
@@ -124,6 +129,10 @@ public function getAnonymous() {
124129
return $this->_anonymous;
125130
}
126131

132+
public function isKeyBlank() {
133+
return isset($this->_key) && empty($this->_key);
134+
}
135+
127136
public function toJSON() {
128137
$json = array("key" => $this->_key);
129138

tests/LDUserTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,20 @@ public function testLDUserAnonymous() {
100100
$this->assertEquals(true, $json['anonymous']);
101101

102102
}
103+
104+
public function testLDUserBlankKey() {
105+
$builder = new LDUserBuilder("");
106+
$user = $builder->build();
107+
$this->assertTrue($user->isKeyBlank());
108+
109+
$builder = new LDUserBuilder("key");
110+
$user = $builder->build();
111+
$this->assertFalse($user->isKeyBlank());
112+
113+
$builder = new LDUserBuilder(null);
114+
$user = $builder->build();
115+
$this->assertFalse($user->isKeyBlank());
116+
117+
}
103118
}
104119

0 commit comments

Comments
 (0)