Skip to content

Commit 9d9f6cf

Browse files
committed
messages: recommend ::new() everywhere, document why it exists
Constructor errors said "use new SmartArrayHtml($data)" but the docs teach SmartArrayHtml::new(). Standardized every message on ::new(), and the docblocks now say it exists for chaining on pre-8.4 PHP. Also switched strings with escaped \$ to single quotes where nothing needed interpolating.
1 parent 9c81161 commit 9d9f6cf

5 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/DeprecatedAliases.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function triggerArrayAccessDeprecation(mixed $key, string $operation = '
244244
// programmer error that PHP itself treats as an empty-string key.
245245
$suggestion = match ($operation) {
246246
'set' => match (true) {
247-
is_null($key) => "->set(\$key, \$value) using an explicit key",
247+
is_null($key) => '->set($key, $value) using an explicit key',
248248
is_int($key) => "->set($key, \$value)",
249249
$isValidPropName => "->$key = \$value",
250250
default => "->set('$key', \$value) or ->{'$key'} = \$value",

src/SmartArray.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ public function __construct(array $array = [], bool|array|null $properties = [])
4444
// Handle deprecated boolean parameter: true contradicts this class (throw),
4545
// false is redundant (deprecation only)
4646
if ($properties === true) {
47-
self::logDeprecation("new SmartArray(\$data, true) is deprecated. Use new SmartArrayHtml(\$data) instead.");
48-
throw new InvalidArgumentException("Cannot create SmartArray with useSmartStrings=true. Use new SmartArrayHtml(\$data) instead.");
47+
self::logDeprecation('Creating a SmartArray with useSmartStrings=true is deprecated. Use SmartArrayHtml::new($data) instead.');
48+
throw new InvalidArgumentException('Cannot create SmartArray with useSmartStrings=true. Use SmartArrayHtml::new($data) instead.');
4949
}
5050
if ($properties === false) {
51-
self::logDeprecation("Passing false to SmartArray is deprecated. Just use SmartArray(\$data)");
51+
self::logDeprecation('Passing false to SmartArray is deprecated. Just use SmartArray::new($data)');
5252
$properties = [];
5353
}
5454

5555
// Handle deprecated useSmartStrings in array
5656
if (is_array($properties) && ($properties['useSmartStrings'] ?? false) === true) {
57-
self::logDeprecation("new SmartArray(\$data, ['useSmartStrings' => true]) is deprecated. Use new SmartArrayHtml(\$data) instead.");
58-
throw new InvalidArgumentException("Cannot create SmartArray with useSmartStrings=true. Use new SmartArrayHtml(\$data) instead.");
57+
self::logDeprecation('Creating a SmartArray with useSmartStrings=true is deprecated. Use SmartArrayHtml::new($data) instead.');
58+
throw new InvalidArgumentException('Cannot create SmartArray with useSmartStrings=true. Use SmartArrayHtml::new($data) instead.');
5959
}
6060

6161
// Force useSmartStrings to false for raw values
@@ -68,6 +68,12 @@ public function __construct(array $array = [], bool|array|null $properties = [])
6868
/**
6969
* Create a new SmartArray that returns raw values without SmartString wrapping.
7070
*
71+
* Same as `new SmartArray()`, but chainable on every supported PHP version:
72+
* before PHP 8.4, `new SmartArray($data)->pluck('id')` is a syntax error without
73+
* wrapping parentheses; `SmartArray::new($data)->pluck('id')` works everywhere.
74+
*
75+
* $users = SmartArray::new($records)->indexBy('user_id');
76+
*
7177
* @param array $array The input array to convert
7278
* @param array|bool $properties Optional properties to pass to the constructor (legacy boolean handled by the constructor)
7379
* @return static A new SmartArray instance

src/SmartArrayBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ public function __debugInfo(): array
12381238
$output = [];
12391239
if ($this === $this->root()) {
12401240
// Call ->help() for usage examples and documentation, or ->debug() to view metadata
1241-
$output["README:" . self::stripNamespace(static::class) . ":private"] = "Call \$obj->help() for documentation, or ->debug() to view metadata";
1241+
$output["README:" . self::stripNamespace(static::class) . ":private"] = 'Call $obj->help() for documentation, or ->debug() to view metadata';
12421242
$output["*useSmartStrings*:private"] = match ($this->useSmartStrings) {
12431243
true => "true, // Values are returned as SmartString objects on access\n",
12441244
false => "false, // Values are returned **as-is** on access (no extra encoding)\n",
@@ -1504,7 +1504,7 @@ public function __toString(): string
15041504
// PHP Error: Fatal error: Uncaught Error: Object of class Itools\SmartArray\SmartArray could not be converted to string in C:\path\file.php:27
15051505
$warning = "Can't convert SmartArray to string $inFileOnLine.\n\n";
15061506
$warning .= "In double-quoted strings, use \"\$var->property\" for properties, but wrap methods in braces like \"{\$var->method()}\"\n\n";
1507-
$warning .= "For more info: \$var->help()";
1507+
$warning .= 'For more info: $var->help()';
15081508

15091509
// output warning
15101510
echo "\nWarning: $warning\n\n"; // Output with echo so PHP doesn't add the filename and line number of this function on the end

src/SmartArrayHtml.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ public function __construct(array $array = [], bool|array|null $properties = [])
4040
// Handle deprecated boolean parameter: false contradicts this class (throw),
4141
// true is redundant (deprecation only)
4242
if ($properties === false) {
43-
self::logDeprecation("new SmartArrayHtml(\$data, false) is deprecated. Use new SmartArray(\$data) instead.");
44-
throw new InvalidArgumentException("Cannot create SmartArrayHtml with useSmartStrings=false. Use new SmartArray(\$data) instead.");
43+
self::logDeprecation('Creating a SmartArrayHtml with useSmartStrings=false is deprecated. Use SmartArray::new($data) instead.');
44+
throw new InvalidArgumentException('Cannot create SmartArrayHtml with useSmartStrings=false. Use SmartArray::new($data) instead.');
4545
}
4646
if ($properties === true) {
47-
self::logDeprecation("Passing true to SmartArrayHtml is deprecated. Just use SmartArrayHtml(\$data)");
47+
self::logDeprecation('Passing true to SmartArrayHtml is deprecated. Just use SmartArrayHtml::new($data)');
4848
$properties = [];
4949
}
5050

5151
// Handle deprecated useSmartStrings in array
5252
if (is_array($properties) && ($properties['useSmartStrings'] ?? true) === false) {
53-
self::logDeprecation("new SmartArrayHtml(\$data, ['useSmartStrings' => false]) is deprecated. Use new SmartArray(\$data) instead.");
54-
throw new InvalidArgumentException("Cannot create SmartArrayHtml with useSmartStrings=false. Use new SmartArray(\$data) instead.");
53+
self::logDeprecation('Creating a SmartArrayHtml with useSmartStrings=false is deprecated. Use SmartArray::new($data) instead.');
54+
throw new InvalidArgumentException('Cannot create SmartArrayHtml with useSmartStrings=false. Use SmartArray::new($data) instead.');
5555
}
5656

5757
// Force useSmartStrings to true so values are SmartStrings
@@ -64,6 +64,12 @@ public function __construct(array $array = [], bool|array|null $properties = [])
6464
/**
6565
* Create a new SmartArrayHtml that returns SmartString objects for HTML safety.
6666
*
67+
* Same as `new SmartArrayHtml()`, but chainable on every supported PHP version:
68+
* before PHP 8.4, `new SmartArrayHtml($data)->pluck('id')` is a syntax error without
69+
* wrapping parentheses; `SmartArrayHtml::new($data)->pluck('id')` works everywhere.
70+
*
71+
* $users = SmartArrayHtml::new($records)->indexBy('user_id');
72+
*
6773
* @param array $array The input array to convert
6874
* @param array|bool $properties Optional properties to pass to the constructor (legacy boolean handled by the constructor)
6975
* @return static A new SmartArrayHtml instance

tests/Unit/CreationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public function testConstructorRejectsUnsupportedValues(string $class): void
113113
public static function contradictoryBoolProvider(): array
114114
{
115115
return [
116-
'SmartArray with true' => [SmartArray::class, true, 'Cannot create SmartArray with useSmartStrings=true. Use new SmartArrayHtml($data) instead.'],
117-
'SmartArrayHtml with false' => [SmartArrayHtml::class, false, 'Cannot create SmartArrayHtml with useSmartStrings=false. Use new SmartArray($data) instead.'],
116+
'SmartArray with true' => [SmartArray::class, true, 'Cannot create SmartArray with useSmartStrings=true. Use SmartArrayHtml::new($data) instead.'],
117+
'SmartArrayHtml with false' => [SmartArrayHtml::class, false, 'Cannot create SmartArrayHtml with useSmartStrings=false. Use SmartArray::new($data) instead.'],
118118
];
119119
}
120120

0 commit comments

Comments
 (0)