Completed
Pull Request — master (#457)
by Claus
02:19
created

Sequencer::sequenceQuotedNode()   C

Complexity

Conditions 14
Paths 17

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 17
nop 2
dl 0
loc 67
rs 5.7333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace TYPO3Fluid\Fluid\Core\Parser;
5
6
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ArrayNode;
7
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression\ExpressionException;
8
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\NodeInterface;
9
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ObjectAccessorNode;
10
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
11
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode;
12
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
13
use TYPO3Fluid\Fluid\Core\ViewHelper\ArgumentDefinition;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperResolver;
15
16
/**
17
 * Sequencer for Fluid syntax
18
 *
19
 * Uses a NoRewindIterator around a sequence of byte values to
20
 * iterate over each syntax-relevant character and determine
21
 * which nodes to create.
22
 *
23
 * Passes the outer iterator between functions that perform the
24
 * iterations. Since the iterator is a NoRewindIterator it will
25
 * not be reset before the start of each loop - meaning that
26
 * when it is passed to a new function, that function continues
27
 * from the last touched index in the byte sequence.
28
 *
29
 * The circumstances around "break or return" in the switches is
30
 * very, very important to understand in context of how iterators
31
 * work. Returning does not advance the iterator like breaking
32
 * would and this causes a different position in the byte sequence
33
 * to be experienced in the method that uses the return value (it
34
 * sees the index of the symbol which terminated the expression,
35
 * not the next symbol after that).
36
 */
37
class Sequencer
38
{
39
    /**
40
     * @var RenderingContextInterface
41
     */
42
    protected $renderingContext;
43
44
    /**
45
     * @var ParsingState
46
     */
47
    protected $state;
48
49
    /**
50
     * @var Contexts
51
     */
52
    protected $contexts;
53
54
    /**
55
     * @var Source
56
     */
57
    protected $source;
58
59
    /**
60
     * @var Splitter
61
     */
62
    protected $splitter;
63
64
    /**
65
     * @var Configuration
66
     */
67
    protected $configuration;
68
69
    /**
70
     * @var ViewHelperResolver
71
     */
72
    protected $resolver;
73
74
    /**
75
     * Whether or not the escaping interceptors are active
76
     *
77
     * @var boolean
78
     */
79
    protected $escapingEnabled = true;
80
81
    public function __construct(
82
        RenderingContextInterface $renderingContext,
83
        ParsingState $state,
84
        Contexts $contexts,
85
        Source $source
86
    ) {
87
        $this->renderingContext = $renderingContext;
88
        $this->resolver = $renderingContext->getViewHelperResolver();
89
        $this->configuration = $renderingContext->buildParserConfiguration();
90
        $this->state = clone $state;
91
        $this->contexts = $contexts;
92
        $this->source = $source;
93
        $this->splitter = new Splitter($this->source, $this->contexts);
94
    }
95
96
    public function sequence(): ParsingState
97
    {
98
        #$sequence = $this->splitter->parse();
99
100
        // Please note: repeated calls to $this->state->getTopmostNodeFromStack() are indeed intentional. That method may
101
        // return different nodes at different times depending on what has occurred in other methods! Only the places
102
        // where $node is actually extracted is it (by design) safe to do so. DO NOT REFACTOR!
103
        // It is *also* intentional that this switch has no default case. The root context is very specific and will
104
        // only apply when the splitter is actually in root, which means there is no chance of it yielding an unexpected
105
        // character (because that implies a method called by this method already threw a SequencingException).
106
        foreach ($this->splitter->sequence as $symbol => $captured) {
107
            switch ($symbol) {
108
                case Splitter::BYTE_INLINE:
109
                    $node = $this->state->getNodeFromStack();
110
                    if ($this->splitter->index > 1 && $this->source->bytes[$this->splitter->index - 1] === Splitter::BYTE_BACKSLASH) {
111
                        $node->addChildNode(new TextNode(substr($captured, 0, -1) . '{'));
112
                        break;
113
                    }
114
                    if ($captured !== null) {
115
                        $node->addChildNode(new TextNode($captured));
116
                    }
117
                    $node->addChildNode($this->sequenceInlineNodes(false));
118
                    $this->splitter->switch($this->contexts->root);
119
                    break;
120
121
                case Splitter::BYTE_TAG:
122
                    if ($captured !== null) {
123
                        $this->state->getNodeFromStack()->addChildNode(new TextNode($captured));
124
                    }
125
126
                    $childNode = $this->sequenceTagNode();
127
                    $this->splitter->switch($this->contexts->root);
128
                    if ($childNode) {
129
                        $this->state->getNodeFromStack()->addChildNode($childNode);
130
                    }
131
                    break;
132
133
                case Splitter::BYTE_NULL:
134
                    if ($captured !== null) {
135
                        $this->state->getNodeFromStack()->addChildNode(new TextNode($captured));
136
                    }
137
                    break;
138
            }
139
        }
140
141
        return $this->state;
142
    }
143
144
    /**
145
     * @return NodeInterface|null
146
     */
147
    protected function sequenceTagNode(): ?NodeInterface
148
    {
149
        $arguments = [];
150
        $definitions = null;
151
        $text = '<';
152
        $namespace = null;
153
        $method = null;
154
        $bytes = &$this->source->bytes;
155
        $node = new RootNode();
156
        $selfClosing = false;
157
        $closing = false;
158
        #$escapingEnabledBackup = $this->escapingEnabled;
159
160
        $interceptionPoint = InterceptorInterface::INTERCEPT_OPENING_VIEWHELPER;
161
162
        $this->splitter->switch($this->contexts->tag);
163
        $this->splitter->sequence->next();
164
        foreach ($this->splitter->sequence as $symbol => $captured) {
165
            $text .= $captured;
166
            switch ($symbol) {
167
                case Splitter::BYTE_INLINE:
168
                    $contextBefore = $this->splitter->context;
169
                    $collected = $this->sequenceInlineNodes(isset($namespace) && isset($method));
170
                    $node->addChildNode(new TextNode($text));
171
                    $node->addChildNode($collected);
172
                    $text = '';
173
                    $this->splitter->switch($contextBefore);
174
                    break;
175
176
                case Splitter::BYTE_SEPARATOR_EQUALS:
177
                    $key = $captured;
178
                    if ($definitions !== null && !isset($definitions[$key])) {
179
                        throw $this->splitter->createUnsupportedArgumentError($key, $definitions);
180
                    }
181
                    break;
182
183
                case Splitter::BYTE_QUOTE_DOUBLE:
184
                case Splitter::BYTE_QUOTE_SINGLE:
185
                    $text .= chr($symbol);
186
                    if (!isset($key)) {
187
                        throw $this->splitter->createErrorAtPosition('Quoted value without a key is not allowed in tags', 1558952412);
188
                    } else {
189
                        $arguments[$key] = $this->sequenceQuotedNode(0, isset($namespace) && isset($method))->flatten(true);
190
                        $key = null;
191
                    }
192
                    break;
193
194
                case Splitter::BYTE_TAG_CLOSE:
195
                    $method = $method ?? $captured;
196
                    $text .= '/';
197
                    $closing = true;
198
                    $selfClosing = $bytes[$this->splitter->index - 1] !== Splitter::BYTE_TAG;
199
                    $interceptionPoint = InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER;
200
                    if ($this->splitter->context->context === Context::CONTEXT_ATTRIBUTES && $captured !== null) {
201
                        // We are still capturing arguments and the last yield contained a value. Null-coalesce key
202
                        // with captured string so object accessor becomes key name (ECMA shorthand literal)
203
                        $arguments[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
204
                        $key = null;
205
                    }
206
                    break;
207
208
                case Splitter::BYTE_SEPARATOR_COLON:
209
                    $text .= ':';
210
                    $namespace = $namespace ?? $captured;
211
                    break;
212
213
                case Splitter::BYTE_TAG_END:
214
                    $text .= '>';
215
                    $method = $method ?? $captured;
216
217
                    if (!isset($namespace) || !isset($method) || $this->splitter->context->context === Context::CONTEXT_DEAD || $this->resolver->isNamespaceIgnored($namespace)) {
218
                        return $node->addChildNode(new TextNode($text))->flatten();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $node->addChildNo...ode($text))->flatten(); (null|integer|double|stri...yntaxTree\NodeInterface) is incompatible with the return type documented by TYPO3Fluid\Fluid\Core\Pa...uencer::sequenceTagNode of type TYPO3Fluid\Fluid\Core\Pa...Tree\NodeInterface|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
219
                    }
220
221
                    try {
222
                        $expectedClass = $this->resolver->resolveViewHelperClassName($namespace, $method);
223
                    } catch (\TYPO3Fluid\Fluid\Core\Exception $exception) {
224
                        throw $this->splitter->createErrorAtPosition($exception->getMessage(), $exception->getCode());
225
                    }
226
227
                    if ($closing && !$selfClosing) {
228
                        // Closing byte was more than two bytes back, meaning the tag is NOT self-closing, but is a
229
                        // closing tag for a previously opened+stacked node. Finalize the node now.
230
                        $closesNode = $this->state->popNodeFromStack();
231
                        if ($closesNode instanceof $expectedClass) {
232
                            $arguments = $closesNode->getParsedArguments();
233
                            $viewHelperNode = $closesNode;
234
                        } else {
235
                            throw $this->splitter->createErrorAtPosition(
236
                                sprintf(
237
                                    'Mismatched closing tag. Expecting: %s:%s (%s). Found: (%s).',
238
                                    $namespace,
239
                                    $method,
240
                                    $expectedClass,
241
                                    get_class($closesNode)
242
                                ),
243
                                1557700789
244
                            );
245
                        }
246
                    }
247
248
                    if ($this->splitter->context->context === Context::CONTEXT_ATTRIBUTES && $captured !== null) {
249
                        // We are still capturing arguments and the last yield contained a value. Null-coalesce key
250
                        // with captured string so object accessor becomes key name (ECMA shorthand literal)
251
                        $arguments[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
252
                    }
253
254
                    $viewHelperNode = $viewHelperNode ?? $this->resolver->createViewHelperInstanceFromClassName($expectedClass);
255
                    #$this->escapingEnabled = $escapingEnabledBackup;
256
257
                    if (!$closing) {
258
                        $this->callInterceptor($viewHelperNode, $interceptionPoint);
259
                        $viewHelperNode->setParsedArguments($arguments);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3Fluid\Fluid\Core\Pa...yntaxTree\NodeInterface as the method setParsedArguments() does only exist in the following implementations of said interface: TYPO3Fluid\FluidExample\...elpers\CustomViewHelper, TYPO3Fluid\Fluid\Core\Vi...ractConditionViewHelper, TYPO3Fluid\Fluid\Core\Vi...tractTagBasedViewHelper, TYPO3Fluid\Fluid\Core\Vi...lper\AbstractViewHelper, TYPO3Fluid\Fluid\ViewHelpers\AliasViewHelper, TYPO3Fluid\Fluid\ViewHel...Cache\DisableViewHelper, TYPO3Fluid\Fluid\ViewHel...\Cache\StaticViewHelper, TYPO3Fluid\Fluid\ViewHel...\Cache\WarmupViewHelper, TYPO3Fluid\Fluid\ViewHelpers\CaseViewHelper, TYPO3Fluid\Fluid\ViewHelpers\CommentViewHelper, TYPO3Fluid\Fluid\ViewHelpers\CountViewHelper, TYPO3Fluid\Fluid\ViewHelpers\CycleViewHelper, TYPO3Fluid\Fluid\ViewHelpers\DebugViewHelper, TYPO3Fluid\Fluid\ViewHelpers\DefaultCaseViewHelper, TYPO3Fluid\Fluid\ViewHelpers\ElseViewHelper, TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper, TYPO3Fluid\Fluid\ViewHel...\Format\CdataViewHelper, TYPO3Fluid\Fluid\ViewHel...lspecialcharsViewHelper, TYPO3Fluid\Fluid\ViewHel...Format\PrintfViewHelper, TYPO3Fluid\Fluid\ViewHelpers\Format\RawViewHelper, TYPO3Fluid\Fluid\ViewHelpers\GroupedForViewHelper, TYPO3Fluid\Fluid\ViewHelpers\IfViewHelper, TYPO3Fluid\Fluid\ViewHelpers\InlineViewHelper, TYPO3Fluid\Fluid\ViewHelpers\LayoutViewHelper, TYPO3Fluid\Fluid\ViewHelpers\OrViewHelper, TYPO3Fluid\Fluid\ViewHelpers\RenderViewHelper, TYPO3Fluid\Fluid\ViewHelpers\SectionViewHelper, TYPO3Fluid\Fluid\ViewHelpers\SpacelessViewHelper, TYPO3Fluid\Fluid\ViewHelpers\SwitchViewHelper, TYPO3Fluid\Fluid\ViewHelpers\ThenViewHelper, TYPO3Fluid\Fluid\ViewHelpers\VariableViewHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
260
                        $this->state->pushNodeToStack($viewHelperNode);
261
                        return null;
262
                    }
263
264
                    $viewHelperNode = $viewHelperNode->postParse($arguments, $this->state, $this->renderingContext);
265
266
                    return $viewHelperNode;
267
268
                case Splitter::BYTE_WHITESPACE_TAB:
269
                case Splitter::BYTE_WHITESPACE_RETURN:
270
                case Splitter::BYTE_WHITESPACE_EOL:
271
                case Splitter::BYTE_WHITESPACE_SPACE:
272
                    if ($this->splitter->context->context === Context::CONTEXT_ATTRIBUTES) {
273
                        if ($captured !== null) {
274
                            $arguments[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
275
                            $key = null;
276
                        }
277
                    } else {
278
                        $text .= chr($symbol);
279
                        if (isset($namespace)) {
280
                            $method = $captured;
281
282
                            $this->escapingEnabled = false;
283
                            $viewHelperNode = $this->resolver->createViewHelperInstance($namespace, $method);
284
                            $definitions = $viewHelperNode->prepareArguments();
285
286
                            // A whitespace character, in tag context, means the beginning of an array sequence (which may
287
                            // or may not contain any items; the next symbol may be a tag end or tag close). We sequence the
288
                            // arguments array and create a ViewHelper node.
289
                            $this->splitter->switch($this->contexts->attributes);
290
                            break;
291
                        }
292
293
                        // A whitespace before a colon means the tag is not a namespaced tag. We will ignore everything
294
                        // inside this tag, except for inline syntax, until the tag ends. For this we use a special,
295
                        // limited variant of the root context where instead of scanning for "<" we scan for ">".
296
                        // We continue in this same loop because it still matches the potential symbols being yielded.
297
                        // Most importantly: this new reduced context will NOT match a colon which is the trigger symbol
298
                        // for a ViewHelper tag.
299
                        $this->splitter->switch($this->contexts->dead);
300
                    }
301
                    break;
302
            }
303
        }
304
305
        // This case on the surface of it, belongs as "default" case in the switch above. However, the only case that
306
        // would *actually* produce this error, is if the splitter reaches EOF (null byte) symbol before the tag was
307
        // closed. Literally every other possible error type will be thrown as more specific exceptions (e.g. invalid
308
        // argument, missing key, wrong quotes, bad inline and *everything* else with the exception of EOF). Even a
309
        // stray null byte would not be caught here as null byte is not part of the symbol collection for "tag" context.
310
        throw $this->splitter->createErrorAtPosition('Unexpected token in tag sequencing', 1557700786);
311
    }
312
313
    /**
314
     * @param bool $allowArray
315
     * @return NodeInterface
316
     */
317
    protected function sequenceInlineNodes(bool $allowArray = true): NodeInterface
318
    {
319
        $text = '{';
320
        $node = null;
321
        $key = null;
322
        $namespace = null;
323
        $method = null;
324
        $potentialAccessor = null;
325
        $callDetected = false;
326
        $hasPass = false;
327
        $hasColon = null;
328
        $hasWhitespace = false;
329
        $isArray = false;
330
        $array = [];
331
        $arguments = [];
332
        $ignoredEndingBraces = 0;
333
        $countedEscapes = 0;
334
335
        $this->splitter->switch($this->contexts->inline);
336
        $this->splitter->sequence->next();
337
        foreach ($this->splitter->sequence as $symbol => $captured) {
338
            $text .= $captured;
339
            switch ($symbol) {
340
                case Splitter::BYTE_BACKSLASH:
341
                    // Increase the number of counted escapes (is passed to sequenceNode() in the "QUOTE" cases and reset
342
                    // after the quoted string is extracted).
343
                    ++$countedEscapes;
344
                    break;
345
346
                case Splitter::BYTE_ARRAY_START:
347
348
                    $text .= chr($symbol);
349
                    $isArray = $allowArray;
350
351
                    #ArrayStart:
352
                    // Sequence the node. Pass the "use numeric keys?" boolean based on the current byte. Only array
353
                    // start creates numeric keys. Inline start with keyless values creates ECMA style {foo:foo, bar:bar}
354
                    // from {foo, bar}.
355
                    $array[$key ?? $captured ?? 0] = $node = $this->sequenceArrayNode(null, $symbol === Splitter::BYTE_ARRAY_START);
356
                    $this->splitter->switch($this->contexts->inline);
357
                    unset($key);
358
                    break;
359
360
                case Splitter::BYTE_INLINE:
361
                    // Encountering this case can mean different things: sub-syntax like {foo.{index}} or array, depending
362
                    // on presence of either a colon or comma before the inline. In protected mode it is simply added.
363
                    $text .= '{';
364
                    if (!$hasWhitespace && $text !== '{{') {
365
                        // Most likely, a nested object accessor syntax e.g. {foo.{bar}} - enter protected context since
366
                        // these accessors do not allow anything other than additional nested accessors.
367
                        $this->splitter->switch($this->contexts->accessor);
368
                        ++$ignoredEndingBraces;
369
                    } elseif ($this->splitter->context->context === Context::CONTEXT_PROTECTED) {
370
                        // Ignore one ending additional curly brace. Subtracted in the BYTE_INLINE_END case below.
371
                        // The expression in this case looks like {{inline}.....} and we capture the curlies.
372
                        $potentialAccessor .= $captured;
373
                        ++$ignoredEndingBraces;
374
                    } elseif ($allowArray || $isArray) {
375
                        $isArray = true;
376
                        $captured = $key ?? $captured ?? $potentialAccessor;
377
                        // This is a sub-syntax following a colon - meaning it is an array.
378
                        if ($captured !== null) {
379
                            #goto ArrayStart;
380
                            $array[$key ?? $captured ?? 0] = $node = $this->sequenceArrayNode(null, $symbol === Splitter::BYTE_ARRAY_START);
381
                            $this->splitter->switch($this->contexts->inline);
382
                        }
383
                    } else {
384
                        $childNodeToAdd = $this->sequenceInlineNodes($allowArray);
385
                        $node = isset($node) ? $node->addChildNode($childNodeToAdd) : (new RootNode())->addChildNode($childNodeToAdd);
386
                    }
387
                    break;
388
389
                case Splitter::BYTE_MINUS:
390
                    $text .= '-';
391
                    break;
392
393
                // Backtick may be encountered in two different contexts: normal inline context, in which case it has
394
                // the same meaning as any quote and causes sequencing of a quoted string. Or protected context, in
395
                // which case it also sequences a quoted node but appends the result instead of assigning to array.
396
                // Note that backticks do not support escapes (they are a new feature that does not require escaping).
397
                case Splitter::BYTE_BACKTICK:
398
                    if ($this->splitter->context->context === Context::CONTEXT_PROTECTED) {
399
                        $node->addChildNode(new TextNode($text));
400
                        $node->addChildNode($this->sequenceQuotedNode()->flatten());
401
                        $text = '';
402
                        break;
403
                    }
404
                // Fallthrough is intentional: if not in protected context, consider the backtick a normal quote.
405
406
                // Case not normally countered in straight up "inline" context, but when encountered, means we have
407
                // explicitly found a quoted array key - and we extract it.
408
                case Splitter::BYTE_QUOTE_SINGLE:
409
                case Splitter::BYTE_QUOTE_DOUBLE:
410
                    if (!$allowArray) {
411
                        $text .= chr($symbol);
412
                        break;
413
                    }
414
                    if (isset($key)) {
415
                        $array[$key] = $this->sequenceQuotedNode($countedEscapes)->flatten(true);
416
                        $key = null;
417
                    } else {
418
                        $key = $this->sequenceQuotedNode($countedEscapes)->flatten(true);
419
                    }
420
                    $countedEscapes = 0;
421
                    $isArray = $allowArray;
422
                    break;
423
424
                case Splitter::BYTE_SEPARATOR_COMMA:
425
                    if (!$allowArray) {
426
                        $text .= ',';
427
                        break;
428
                    }
429
                    if (isset($captured)) {
430
                        $array[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
431
                    }
432
                    $key = null;
433
                    $isArray = $allowArray;
434
                    break;
435
436
                case Splitter::BYTE_SEPARATOR_EQUALS:
437
                    $text .= '=';
438
                    if (!$allowArray) {
439
                        $node = new RootNode();
440
                        $this->splitter->switch($this->contexts->protected);
441
                        break;
442
                    }
443
                    $key = $captured;
444
                    $isArray = $allowArray;
445
                    break;
446
447
                case Splitter::BYTE_SEPARATOR_COLON:
448
                    $text .= ':';
449
                    $hasColon = true;
450
                    $namespace = $captured;
451
                    $key = $key ?? $captured;
452
                    $isArray = $isArray || ($allowArray && is_numeric($key));
453
                    break;
454
455
                case Splitter::BYTE_WHITESPACE_SPACE:
456
                case Splitter::BYTE_WHITESPACE_EOL:
457
                case Splitter::BYTE_WHITESPACE_RETURN:
458
                case Splitter::BYTE_WHITESPACE_TAB:
459
                    // If we already collected some whitespace we must enter protected context.
460
                    $text .= $this->source->source[$this->splitter->index - 1];
461
                    if ($hasWhitespace && !$hasPass && !$allowArray) {
462
                        // Protection mode: this very limited context does not allow tags or inline syntax, and will
463
                        // protect things like CSS and JS - and will only enter a more reactive context if encountering
464
                        // the backtick character, meaning a quoted string will be sequenced. This backtick-quoted
465
                        // string can then contain inline syntax like variable accessors.
466
                        $node = $node ?? new RootNode();
467
                        $this->splitter->switch($this->contexts->protected);
468
                        break;
469
                    }
470
                    $key = $key ?? $captured;
471
                    $hasWhitespace = true;
472
                    $isArray = $allowArray && ($hasColon ?? $isArray ?? is_numeric($captured));
473
                    $potentialAccessor = ($potentialAccessor ?? $captured);
474
                    break;
475
476
                case Splitter::BYTE_TAG_END:
477
                case Splitter::BYTE_PIPE:
478
                    // If there is an accessor on the left side of the pipe and $node is not defined, we create $node
479
                    // as an object accessor. If $node already exists we do nothing (and expect the VH trigger, the
480
                    // parenthesis start case below, to add $node as childnode and create a new $node).
481
                    $hasPass = true;
482
                    $isArray = $allowArray;
483
                    $callDetected = false;
484
                    $potentialAccessor = $potentialAccessor ?? $captured;
485
                    $text .=  $this->source->source[$this->splitter->index - 1];
486
                    if (isset($potentialAccessor)) {
487
                        $childNodeToAdd = new ObjectAccessorNode($potentialAccessor);
488
                        $node = isset($node) ? $node->addChildNode($childNodeToAdd) : $childNodeToAdd; //$node ?? (is_numeric($potentialAccessor) ? $potentialAccessor + 0 : new ObjectAccessorNode($potentialAccessor));
489
                    }
490
                    //!isset($potentialAccessor) ?: ($node = ($node ?? $this->createObjectAccessorNodeOrRawValue($potentialAccessor)));
491
                    unset($namespace, $method, $potentialAccessor, $key);
492
                    break;
493
494
                case Splitter::BYTE_PARENTHESIS_START:
495
                    $isArray = false;
496
                    // Special case: if a parenthesis start was preceded by whitespace but had no pass operator we are
497
                    // not dealing with a ViewHelper call and will continue the sequencing, grabbing the parenthesis as
498
                    // part of the expression.
499
                    $text .= '(';
500
                    if (!$hasColon || ($hasWhitespace && !$hasPass)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hasColon of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
501
                        $this->splitter->switch($this->contexts->protected);
502
                        unset($namespace, $method);
503
                        break;
504
                    }
505
506
                    $callDetected = true;
507
                    $method = $captured;
508
                    $childNodeToAdd = $node;
509
                    try {
510
                        $node = $this->resolver->createViewHelperInstance($namespace, $method);
511
                        $definitions = $node->prepareArguments();
512
                    } catch (\TYPO3Fluid\Fluid\Core\Exception $exception) {
513
                        throw $this->splitter->createErrorAtPosition($exception->getMessage(), $exception->getCode());
514
                    }
515
                    $this->splitter->switch($this->contexts->array);
516
                    $arguments = $this->sequenceArrayNode($definitions)->getInternalArray();
517
                    $this->splitter->switch($this->contexts->inline);
518
                    if ($childNodeToAdd) {
519
                        $escapingEnabledBackup = $this->escapingEnabled;
520
                        $this->escapingEnabled = (bool)$node->isChildrenEscapingEnabled();
521
                        if ($childNodeToAdd instanceof ObjectAccessorNode) {
522
                            $this->callInterceptor($childNodeToAdd, InterceptorInterface::INTERCEPT_OBJECTACCESSOR);
523
                        }
524
                        $this->escapingEnabled = $escapingEnabledBackup;
525
                        $node->addChildNode($childNodeToAdd);
526
                    }
527
                    $text .= ')';
528
                    unset($potentialAccessor);
529
                    break;
530
531
                case Splitter::BYTE_INLINE_END:
532
                    $text .= '}';
533
                    if (--$ignoredEndingBraces >= 0) {
534
                        break;
535
                    }
536
                    $isArray = $allowArray && ($isArray ?: ($hasColon && !$hasPass && !$callDetected));
537
                    $potentialAccessor = $potentialAccessor ?? $captured;
538
539
                    // Decision: if we did not detect a ViewHelper we match the *entire* expression, from the cached
540
                    // starting index, to see if it matches a known type of expression. If it does, we must return the
541
                    // appropriate type of ExpressionNode.
542
                    if ($isArray) {
543
                        if ($captured !== null) {
544
                            $array[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
545
                        }
546
                        return new ArrayNode($array);
547
                    } elseif ($callDetected) {
548
                        // The first-priority check is for a ViewHelper used right before the inline expression ends,
549
                        // in which case there is no further syntax to come.
550
                        $node = $node->postParse($arguments, $this->state, $this->renderingContext);
551
                        $interceptionPoint = InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER;
552
                    } elseif ($this->splitter->context->context === Context::CONTEXT_ACCESSOR) {
553
                        // If we are currently in "accessor" context we can now add the accessor by stripping the collected text.
554
                        $node = new ObjectAccessorNode(substr($text, 1, -1));
555
                        $interceptionPoint = InterceptorInterface::INTERCEPT_OBJECTACCESSOR;
556
                    } elseif ($this->splitter->context->context === Context::CONTEXT_PROTECTED || ($hasWhitespace && !$callDetected && !$hasPass)) {
557
                        // In order to qualify for potentially being an expression, the entire inline node must contain
558
                        // whitespace, must not contain parenthesis, must not contain a colon and must not contain an
559
                        // inline pass operand. This significantly limits the number of times this (expensive) routine
560
                        // has to be executed.
561
                        $interceptionPoint = InterceptorInterface::INTERCEPT_TEXT;
562
                        $childNodeToAdd = new TextNode($text);
563
                        foreach ($this->renderingContext->getExpressionNodeTypes() as $expressionNodeTypeClassName) {
564
                            $matchedVariables = [];
565
                            // TODO: rewrite expression nodes to receive a sub-Splitter that lets the expression node
566
                            // consume a symbol+capture sequence and either match or ignore it; then use the already
567
                            // consumed (possibly halted mid-way through iterator!) sequence to achieve desired behavior.
568
                            preg_match_all($expressionNodeTypeClassName::$detectionExpression, $text, $matchedVariables, PREG_SET_ORDER);
569
                            foreach ($matchedVariables as $matchedVariableSet) {
0 ignored issues
show
Bug introduced by
The expression $matchedVariables of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
570
                                try {
571
                                    $childNodeToAdd = new $expressionNodeTypeClassName($matchedVariableSet[0], $matchedVariableSet, $this->state);
572
                                    $interceptionPoint = InterceptorInterface::INTERCEPT_EXPRESSION;
573
                                } catch (ExpressionException $error) {
574
                                    $childNodeToAdd = new TextNode($this->renderingContext->getErrorHandler()->handleExpressionError($error));
575
                                }
576
                                break;
577
                            }
578
                        }
579
                        $node = isset($node) ? $node->addChildNode($childNodeToAdd) : $childNodeToAdd;
580
                    } elseif (!$hasPass && !$callDetected) {
581
                        // Third priority check is if there was no pass syntax and no ViewHelper, in which case we
582
                        // create a standard ObjectAccessorNode; alternatively, if nothing was captured (expression
583
                        // was empty, e.g. {} was used) we create a TextNode with the captured text to output "{}".
584
                        if (isset($potentialAccessor)) {
585
                            // If the accessor is set we can trust it is not a numeric value, since this will have
586
                            // set $isArray to TRUE if nothing else already did so.
587
                            $node = is_numeric($potentialAccessor) ? $potentialAccessor + 0 : new ObjectAccessorNode($potentialAccessor);
588
                            $interceptionPoint = InterceptorInterface::INTERCEPT_OBJECTACCESSOR;
589
                        } else {
590
                            $node = new TextNode($text);
591
                            $interceptionPoint = InterceptorInterface::INTERCEPT_TEXT;
592
                        }
593
                    } elseif ($hasPass && $this->resolver->isAliasRegistered((string)$potentialAccessor)) {
594
                        // Fourth priority check is for a pass to a ViewHelper alias, e.g. "{value | raw}" in which case
595
                        // we look for the alias used and create a ViewHelperNode with no arguments.
596
                        $childNodeToAdd = $node;
597
                        $node = $this->resolver->createViewHelperInstance(null, $potentialAccessor);
598
                        $node->addChildNode($childNodeToAdd);
599
                        $node = $node->postParse($arguments, $this->state, $this->renderingContext);
600
                        $interceptionPoint = InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER;
601
                    } else {
602
                        # TODO: should this be an error case, or should it result in a TextNode?
603
                        throw $this->splitter->createErrorAtPosition(
604
                            'Invalid inline syntax - not accessor, not expression, not array, not ViewHelper, but ' .
605
                            'contains the tokens used by these in a sequence that is not valid Fluid syntax. You can ' .
606
                            'most likely avoid this by adding whitespace inside the curly braces before the first ' .
607
                            'Fluid-like symbol in the expression. Symbols recognized as Fluid are: "' .
608
                            addslashes(implode('","', array_map('chr', $this->contexts->inline->bytes))) . '"',
609
                            1558782228
610
                        );
611
                    }
612
613
                    $escapingEnabledBackup = $this->escapingEnabled;
614
                    $this->escapingEnabled = (bool)((isset($viewHelper) && $node->isOutputEscapingEnabled()) || $escapingEnabledBackup);
0 ignored issues
show
Bug introduced by
The variable $viewHelper seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
615
                    $this->callInterceptor($node, $interceptionPoint);
616
                    $this->escapingEnabled = $escapingEnabledBackup;
617
                    return $node;
618
            }
619
        }
620
621
        // See note in sequenceTagNode() end of method body. TL;DR: this is intentionally here instead of as "default"
622
        // case in the switch above for a very specific reason: the case is only encountered if seeing EOF before the
623
        // inline expression was closed.
624
        throw $this->splitter->createErrorAtPosition('Unterminated inline syntax', 1557838506);
625
    }
626
627
    /**
628
     * @param ArgumentDefinition[] $definitions
629
     * @param bool $numeric
630
     * @return ArrayNode
631
     */
632
    protected function sequenceArrayNode(array $definitions = null, bool $numeric = false): ArrayNode
633
    {
634
        $array = [];
635
636
        $keyOrValue = null;
637
        $key = null;
638
        $escapingEnabledBackup = $this->escapingEnabled;
639
        $this->escapingEnabled = false;
640
        $itemCount = -1;
641
        $countedEscapes = 0;
642
643
        $this->splitter->sequence->next();
644
        foreach ($this->splitter->sequence as $symbol => $captured) {
645
            switch ($symbol) {
646
                case Splitter::BYTE_SEPARATOR_COLON:
647
                case Splitter::BYTE_SEPARATOR_EQUALS:
648
                    // Colon or equals has same meaning (which allows tag syntax as argument syntax). Encountering this
649
                    // byte always means the preceding byte was a key. However, if nothing was captured before this,
650
                    // it means colon or equals was used without a key which is a syntax error.
651
                    $key = $key ?? $captured ?? (isset($keyOrValue) ? $keyOrValue->flatten(true) : null);
652
                    if (!isset($key)) {
653
                        throw $this->splitter->createErrorAtPosition('Unexpected colon or equals sign, no preceding key', 1559250839);
654
                    }
655
                    if ($definitions !== null && !$numeric && !isset($definitions[$key])) {
656
                        throw $this->splitter->createUnsupportedArgumentError((string)$key, $definitions);
657
                    }
658
                    break;
659
660
                case Splitter::BYTE_ARRAY_START:
661
                case Splitter::BYTE_INLINE:
662
                    // Minimal safeguards to improve error feedback. Theoretically such "garbage" could simply be ignored
663
                    // without causing problems to the parser, but it is probably best to report it as it could indicate
664
                    // the user expected X value but gets Y and doesn't notice why.
665
                    if ($captured !== null) {
666
                        throw $this->splitter->createErrorAtPosition('Unexpected content before array/inline start in associative array, ASCII: ' . ord($captured), 1559131849);
667
                    }
668
                    if (!isset($key) && !$numeric) {
669
                        throw $this->splitter->createErrorAtPosition('Unexpected array/inline start in associative array without preceding key', 1559131848);
670
                    }
671
672
                    // Encountering a curly brace or square bracket start byte will both cause a sub-array to be sequenced,
673
                    // the difference being that only the square bracket will cause third parameter ($numeric) passed to
674
                    // sequenceArrayNode() to be true, which in turn causes key-less items to be added with numeric indexes.
675
                    $key = $key ?? ++$itemCount;
676
                    $array[$key] = $this->sequenceArrayNode(null, $symbol === Splitter::BYTE_ARRAY_START);
677
                    $keyOrValue = null;
678
                    $key = null;
679
                    break;
680
681
                case Splitter::BYTE_QUOTE_SINGLE:
682
                case Splitter::BYTE_QUOTE_DOUBLE:
683
                    // Safeguard: if anything is captured before a quote this indicates garbage leading content. As with
684
                    // the garbage safeguards above, this one could theoretically be ignored in favor of silently making
685
                    // the odd syntax "just work".
686
                    if ($captured !== null) {
687
                        throw $this->splitter->createErrorAtPosition('Unexpected content before quote start in associative array, ASCII: ' . ord($captured), 1559145560);
688
                    }
689
690
                    // Quotes will always cause sequencing of the quoted string, but differs in behavior based on whether
691
                    // or not the $key is set. If $key is set, we know for sure we can assign a value. If it is not set
692
                    // we instead leave $keyOrValue defined so this will be processed by one of the next iterations.
693
                    $keyOrValue = $this->sequenceQuotedNode($countedEscapes);
694
                    if (isset($key)) {
695
                        $array[$key] = $keyOrValue->flatten(true);
696
                        $keyOrValue = null;
697
                        $key = null;
698
                        $countedEscapes = 0;
699
                    }
700
                    break;
701
702
                case Splitter::BYTE_SEPARATOR_COMMA:
703
                    // Comma separator: if we've collected a key or value, use it. Otherwise, use captured string.
704
                    // If neither key nor value nor captured string exists, ignore the comma (likely a tailing comma).
705
                    if (isset($keyOrValue)) {
706
                        // Key or value came as quoted string and exists in $keyOrValue
707
                        $potentialValue = $keyOrValue->flatten(true);
708
                        $key = $numeric ? ++$itemCount : $potentialValue;
709
                        $array[$key] = $numeric ? $potentialValue : (is_numeric($key) ? $key + 0 : new ObjectAccessorNode($key));
0 ignored issues
show
Bug introduced by
It seems like $key defined by $numeric ? ++$itemCount : $potentialValue on line 708 can also be of type object<TYPO3Fluid\Fluid\...ntaxTree\NodeInterface>; however, TYPO3Fluid\Fluid\Core\Pa...ssorNode::__construct() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
710
                    } elseif (isset($captured)) {
711
                        $key = $key ?? ($numeric ? ++$itemCount : $captured);
712
                        if (!$numeric && isset($definitions) && !isset($definitions[$key])) {
713
                            throw $this->splitter->createUnsupportedArgumentError((string)$key, $definitions);
714
                        }
715
                        $array[$key] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
716
                    }
717
                    $keyOrValue = null;
718
                    $key = null;
719
                    break;
720
721
                case Splitter::BYTE_WHITESPACE_TAB:
722
                case Splitter::BYTE_WHITESPACE_RETURN:
723
                case Splitter::BYTE_WHITESPACE_EOL:
724
                case Splitter::BYTE_WHITESPACE_SPACE:
725
                    // Any whitespace attempts to set the key, if not already set. The captured string may be null as
726
                    // well, leaving the $key variable still null and able to be coalesced.
727
                    $key = $key ?? $captured;
728
                    break;
729
730
                case Splitter::BYTE_BACKSLASH:
731
                    // Escapes are simply counted and passed to the sequenceQuotedNode() method, causing that method
732
                    // to ignore exactly this number of backslashes before a matching quote is seen as closing quote.
733
                    ++$countedEscapes;
734
                    break;
735
736
                case Splitter::BYTE_INLINE_END:
737
                case Splitter::BYTE_ARRAY_END:
738
                case Splitter::BYTE_PARENTHESIS_END:
739
                    // Array end indication. Check if anything was collected previously or was captured currently,
740
                    // assign that to the array and return an ArrayNode with the full array inside.
741
                    $captured = $captured ?? (isset($keyOrValue) ? $keyOrValue->flatten(true) : null);
742
                    $key = $key ?? ($numeric ? ++$itemCount : $captured);
743
                    if (isset($captured, $key)) {
744
                        if (is_numeric($captured)) {
745
                            $array[$key] = $captured + 0;
746
                        } elseif (isset($keyOrValue)) {
747
                            $array[$key] = $keyOrValue->flatten();
748
                        } else {
749
                            $array[$key] = new ObjectAccessorNode($captured ?? $key);
750
                        }
751
                    }
752
                    if (!$numeric && isset($key, $definitions) && !isset($definitions[$key])) {
753
                        throw $this->splitter->createUnsupportedArgumentError((string)$key, $definitions);
754
                    }
755
                    $this->escapingEnabled = $escapingEnabledBackup;
756
                    return new ArrayNode($array);
757
            }
758
        }
759
760
        throw $this->splitter->createErrorAtPosition(
761
            'Unterminated array',
762
            1557748574
763
        );
764
    }
765
766
    /**
767
     * Sequence a quoted value
768
     *
769
     * The return can be either of:
770
     *
771
     * 1. A string value if source was for example "string"
772
     * 2. An integer if source was for example "1"
773
     * 3. A float if source was for example "1.25"
774
     * 4. A RootNode instance with multiple child nodes if source was for example "string {var}"
775
     *
776
     * The idea is to return the raw value if there is no reason for it to
777
     * be a node as such - which is only necessary if the quoted expression
778
     * contains other (dynamic) values like an inline syntax.
779
     *
780
     * @param int $leadingEscapes A backwards compatibility measure: when passed, this number of escapes must precede a closing quote for it to trigger node closing.
781
     * @param bool $allowArray
782
     * @return RootNode
783
     */
784
    protected function sequenceQuotedNode(int $leadingEscapes = 0, $allowArray = true): RootNode
785
    {
786
        $startingByte = $this->source->bytes[$this->splitter->index];
787
        $contextToRestore = $this->splitter->switch($this->contexts->quoted);
788
        $node = new RootNode();
789
        $this->splitter->sequence->next();
790
        $countedEscapes = 0;
791
792
        foreach ($this->splitter->sequence as $symbol => $captured) {
793
            switch ($symbol) {
794
795
                case Splitter::BYTE_ARRAY_START:
796
                    $countedEscapes = 0; // Theoretically not required but done in case of stray escapes (gets ignored)
797
                    if ($captured === null) {
798
                        // Array start "[" only triggers array sequencing if it is the very first byte in the quoted
799
                        // string - otherwise, it is added as part of the text.
800
                        $this->splitter->switch($this->contexts->array);
801
                        $node->addChildNode($this->sequenceArrayNode(null, $allowArray));
802
                        $this->splitter->switch($this->contexts->quoted);
803
                    } else {
804
                        $node->addChildNode(new TextNode($captured . '['));
805
                    }
806
                    break;
807
808
                case Splitter::BYTE_INLINE:
809
                    $countedEscapes = 0; // Theoretically not required but done in case of stray escapes (gets ignored)
810
                    // The quoted string contains a sub-expression. We extract the captured content so far and if it
811
                    // is not an empty string, add it as a child of the RootNode we're building, then we add the inline
812
                    // expression as next sibling and continue the loop.
813
                    if ($captured !== null) {
814
                        $childNode = new TextNode($captured);
815
                        $this->callInterceptor($childNode, InterceptorInterface::INTERCEPT_TEXT);
816
                        $node->addChildNode($childNode);
817
                    }
818
819
                    $node->addChildNode($this->sequenceInlineNodes());
820
                    $this->splitter->switch($this->contexts->quoted);
821
                    break;
822
823
                case Splitter::BYTE_BACKSLASH:
824
                    ++$countedEscapes;
825
                    if ($captured !== null) {
826
                        $node->addChildNode(new TextNode($captured));
827
                    }
828
                    break;
829
830
                // Note: although "case $startingByte:" could have been used here, it would not compile the switch
831
                // as a hash map and thus would not perform as well overall - when called frequently as it will be.
832
                // Backtick will only be encountered if the context is "protected" (insensitive inline sequencing)
833
                case Splitter::BYTE_QUOTE_SINGLE:
834
                case Splitter::BYTE_QUOTE_DOUBLE:
835
                case Splitter::BYTE_BACKTICK:
836
                    if ($symbol !== $startingByte || $countedEscapes !== $leadingEscapes) {
837
                        $node->addChildNode(new TextNode($captured . chr($symbol)));
838
                        $countedEscapes = 0; // If number of escapes do not match expected, reset the counter
839
                        break;
840
                    }
841
                    if ($captured !== null) {
842
                        $node->addChildNode(new TextNode($captured));
843
                    }
844
                    $this->splitter->switch($contextToRestore);
845
                    return $node;
846
            }
847
        }
848
849
        throw $this->splitter->createErrorAtPosition('Unterminated expression inside quotes', 1557700793);
850
    }
851
852
    /**
853
     * Call all interceptors registered for a given interception point.
854
     *
855
     * @param NodeInterface $node The syntax tree node which can be modified by the interceptors.
856
     * @param integer $interceptionPoint the interception point. One of the \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_* constants.
857
     * @return void
858
     */
859
    protected function callInterceptor(NodeInterface &$node, $interceptionPoint)
860
    {
861
        if ($this->escapingEnabled) {
862
            /** @var $interceptor InterceptorInterface */
863
            foreach ($this->configuration->getEscapingInterceptors($interceptionPoint) as $interceptor) {
864
                $node = $interceptor->process($node, $interceptionPoint, $this->state);
865
            }
866
        }
867
868
        /** @var $interceptor InterceptorInterface */
869
        foreach ($this->configuration->getInterceptors($interceptionPoint) as $interceptor) {
870
            $node = $interceptor->process($node, $interceptionPoint, $this->state);
871
        }
872
    }
873
}