Skip to content

Commit de0f6cb

Browse files
committed
Update how we stringify parameters
The `PlaceholderFormatter` did not call the modifiers when the value was a string, and the pipe was null. That’s not up to that class to decide, but to the modifiers themselves to decide whether they can handle the value of the parameter or not. Another issue was that the `StringifyModifier` did not stringify values that were strings containing control characters. That led to cryptic formatting, since we couldn’t see those characters when the message was displayed. Assisted-by: OpenCode (GLM-4.6)
1 parent 9fcce9a commit de0f6cb

5 files changed

Lines changed: 22 additions & 12 deletions

File tree

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
"type": "library",
44
"description": "A powerful and flexible way of formatting and transforming strings",
55
"require": {
6-
"symfony/polyfill-mbstring": "^1.33",
76
"php": "^8.5",
7+
"ext-ctype": "*",
88
"respect/stringifier": "^3.0",
9+
"symfony/polyfill-mbstring": "^1.33",
910
"symfony/translation-contracts": "^3.6"
1011
},
1112
"suggest": {
1213
"symfony/translation": "For translation support in TransModifier (^6.0|^7.0)"
1314
},
1415
"require-dev": {
15-
"phpunit/phpunit": "^12.5",
16-
"phpstan/phpstan": "^2.1",
1716
"phpstan/extension-installer": "^1.4",
17+
"phpstan/phpstan": "^2.1",
1818
"phpstan/phpstan-deprecation-rules": "^2.0",
1919
"phpstan/phpstan-phpunit": "^2.0",
20+
"phpunit/phpunit": "^12.5",
2021
"respect/coding-standard": "^5.0",
2122
"symfony/translation": "^6.0|^7.0"
2223
},
@@ -48,6 +49,7 @@
4849
"allow-plugins": {
4950
"phpstan/extension-installer": true,
5051
"dealerdirect/phpcodesniffer-composer-installer": true
51-
}
52+
},
53+
"sort-packages": true
5254
}
5355
}

src/Modifiers/StringifyModifier.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Respect\Stringifier\HandlerStringifier;
99
use Respect\Stringifier\Stringifier;
1010

11+
use function ctype_cntrl;
1112
use function is_string;
1213
use function sprintf;
1314

@@ -27,7 +28,7 @@ public function modify(mixed $value, string|null $pipe): string
2728
throw new InvalidModifierPipeException(sprintf('"%s" is not recognized as a valid pipe', $pipe));
2829
}
2930

30-
if (is_string($value)) {
31+
if (is_string($value) && !ctype_cntrl($value)) {
3132
return $value;
3233
}
3334

src/PlaceholderFormatter.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Respect\StringFormatter\Modifiers\TransModifier;
1111

1212
use function array_key_exists;
13-
use function is_string;
1413
use function preg_replace_callback;
1514

1615
final readonly class PlaceholderFormatter implements Formatter
@@ -57,11 +56,6 @@ private function processPlaceholder(array $matches, array $parameters): string
5756
return $placeholder;
5857
}
5958

60-
$value = $parameters[$name];
61-
if (is_string($value) && $pipe === null) {
62-
return $value;
63-
}
64-
65-
return $this->modifier->modify($value, $pipe);
59+
return $this->modifier->modify($parameters[$name], $pipe);
6660
}
6761
}

tests/Unit/Modifiers/StringifyModifierTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static function providerForModifiableValues(): array
3131
'object value' => [(object) ['key' => 'value']],
3232
'empty array' => [[]],
3333
'resource' => [fopen('php://memory', 'r')],
34+
'control characters' => ["\n\r\t"],
3435
];
3536
}
3637

tests/Unit/PlaceholderFormatterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,18 @@ public function itShouldAcceptCustomModifier(): void
402402
self::assertSame($expected, $actual);
403403
}
404404

405+
#[Test]
406+
public function itShouldAlwaysCallModifierWhenParameterExists(): void
407+
{
408+
$modifier = new TestingModifier('modified');
409+
$placeholder = 'name';
410+
411+
$formatter = new PlaceholderFormatter([$placeholder => 'John'], $modifier);
412+
$actual = $formatter->format(sprintf('Hello {{%s}}!', $placeholder));
413+
414+
self::assertSame('Hello modified!', $actual);
415+
}
416+
405417
/** @param array<string, mixed> $parameters */
406418
#[Test]
407419
#[DataProvider('providerForRealWorldUseCases')]

0 commit comments

Comments
 (0)