Skip to content

Commit b6b0791

Browse files
committed
docs: finish the QA fix list
- README: fix the run-on support sentence, backtick htmlspecialchars() - date example: use a 2026 timestamp (example dates use the current year), update the test to match - method-reference: trim() shows its real signature, pregReplace() says what throws and what returns null, add missing backticks here and in ai-reference - troubleshooting: decode example uses the same flags as the other pages - ai-reference: new SmartString() works like new(); point phoneFormat() users at pregReplace() like the changelog does - cut the grep-audit sentence from the encoding intro (audit tips don't belong on a learning page), reword three phrases into plain English
1 parent 964ba45 commit b6b0791

9 files changed

Lines changed: 27 additions & 24 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ https://github.com/interactivetools-com/SmartString/blob/main/docs/ai-reference.
1111

1212
SmartString lets you write template code that's shorter, easier to read, and XSS-safe by default.
1313
Values HTML-encode themselves the moment you echo, interpolate, or concatenate them, so one
14-
forgotten htmlspecialchars() can't become an injection.
14+
forgotten `htmlspecialchars()` can't become an injection.
1515

1616
Instead of writing code like this:
1717

@@ -70,7 +70,7 @@ $rows = $orders->toArray();
7070

7171
## Questions?
7272

73-
This library was developed for CMS Builder, post a message in our "CMS Builder" forum here:
73+
This library was developed for CMS Builder. Post a message in our "CMS Builder" forum here:
7474
[https://www.interactivetools.com/forum/](https://www.interactivetools.com/forum/)
7575

7676
## License

docs/ai-reference.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ echo $str->value(); // It's easy!<hr>
5050
echo $str->trim()->maxChars(60)->or('None'); // chains left to right
5151
```
5252

53+
`new SmartString($value)` and `SmartString::new($value)` do the same thing.
54+
`new()` also accepts arrays (returns `SmartArrayHtml`), and before PHP 8.4
55+
`new SmartString($x)->trim()` is a syntax error without wrapping parentheses;
56+
`SmartString::new($x)->trim()` always works.
57+
5358
Key definitions used throughout:
5459

5560
- **missing** = null or `""` exactly. Zero (`0`, `"0"`) and `false` are NOT
@@ -252,10 +257,10 @@ map(callable|string $callback, mixed ...$args): SmartString
252257

253258
Calls `$callback($rawValue, ...$args)` and wraps the result. The callback ALWAYS
254259
runs and receives the raw value in its original type, null included (matches
255-
`array_map()`). Built-ins with typed string parameters throw TypeError on null
260+
`array_map()`). Built-ins with typed string parameters throw `TypeError` on null
256261
or numeric values - chain `->map('strval')` first. Callback must return scalar or null; other
257-
return types throw InvalidArgumentException. Non-callable `$callback` throws
258-
InvalidArgumentException.
262+
return types throw `InvalidArgumentException`. Non-callable `$callback` throws
263+
`InvalidArgumentException`.
259264

260265
```php
261266
echo $name->map('mb_strtoupper');
@@ -340,7 +345,7 @@ deprecated; always write the current name in new code.
340345
| `->noEncode()` | `->rawHtml()` |
341346
| `->jsEncode()` | `->jsonEncode()` - different output; the old name keeps the old behavior |
342347
| `SmartString::fromArray()` | `SmartArrayHtml::new()` (SmartArray library) |
343-
| `->phoneFormat()` | retired, no replacement - format phone numbers yourself |
348+
| `->phoneFormat()` | retired - use `pregReplace()` or format the number yourself |
344349
| `SmartString::help()` | retired - this file and the GitHub docs replaced it |
345350

346351
---

docs/common-patterns.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ function is called just once (the name matches `array_map()` and
238238
`SmartArray::map()`):
239239

240240
```php
241-
echo $province->code->map('mb_strtoupper'); // BC
242-
echo $user->name->map('strval')->map('mb_convert_case', MB_CASE_TITLE); // built-ins need a string: strval converts null and numbers
243-
echo $sku->map(fn($v) => str_pad((string)$v, 6, '0', STR_PAD_LEFT)); // zero-pads to 6 digits: 000042
241+
echo $province->code->map('mb_strtoupper'); // BC
242+
echo $user->name->map('strval')->map('mb_convert_case', MB_CASE_TITLE); // built-ins need a string: strval converts null and numbers
243+
echo $sku->map(fn($v) => str_pad((string)$v, 6, '0', STR_PAD_LEFT)); // zero-pads to 6 digits: 000042
244244
```
245245

246246
A closure even works inside a template string; wrap the whole chain in

docs/encoding-and-html.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ Auto-encoding is SmartString's main job, and this page covers all of it:
66
how encoding works, the encoding methods for URLs and JSON, and the named
77
methods that let real markup through: `nl2br()`, `rawHtml()`, and the
88
`appendHtml()`/`wrapHtml()` pair. Markup enters your page only through
9-
those named methods, which is what makes SmartString output reviewable:
10-
search for `rawHtml(` and `Html(` and you have found every place unencoded
11-
markup can appear.
9+
those named methods; everything else comes out encoded.
1210

1311
Contents:
1412

docs/method-reference.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ ready for regular PHP code.
7171
| `->prepend($value)` | Adds `$value` to the beginning of the current value |
7272
| `->wrap($before, $after)` | Wraps the value; pass "" for a side you don't want |
7373
| `->textOnly()` | Removes HTML tags, decodes entities, and trims whitespace |
74-
| `->trim()` | Trims whitespace (or the characters you specify) from both ends |
74+
| `->trim(...$args)` | Trims whitespace from both ends, or pass a character list like PHP `trim()` |
7575
| `->maxWords($max, $ellipsis = '...')` | Limits the value to `$max` words; adds `$ellipsis` if text was cut off |
7676
| `->maxChars($max, $ellipsis = '...')` | Limits the value to `$max` characters; adds `$ellipsis` if text was cut off |
77-
| `->pregReplace($pattern, $replacement)` | Replaces text matching a regex pattern |
77+
| `->pregReplace($pattern, $replacement)` | Replaces text matching a regex; an invalid `$pattern` throws `InvalidArgumentException`, a value it can't process (bad UTF-8) returns null |
7878

7979
### [Dates & Numbers](text-and-formatting.md#formatting-dates---dateformat)
8080

@@ -116,8 +116,8 @@ continues. Zero counts as present.*
116116
|-------------------------|--------------------------------------------------------------------------------------|
117117
| `->orDie($text)` | Outputs the message and exits |
118118
| `->or404($text = null)` | Outputs a 404 header and the message (default: standard not-found text), then exits |
119-
| `->orThrow($text)` | Throws a RuntimeException with the message |
120-
| `->orRedirect($url)` | Redirects to `$url` and exits (throws RuntimeException if headers were already sent) |
119+
| `->orThrow($text)` | Throws a `RuntimeException` with the message |
120+
| `->orRedirect($url)` | Redirects to `$url` and exits (throws `RuntimeException` if headers were already sent) |
121121

122122
### [Value Checks](conditionals-and-error-checking.md#truefalse-checks---isempty-isnotempty-ismissing-isnull)
123123

@@ -127,8 +127,8 @@ want.*
127127

128128
| Method | Description |
129129
|------------------|--------------------------------------------------------------------------------------|
130-
| `->isEmpty()` | Returns true when the value is empty ("", null, false, 0, "0") - same as PHP empty() |
131-
| `->isNotEmpty()` | Returns true when the value has content - the exact opposite of isEmpty() |
130+
| `->isEmpty()` | Returns true when the value is empty ("", null, false, 0, "0") - same as PHP `empty()` |
131+
| `->isNotEmpty()` | Returns true when the value has content - the exact opposite of `isEmpty()` |
132132
| `->isMissing()` | Returns true when the value is missing (null or ""); zero counts as present |
133133
| `->isNull()` | Returns true when the value is null |
134134

docs/performance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ combinations, recorded in the repo at `.github/scripts/speed-results.md`.
174174
Four benchmark choices, stated plainly:
175175

176176
- **Both sides produce identical output.** The helper is timed with the same
177-
full flags SmartString uses, so the race is work-for-work:
177+
full flags SmartString uses, so both sides do the same work:
178178

179179
```php
180180
htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML5, 'UTF-8'); // both what we time against and what we produce

docs/text-and-formatting.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ shows. Numeric values are treated as unix timestamps and format in your
160160
server's PHP timezone; everything else is parsed with `strtotime()`:
161161

162162
```php
163-
echo SmartString::new(1684159800)->dateFormat('Y-m-d T'); // 2023-05-15 PDT
163+
echo SmartString::new(1778866200)->dateFormat('Y-m-d T'); // 2026-05-15 PDT
164164
```
165165

166166
Invalid dates return null rather than throwing, so a fallback chains
@@ -281,8 +281,8 @@ before the math:
281281
echo $value->ifNull(0)->add(50); // 50
282282
```
283283

284-
A null result is a value like any other, so a mid-chain replacement fully
285-
recovers the chain; nothing is poisoned:
284+
A null result is a value like any other, so you can swap in a fallback
285+
mid-chain and the calls after it run on the new value:
286286

287287
```php
288288
echo SmartString::new("cat")->add(10)->ifNull(0)->add(5); // 5 (recovered mid-chain)

docs/troubleshooting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The other cause is a database that already contains encoded text, usually
8282
from a form handler that encoded values before saving them. Whatever is
8383
stored encoded gets encoded again on output. Save the raw text instead,
8484
then clean up the existing rows with a one-time
85-
`htmlspecialchars_decode($value, ENT_QUOTES | ENT_HTML5)` (the flags
85+
`htmlspecialchars_decode($value, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5)` (the flags
8686
matter; the defaults leave `&apos;` behind).
8787

8888
### or() kept my zero / isEmpty() lost my zero

tests/Integration/DocsExamplesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function testDateFormat(): void
245245
$this->assertSame('May 15th, 2026', (string)$date->dateFormat());
246246

247247
date_default_timezone_set('America/Vancouver'); // the docs show the timestamp example with T = PDT
248-
$this->assertSame('2023-05-15 PDT', (string)SmartString::new(1684159800)->dateFormat('Y-m-d T'));
248+
$this->assertSame('2026-05-15 PDT', (string)SmartString::new(1778866200)->dateFormat('Y-m-d T'));
249249

250250
$invalid = SmartString::new("not a date");
251251
$this->assertSame('Date not set', (string)$invalid->dateFormat()->or("Date not set"));

0 commit comments

Comments
 (0)