4242use function is_callable ;
4343use function is_int ;
4444use function is_numeric ;
45+ use function is_string ;
4546use function preg_match ;
4647use function sprintf ;
4748
@@ -97,8 +98,6 @@ public function format(string $pattern, array $values = []): string
9798 }
9899
99100 /**
100- * @param array<array-key, float | int | string | callable(string):string> $values
101- *
102101 * @throws Parser\Exception\IllegalParserUsageException
103102 * @throws Parser\Exception\InvalidArgumentException
104103 * @throws Parser\Exception\InvalidOffsetException
@@ -108,16 +107,27 @@ public function format(string $pattern, array $values = []): string
108107 * @throws Parser\Exception\UnableToParseMessageException
109108 * @throws UnableToFormatMessageException
110109 * @throws CollectionMismatchException
110+ *
111+ * @psalm-param array<array-key, float | int | string | callable(string=):string> $values
111112 */
112113 private function applyPreprocessing (string $ pattern , array &$ values = []): string
113114 {
114- $ callbacks = array_filter ($ values , fn ($ value ): bool => is_callable ($ value ));
115+ /** @var array<array-key, callable(string=):string> $callbacks */
116+ $ callbacks = array_filter ($ values , fn ($ value ): bool => !is_string ($ value ) && is_callable ($ value ));
115117
116118 // Remove the callbacks from the values, since we will use them below.
117119 foreach (array_keys ($ callbacks ) as $ key ) {
118120 unset($ values [$ key ]);
119121 }
120122
123+ /**
124+ * This is to satisfy static analysis. At this point, $values should
125+ * not contain any callables.
126+ *
127+ * @var array<array-key, float | int | string> $valuesWithoutCallables
128+ */
129+ $ valuesWithoutCallables = &$ values ;
130+
121131 $ parserOptions = new Parser \Options ();
122132 $ parserOptions ->shouldParseSkeletons = true ;
123133
@@ -130,15 +140,16 @@ private function applyPreprocessing(string $pattern, array &$values = []): strin
130140
131141 assert ($ parsed ->val instanceof Parser \Type \ElementCollection);
132142
133- return (new Printer ())->printAst ($ this ->processAst ($ parsed ->val , $ callbacks , $ values ));
143+ return (new Printer ())->printAst ($ this ->processAst ($ parsed ->val , $ callbacks , $ valuesWithoutCallables ));
134144 }
135145
136146 /**
137- * @param array<array-key, callable(string):string> $callbacks
138- * @param array<array-key, float | int | string | callable(string):string> $values
147+ * @param array<array-key, float | int | string> $values
139148 *
140149 * @throws CollectionMismatchException
141150 * @throws UnableToFormatMessageException
151+ *
152+ * @psalm-param array<array-key, callable(string=):string> $callbacks
142153 */
143154 private function processAst (
144155 Parser \Type \ElementCollection $ ast ,
@@ -179,11 +190,12 @@ private function processAst(
179190 }
180191
181192 /**
182- * @param array<array-key, callable(string):string> $callbacks
183- * @param array<array-key, float | int | string | callable(string):string> $values
193+ * @param array<array-key, float | int | string> $values
184194 *
185195 * @throws CollectionMismatchException
186196 * @throws UnableToFormatMessageException
197+ *
198+ * @psalm-param array<array-key, callable(string=):string> $callbacks
187199 */
188200 private function processTagElement (
189201 Parser \Type \TagElement $ tagElement ,
@@ -246,7 +258,7 @@ private function processTagElement(
246258 * @link https://tc39.es/ecma402/#sec-partitionnumberpattern
247259 * @link https://formatjs.io/docs/core-concepts/icu-syntax/#number-type
248260 *
249- * @param array<array-key, float | int | string | callable(string):string > $values
261+ * @param array<array-key, float | int | string> $values
250262 */
251263 private function processNumberElement (
252264 Parser \Type \NumberElement $ numberElement ,
@@ -267,10 +279,10 @@ private function processNumberElement(
267279 }
268280
269281 /**
270- * @param array<array-key, callable(string):string> $callbacks
271- *
272282 * @throws CollectionMismatchException
273283 * @throws UnableToFormatMessageException
284+ *
285+ * @psalm-param array<array-key, callable(string=):string> $callbacks
274286 */
275287 private function processLiteralElement (
276288 Parser \Type \LiteralElement $ literalElement ,
0 commit comments