Skip to content

Commit 547a400

Browse files
committed
get, nth: restore per-mode return types on the subclasses
Moving get() and nth() into the Deprecations trait dropped the narrowing overrides v2.7 had, so static analysis saw the wide base union at every call site: raw-mode get() claimed it could return SmartString, HTML-mode get() claimed raw scalars. Both subclasses now redeclare them next to the other narrowed proxies, with the deprecation markers repeated so IDEs keep flagging them. get()'s proxy forwards func_get_args() because get() branches on whether a default was passed - a fixed second argument would repeat the 2.7 where() inversion.
1 parent 624c0bd commit 547a400

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/SmartArray.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use InvalidArgumentException;
88
use Itools\SmartString\SmartString;
9+
use JetBrains\PhpStorm\Deprecated;
910

1011
/**
1112
* SmartArray - Collection returning raw PHP values (string, int, float, bool, null).
@@ -236,13 +237,35 @@ public function load(string $field): static|SmartNull
236237
}
237238

238239
//endregion
239-
//region Deprecated Array Access
240+
//region Deprecated Access
240241

241242
/** {@inheritDoc} */
242243
public function offsetGet(mixed $offset): static|SmartNull|string|int|float|bool|null
243244
{
244245
return parent::offsetGet($offset);
245246
}
246247

248+
/**
249+
* {@inheritDoc}
250+
* @deprecated Use property access: ->key, or ->{'users.id'} for keys property syntax
251+
* can't type. For a missing-key default use ->key ?? $default.
252+
*/
253+
#[Deprecated(reason: "use property access ->key or ->{'key'}, with ?? for defaults")]
254+
public function get(int|string|SmartString|SmartNull $key, mixed $default = null): static|SmartNull|string|int|float|bool|null
255+
{
256+
// func_get_args: get() branches on whether $default was passed, so forward the real arg count
257+
return parent::get(...func_get_args());
258+
}
259+
260+
/**
261+
* {@inheritDoc}
262+
* @deprecated Use ->at() - same behavior, new name
263+
*/
264+
#[Deprecated(reason: 'renamed to at()', replacement: '%class%->at()')]
265+
public function nth(int $index): static|SmartNull|string|int|float|bool|null
266+
{
267+
return parent::nth($index);
268+
}
269+
247270
//endregion
248271
}

src/SmartArrayHtml.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use InvalidArgumentException;
66
use Iterator;
77
use Itools\SmartString\SmartString;
8+
use JetBrains\PhpStorm\Deprecated;
89

910
/**
1011
* SmartArrayHtml - Collection returning SmartString values for HTML safety.
@@ -231,13 +232,35 @@ public function load(string $field): static|SmartNull
231232
}
232233

233234
//endregion
234-
//region Deprecated Array Access
235+
//region Deprecated Access
235236

236237
/** {@inheritDoc} */
237238
public function offsetGet(mixed $offset): static|SmartNull|SmartString
238239
{
239240
return parent::offsetGet($offset);
240241
}
241242

243+
/**
244+
* {@inheritDoc}
245+
* @deprecated Use property access: ->key, or ->{'users.id'} for keys property syntax
246+
* can't type. For a missing-key default use ->key ?? $default.
247+
*/
248+
#[Deprecated(reason: "use property access ->key or ->{'key'}, with ?? for defaults")]
249+
public function get(int|string|SmartString|SmartNull $key, mixed $default = null): static|SmartNull|SmartString
250+
{
251+
// func_get_args: get() branches on whether $default was passed, so forward the real arg count
252+
return parent::get(...func_get_args());
253+
}
254+
255+
/**
256+
* {@inheritDoc}
257+
* @deprecated Use ->at() - same behavior, new name
258+
*/
259+
#[Deprecated(reason: 'renamed to at()', replacement: '%class%->at()')]
260+
public function nth(int $index): static|SmartNull|SmartString
261+
{
262+
return parent::nth($index);
263+
}
264+
242265
//endregion
243266
}

0 commit comments

Comments
 (0)