Skip to content

Commit f36449e

Browse files
patel-vanshpaulbalandanmichalsn
committed
Applying code suggestions
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> Co-authored-by: michalsn <michal@sniatala.pl>
1 parent bf7ed1b commit f36449e

File tree

4 files changed

+511
-84
lines changed

4 files changed

+511
-84
lines changed

system/Config/Services.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,8 @@ public static function typography(bool $getShared = true)
872872

873873
/**
874874
* The Context class provides a way to store and retrieve static data throughout requests.
875-
*
876-
* @return Context
877875
*/
878-
public static function context(bool $getShared = true)
876+
public static function context(bool $getShared = true): Context
879877
{
880878
if ($getShared) {
881879
return static::getSharedInstance('context');

system/Context/Context.php

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414
namespace CodeIgniter\Context;
1515

1616
use CodeIgniter\Helpers\Array\ArrayHelper;
17+
use SensitiveParameter;
18+
use SensitiveParameterValue;
1719

18-
class Context
20+
final class Context
1921
{
2022
/**
2123
* The data stored in the context.
2224
*
2325
* @var array<string, mixed>
2426
*/
25-
protected array $data;
27+
private array $data;
2628

2729
/**
28-
* The data that is stored, but not included in logs.
30+
* The data that is stored but not included in logs.
2931
*
3032
* @var array<string, mixed>
3133
*/
3234
private array $hiddenData;
3335

34-
/**
35-
* Constructor
36-
*/
3736
public function __construct()
3837
{
3938
$this->data = [];
@@ -42,6 +41,7 @@ public function __construct()
4241

4342
/**
4443
* Set a key-value pair to the context.
44+
* Supports dot notation for nested arrays.
4545
*
4646
* @param array<string, mixed>|string $key The key to identify the data. Can be a string or an array of key-value pairs.
4747
* @param mixed $value The value to be stored in the context.
@@ -51,43 +51,48 @@ public function __construct()
5151
public function set(array|string $key, mixed $value = null): self
5252
{
5353
if (is_array($key)) {
54-
$this->data = array_merge($this->data, $key);
54+
foreach ($key as $k => $v) {
55+
ArrayHelper::dotSet($this->data, $k, $v);
56+
}
5557

5658
return $this;
5759
}
5860

59-
$this->data[$key] = $value;
61+
ArrayHelper::dotSet($this->data, $key, $value);
6062

6163
return $this;
6264
}
6365

6466
/**
6567
* Set a hidden key-value pair to the context. This data will not be included in logs.
68+
* Supports dot notation for nested arrays.
6669
*
6770
* @param array<string, mixed>|string $key The key to identify the data. Can be a string or an array of key-value pairs.
6871
* @param mixed $value The value to be stored in the context.
6972
*
7073
* @return $this
7174
*/
72-
public function setHidden(array|string $key, mixed $value = null): self
75+
public function setHidden(#[SensitiveParameter] array|string $key, #[SensitiveParameter] mixed $value = null): self
7376
{
7477
if (is_array($key)) {
75-
$this->hiddenData = array_merge($this->hiddenData, $key);
78+
foreach ($key as $k => $v) {
79+
ArrayHelper::dotSet($this->hiddenData, $k, $v);
80+
}
7681

7782
return $this;
7883
}
7984

80-
$this->hiddenData[$key] = $value;
85+
ArrayHelper::dotSet($this->hiddenData, $key, $value);
8186

8287
return $this;
8388
}
8489

8590
/**
8691
* Get a value from the context by its key, or return a default value if the key does not exist.
87-
* Supports dot notation for nested arrays (e.g., 'user.profile.name' to access $data['user']['profile']['name']).
92+
* Supports dot notation for nested arrays.
8893
*
89-
* @param string $key The key to identify the data.
90-
* @param mixed|null $default The default value to return if the key does not exist in the context.
94+
* @param string $key The key to identify the data.
95+
* @param mixed $default The default value to return if the key does not exist in the context.
9196
*
9297
* @return mixed The value associated with the key, or the default value if the key does not exist.
9398
*/
@@ -98,34 +103,28 @@ public function get(string $key, mixed $default = null): mixed
98103

99104
/**
100105
* Get only the specified keys from the context. If a key does not exist, it will be ignored.
106+
* Supports dot notation for nested arrays.
101107
*
102108
* @param list<string>|string $keys An array of keys to retrieve from the context.
103109
*
104110
* @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the context.
105111
*/
106112
public function getOnly(array|string $keys): array
107113
{
108-
if (is_string($keys)) {
109-
$keys = [$keys];
110-
}
111-
112-
return array_filter($this->data, static fn ($k): bool => in_array($k, $keys, true), ARRAY_FILTER_USE_KEY);
114+
return ArrayHelper::dotOnly($this->data, $keys);
113115
}
114116

115117
/**
116118
* Get all keys from the context except the specified keys.
119+
* Supports dot notation for nested arrays.
117120
*
118121
* @param list<string>|string $keys An array of keys to exclude from the context.
119122
*
120123
* @return array<string, mixed> An array of key-value pairs for all keys in the context except the specified keys.
121124
*/
122125
public function getExcept(array|string $keys): array
123126
{
124-
if (is_string($keys)) {
125-
$keys = [$keys];
126-
}
127-
128-
return array_filter($this->data, static fn ($k): bool => ! in_array($k, $keys, true), ARRAY_FILTER_USE_KEY);
127+
return ArrayHelper::dotExcept($this->data, $keys);
129128
}
130129

131130
/**
@@ -140,47 +139,42 @@ public function getAll(): array
140139

141140
/**
142141
* Get a hidden value from the context by its key, or return a default value if the key does not exist.
142+
* Supports dot notation for nested arrays.
143143
*
144144
* @param string $key The key to identify the data.
145145
* @param mixed|null $default The default value to return if the key does not exist in the context.
146146
*
147147
* @return mixed The value associated with the key, or the default value if the key does not exist.
148148
*/
149-
public function getHidden(string $key, mixed $default = null): mixed
149+
public function getHidden(#[SensitiveParameter] string $key, #[SensitiveParameter] mixed $default = null): mixed
150150
{
151151
return ArrayHelper::dotSearch($key, $this->hiddenData) ?? $default;
152152
}
153153

154154
/**
155155
* Get only the specified keys from the hidden context. If a key does not exist, it will be ignored.
156+
* Supports dot notation for nested arrays.
156157
*
157158
* @param list<string>|string $keys An array of keys to retrieve from the hidden context.
158159
*
159160
* @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the hidden context.
160161
*/
161-
public function getOnlyHidden(array|string $keys): array
162+
public function getOnlyHidden(#[SensitiveParameter] array|string $keys): array
162163
{
163-
if (is_string($keys)) {
164-
$keys = [$keys];
165-
}
166-
167-
return array_filter($this->hiddenData, static fn ($k): bool => in_array($k, $keys, true), ARRAY_FILTER_USE_KEY);
164+
return ArrayHelper::dotOnly($this->hiddenData, $keys);
168165
}
169166

170167
/**
171168
* Get all keys from the hidden context except the specified keys.
169+
* Supports dot notation for nested arrays.
172170
*
173171
* @param list<string>|string $keys An array of keys to exclude from the hidden context.
174172
*
175173
* @return array<string, mixed> An array of key-value pairs for all keys in the hidden context except the specified keys.
176174
*/
177-
public function getExceptHidden(array|string $keys): array
175+
public function getExceptHidden(#[SensitiveParameter] array|string $keys): array
178176
{
179-
if (is_string($keys)) {
180-
$keys = [$keys];
181-
}
182-
183-
return array_filter($this->hiddenData, static fn ($k): bool => ! in_array($k, $keys, true), ARRAY_FILTER_USE_KEY);
177+
return ArrayHelper::dotExcept($this->hiddenData, $keys);
184178
}
185179

186180
/**
@@ -195,30 +189,33 @@ public function getAllHidden(): array
195189

196190
/**
197191
* Check if a key exists in the context.
192+
* Supports dot notation for nested arrays.
198193
*
199194
* @param string $key The key to check for existence in the context.
200195
*
201196
* @return bool True if the key exists in the context, false otherwise.
202197
*/
203198
public function has(string $key): bool
204199
{
205-
return ArrayHelper::dotKeyExists($key, $this->data);
200+
return ArrayHelper::dotHas($key, $this->data);
206201
}
207202

208203
/**
209204
* Check if a key exists in the hidden context.
205+
* Supports dot notation for nested arrays.
210206
*
211207
* @param string $key The key to check for existence in the hidden context.
212208
*
213209
* @return bool True if the key exists in the hidden context, false otherwise.
214210
*/
215211
public function hasHidden(string $key): bool
216212
{
217-
return ArrayHelper::dotKeyExists($key, $this->hiddenData);
213+
return ArrayHelper::dotHas($key, $this->hiddenData);
218214
}
219215

220216
/**
221217
* Remove a key-value pair from the context by its key.
218+
* Supports dot notation for nested arrays.
222219
*
223220
* @param list<string>|string $key The key to identify the data to be removed from the context.
224221
*
@@ -228,35 +225,36 @@ public function remove(array|string $key): self
228225
{
229226
if (is_array($key)) {
230227
foreach ($key as $k) {
231-
unset($this->data[$k]);
228+
ArrayHelper::dotUnset($this->data, $k);
232229
}
233230

234231
return $this;
235232
}
236233

237-
unset($this->data[$key]);
234+
ArrayHelper::dotUnset($this->data, $key);
238235

239236
return $this;
240237
}
241238

242239
/**
243240
* Remove a key-value pair from the hidden context by its key.
241+
* Supports dot notation for nested arrays.
244242
*
245243
* @param list<string>|string $key The key to identify the data to be removed from the hidden context.
246244
*
247245
* @return $this
248246
*/
249-
public function removeHidden(array|string $key): self
247+
public function removeHidden(#[SensitiveParameter] array|string $key): self
250248
{
251249
if (is_array($key)) {
252250
foreach ($key as $k) {
253-
unset($this->hiddenData[$k]);
251+
ArrayHelper::dotUnset($this->hiddenData, $k);
254252
}
255253

256254
return $this;
257255
}
258256

259-
unset($this->hiddenData[$key]);
257+
ArrayHelper::dotUnset($this->hiddenData, $key);
260258

261259
return $this;
262260
}
@@ -297,4 +295,17 @@ public function clearHidden(): self
297295

298296
return $this;
299297
}
298+
299+
public function __debugInfo(): array
300+
{
301+
return [
302+
'data' => $this->data,
303+
'hiddenData' => new SensitiveParameterValue($this->hiddenData),
304+
];
305+
}
306+
307+
public function __clone()
308+
{
309+
$this->hiddenData = [];
310+
}
300311
}

0 commit comments

Comments
 (0)