Skip to content

Commit 8e5ddd6

Browse files
authored
(U2C #5) misc syntax cleanup to take advantage of modern language features (#106)
1 parent 4de6e1f commit 8e5ddd6

23 files changed

+92
-144
lines changed

src/LaunchDarkly/EvaluationReason.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ public function __toString(): string
233233
{
234234
switch ($this->_kind) {
235235
case self::RULE_MATCH:
236-
return $this->_kind . '(' . ($this->_ruleIndex ?? 0) . ',' . ($this->_ruleId ?? '') . ')';
236+
return $this->_kind . '(' . ($this->_ruleIndex ?: 0) . ',' . ($this->_ruleId ?: '') . ')';
237237
case self::PREREQUISITE_FAILED:
238-
return $this->_kind . '(' . ($this->_prerequisiteKey ?? '') . ')';
238+
return $this->_kind . '(' . ($this->_prerequisiteKey ?: '') . ')';
239239
case self::ERROR:
240-
return $this->_kind . '(' . ($this->_errorKind ?? '') . ')';
240+
return $this->_kind . '(' . ($this->_errorKind ?: '') . ')';
241241
default:
242242
return $this->_kind;
243243
}

src/LaunchDarkly/FeatureFlagsState.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function isValid(): bool
9999
*/
100100
public function getFlagValue(string $key): mixed
101101
{
102-
return isset($this->_flagValues[$key]) ? $this->_flagValues[$key] : null;
102+
return $this->_flagValues[$key] ?? null;
103103
}
104104

105105
/**
@@ -112,11 +112,7 @@ public function getFlagValue(string $key): mixed
112112
*/
113113
public function getFlagReason(string $key): ?EvaluationReason
114114
{
115-
if (isset($this->_flagMetadata[$key])) {
116-
$meta = $this->_flagMetadata[$key];
117-
return isset($meta['reason']) ? $meta['reason'] : null;
118-
}
119-
return null;
115+
return ($this->_flagMetadata[$key] ?? [])['reason'] ?? null;
120116
}
121117

122118
/**
@@ -152,7 +148,7 @@ public function jsonSerialize(): array
152148
$metaMap = [];
153149
foreach ($this->_flagMetadata as $key => $meta) {
154150
$meta = array_replace([], $meta);
155-
if (isset($meta['reason'])) {
151+
if ($meta['reason'] ?? null) {
156152
$meta['reason'] = $meta['reason']->jsonSerialize();
157153
}
158154
$metaMap[$key] = $meta;

src/LaunchDarkly/Impl/Events/EventFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ public function newCustomEvent(string $eventName, LDUser $user, mixed $data, int
149149
'key' => $eventName,
150150
'user' => $user
151151
];
152-
if (isset($data)) {
152+
if ($data !== null) {
153153
$e['data'] = $data;
154154
}
155-
if (isset($metricValue)) {
155+
if ($metricValue !== null) {
156156
$e['metricValue'] = $metricValue;
157157
}
158158
if ($user->getAnonymous()) {

src/LaunchDarkly/Impl/Events/EventSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class EventSerializer
1919

2020
public function __construct(array $options)
2121
{
22-
$this->_allAttrsPrivate = isset($options['all_attributes_private']) && $options['all_attributes_private'];
23-
$this->_privateAttrNames = isset($options['private_attribute_names']) ? $options['private_attribute_names'] : [];
22+
$this->_allAttrsPrivate = !!($options['all_attributes_private'] ?? false);
23+
$this->_privateAttrNames = $options['private_attribute_names'] ?? [];
2424
}
2525

2626
public function serializeEvents(array $events): string

src/LaunchDarkly/Impl/Integrations/FeatureRequesterBase.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function __construct(string $baseUri, string $sdkKey, array $options)
3434
$this->_options = $options;
3535
$this->_cache = $this->createCache($options);
3636

37-
if (isset($options['logger']) && $options['logger']) {
37+
if ($options['logger'] ?? null) {
3838
$this->_logger = $options['logger'];
3939
} else {
4040
$this->_logger = new NullLogger();
@@ -140,30 +140,26 @@ public function getAllFeatures(): ?array
140140
protected function getJsonItem(string $namespace, string $key): ?array
141141
{
142142
$cacheKey = $this->makeCacheKey($namespace, $key);
143-
$raw = $this->_cache ? $this->_cache->getCachedString($cacheKey) : null;
143+
$raw = $this->_cache?->getCachedString($cacheKey);
144144
if ($raw === null) {
145145
$raw = $this->readItemString($namespace, $key);
146-
if ($this->_cache) {
147-
$this->_cache->putCachedString($cacheKey, $raw);
148-
}
146+
$this->_cache?->putCachedString($cacheKey, $raw);
149147
}
150148
return ($raw === null) ? null : json_decode($raw, true);
151149
}
152150

153151
protected function getJsonItemList(string $namespace): array
154152
{
155153
$cacheKey = $this->makeCacheKey($namespace, self::ALL_ITEMS_KEY);
156-
$raw = $this->_cache ? $this->_cache->getCachedString($cacheKey) : null;
154+
$raw = $this->_cache?->getCachedString($cacheKey);
157155
if ($raw) {
158156
$values = json_decode($raw, true);
159157
} else {
160158
$values = $this->readItemStringList($namespace);
161159
if (!$values) {
162160
$values = [];
163161
}
164-
if ($this->_cache) {
165-
$this->_cache->putCachedString($cacheKey, json_encode($values));
166-
}
162+
$this->_cache?->putCachedString($cacheKey, json_encode($values));
167163
}
168164
foreach ($values as $i => $s) {
169165
$values[$i] = json_decode($s, true);

src/LaunchDarkly/Impl/Integrations/FileDataFeatureRequester.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,31 @@ private function loadFile(string $filePath, array &$flags, array &$segments): vo
7979
if ($data == null) {
8080
throw new \InvalidArgumentException("File is not valid JSON: " . $filePath);
8181
}
82-
if (isset($data['flags'])) {
83-
foreach ($data['flags'] as $key => $value) {
84-
$flag = FeatureFlag::decode($value);
85-
$this->tryToAdd($flags, $key, $flag, "feature flag");
86-
}
82+
foreach ($data['flags'] ?? [] as $key => $value) {
83+
$flag = FeatureFlag::decode($value);
84+
$this->tryToAdd($flags, $key, $flag, "feature flag");
8785
}
88-
if (isset($data['flagValues'])) {
89-
foreach ($data['flagValues'] as $key => $value) {
90-
$flag = FeatureFlag::decode([
91-
"key" => $key,
92-
"version" => 1,
93-
"on" => false,
94-
"prerequisites" => [],
95-
"salt" => "",
96-
"targets" => [],
97-
"rules" => [],
98-
"fallthrough" => [],
99-
"offVariation" => 0,
100-
"variations" => [$value],
101-
"deleted" => false,
102-
"trackEvents" => false,
103-
"clientSide" => false
104-
]);
105-
$this->tryToAdd($flags, $key, $flag, "feature flag");
106-
}
86+
foreach ($data['flagValues'] ?? [] as $key => $value) {
87+
$flag = FeatureFlag::decode([
88+
"key" => $key,
89+
"version" => 1,
90+
"on" => false,
91+
"prerequisites" => [],
92+
"salt" => "",
93+
"targets" => [],
94+
"rules" => [],
95+
"fallthrough" => [],
96+
"offVariation" => 0,
97+
"variations" => [$value],
98+
"deleted" => false,
99+
"trackEvents" => false,
100+
"clientSide" => false
101+
]);
102+
$this->tryToAdd($flags, $key, $flag, "feature flag");
107103
}
108-
if (isset($data['segments'])) {
109-
foreach ($data['segments'] as $key => $value) {
110-
$segment = Segment::decode($value);
111-
$this->tryToAdd($segments, $key, $segment, "user segment");
112-
}
104+
foreach ($data['segments'] ?? [] as $key => $value) {
105+
$segment = Segment::decode($value);
106+
$this->tryToAdd($segments, $key, $segment, "user segment");
113107
}
114108
}
115109

src/LaunchDarkly/Impl/Model/Clause.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,16 @@ private function __construct(?string $attribute, ?string $op, array $values, boo
3535
*/
3636
public static function getDecoder(): \Closure
3737
{
38-
return function ($v) {
39-
return new Clause($v['attribute'], $v['op'], $v['values'], $v['negate']);
40-
};
38+
return fn ($v) => new Clause($v['attribute'], $v['op'], $v['values'], $v['negate']);
4139
}
4240

4341
public function matchesContext(LDContext $context, ?FeatureRequester $featureRequester): bool
4442
{
4543
if ($this->_op === 'segmentMatch') {
4644
foreach ($this->_values as $value) {
47-
$segment = $featureRequester ? $featureRequester->getSegment($value) : null;
48-
if ($segment) {
49-
if ($segment->matchesContext($context)) {
50-
return $this->_maybeNegate(true);
51-
}
45+
$segment = $featureRequester?->getSegment($value);
46+
if ($segment?->matchesContext($context)) {
47+
return $this->_maybeNegate(true);
5248
}
5349
}
5450
return $this->_maybeNegate(false);

src/LaunchDarkly/Impl/Model/FeatureFlag.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ protected function __construct(
8787
*/
8888
public static function getDecoder(): \Closure
8989
{
90-
return function ($v) {
91-
return new FeatureFlag(
90+
return fn ($v) =>
91+
new FeatureFlag(
9292
$v['key'],
9393
$v['version'],
9494
$v['on'],
@@ -100,12 +100,11 @@ public static function getDecoder(): \Closure
100100
$v['offVariation'],
101101
$v['variations'] ?: [],
102102
$v['deleted'],
103-
isset($v['trackEvents']) && $v['trackEvents'],
104-
isset($v['trackEventsFallthrough']) && $v['trackEventsFallthrough'],
105-
isset($v['debugEventsUntilDate']) ? $v['debugEventsUntilDate'] : null,
106-
isset($v['clientSide']) && $v['clientSide']
103+
!!($v['trackEvents'] ?? false),
104+
!!($v['trackEventsFallthrough'] ?? false),
105+
$v['debugEventsUntilDate'] ?? null,
106+
!!($v['clientSide'] ?? false)
107107
);
108-
};
109108
}
110109

111110
public static function decode(array $v): self
@@ -136,7 +135,7 @@ public function isExperiment(EvaluationReason $reason): bool
136135
case 'RULE_MATCH':
137136
$i = $reason->getRuleIndex();
138137
$rules = $this->getRules();
139-
return isset($i) && $i >= 0 && $i < count($rules) && $rules[$i]->isTrackEvents();
138+
return $i !== null && $i >= 0 && $i < count($rules) && $rules[$i]->isTrackEvents();
140139
case 'FALLTHROUGH':
141140
return $this->isTrackEventsFallthrough();
142141
default:

src/LaunchDarkly/Impl/Model/Prerequisite.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ protected function __construct(string $key, int $variation)
2525

2626
public static function getDecoder(): \Closure
2727
{
28-
return function (array $v) {
29-
return new Prerequisite($v['key'], $v['variation']);
30-
};
28+
return fn (array $v) => new Prerequisite($v['key'], $v['variation']);
3129
}
3230

3331
public function getKey(): string

src/LaunchDarkly/Impl/Model/Rollout.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function __construct(
3030
) {
3131
$this->_variations = $variations;
3232
$this->_bucketBy = $bucketBy;
33-
$this->_kind = $kind ?? 'rollout';
33+
$this->_kind = $kind ?: 'rollout';
3434
$this->_seed = $seed;
3535
}
3636

0 commit comments

Comments
 (0)