12
12
namespace FFI \Preprocessor \Internal \Runtime ;
13
13
14
14
use FFI \Contracts \Preprocessor \Directive \DirectiveInterface ;
15
+ use FFI \Contracts \Preprocessor \Directive \FunctionLikeDirectiveInterface ;
15
16
use FFI \Contracts \Preprocessor \Directive \RepositoryInterface ;
16
17
use FFI \Preprocessor \Exception \DirectiveEvaluationException ;
17
18
use FFI \Preprocessor \Exception \PreprocessException ;
@@ -84,9 +85,7 @@ public function replace(string $body, int $ctx = self::CTX_SOURCE): string
84
85
$ body = $ this ->replaceDefinedExpression ($ body );
85
86
}
86
87
87
- $ body = $ this ->replaceDefinedDirectives ($ body );
88
-
89
- return $ body ;
88
+ return $ this ->replaceDefinedDirectives ($ body );
90
89
}
91
90
92
91
/**
@@ -110,7 +109,10 @@ public function replace(string $body, int $ctx = self::CTX_SOURCE): string
110
109
*/
111
110
private function replaceDefinedExpression (string $ body ): string
112
111
{
113
- $ lookup = fn (array $ matches ) => $ this ->directives ->defined ($ matches [1 ]) ? 'true ' : 'false ' ;
112
+ $ lookup = fn (array $ matches ): string =>
113
+ /** @psalm-suppress MixedArgument */
114
+ $ this ->directives ->defined ($ matches [1 ]) ? 'true ' : 'false '
115
+ ;
114
116
115
117
return \preg_replace_callback (self ::PCRE_DEFINED , $ lookup , $ body );
116
118
}
@@ -120,21 +122,22 @@ private function replaceDefinedExpression(string $body): string
120
122
*
121
123
* @param string $body
122
124
* @return string
125
+ * @psalm-suppress MixedInferredReturnType
123
126
*/
124
127
private function replaceDefinedDirectives (string $ body ): string
125
128
{
126
129
$ stream = $ this ->findAndReplace ($ body );
127
130
128
131
while ($ stream ->valid ()) {
129
- [$ name , $ arguments ] = [$ stream ->key (), $ stream ->current ()];
130
-
131
132
try {
132
- $ stream ->send ($ this ->execute ($ name , $ arguments ));
133
+ /** @psalm-suppress MixedArgument */
134
+ $ stream ->send ($ this ->execute ($ stream ->key (), $ stream ->current ()));
133
135
} catch (\Throwable $ e ) {
134
136
$ stream ->throw ($ e );
135
137
}
136
138
}
137
139
140
+ /** @psalm-suppress MixedReturnStatement */
138
141
return $ stream ->getReturn ();
139
142
}
140
143
@@ -201,7 +204,8 @@ private function findDirectiveAndReplace(string $name, DirectiveInterface $direc
201
204
// and means that do not need to do a lookahead to read
202
205
// additional directive arguments.
203
206
//
204
- $ isSimpleDirective = $ directive ->getMaxArgumentsCount () === 0 ;
207
+ $ isSimpleDirective = !$ directive instanceof FunctionLikeDirectiveInterface
208
+ || $ directive ->getMaxArgumentsCount () === 0 ;
205
209
206
210
$ coroutine = $ this ->findDirectiveAndUpdateBody ($ name , $ body );
207
211
@@ -263,11 +267,11 @@ private function findDirectiveAndReplace(string $name, DirectiveInterface $direc
263
267
* }
264
268
* </code>
265
269
*
266
- * @psalm-return \Generator<int, int, string, string>
267
- *
268
270
* @param string $name
269
271
* @param string $body
270
272
* @return \Generator
273
+ * @psalm-return \Generator<int, int, string, string>
274
+ * @psalm-suppress MixedReturnTypeCoercion
271
275
*/
272
276
private function findDirectiveAndUpdateBody (string $ name , string $ body ): \Generator
273
277
{
@@ -285,6 +289,7 @@ private function findDirectiveAndUpdateBody(string $name, string $body): \Genera
285
289
continue ;
286
290
}
287
291
292
+ /** @psalm-suppress RedundantConditionGivenDocblockType */
288
293
if (\is_string ($ replacement = yield $ offset => $ offset + $ length )) {
289
294
$ body = $ replacement ;
290
295
}
@@ -444,7 +449,7 @@ private function createTokenForSource(string $name, string $body, int $from, int
444
449
* Accepts the name of a directive and its arguments, and returns the
445
450
* result of executing that directive.
446
451
*
447
- * @param string $name
452
+ * @param non-empty- string $name
448
453
* @param array $arguments
449
454
* @return string
450
455
* @throws DirectiveEvaluationException
0 commit comments