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

Sequencer::sequenceInlineNodes()   F

Complexity

Conditions 75
Paths 236

Size

Total Lines 312

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 75
nc 236
nop 1
dl 0
loc 312
rs 2.3066
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\ViewHelperInterface;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperResolver;
16
17
/**
18
 * Sequencer for Fluid syntax
19
 *
20
 * Uses a NoRewindIterator around a sequence of byte values to
21
 * iterate over each syntax-relevant character and determine
22
 * which nodes to create.
23
 *
24
 * Passes the outer iterator between functions that perform the
25
 * iterations. Since the iterator is a NoRewindIterator it will
26
 * not be reset before the start of each loop - meaning that
27
 * when it is passed to a new function, that function continues
28
 * from the last touched index in the byte sequence.
29
 *
30
 * The circumstances around "break or return" in the switches is
31
 * very, very important to understand in context of how iterators
32
 * work. Returning does not advance the iterator like breaking
33
 * would and this causes a different position in the byte sequence
34
 * to be experienced in the method that uses the return value (it
35
 * sees the index of the symbol which terminated the expression,
36
 * not the next symbol after that).
37
 */
38
class Sequencer
39
{
40
    /**
41
     * @var RenderingContextInterface
42
     */
43
    protected $renderingContext;
44
45
    /**
46
     * @var ParsingState
47
     */
48
    protected $state;
49
50
    /**
51
     * @var Contexts
52
     */
53
    protected $contexts;
54
55
    /**
56
     * @var Source
57
     */
58
    protected $source;
59
60
    /**
61
     * @var Splitter
62
     */
63
    protected $splitter;
64
65
    /**
66
     * @var Configuration
67
     */
68
    protected $configuration;
69
70
    /**
71
     * @var ViewHelperResolver
72
     */
73
    protected $resolver;
74
75
    /**
76
     * Whether or not the escaping interceptors are active
77
     *
78
     * @var boolean
79
     */
80
    protected $escapingEnabled = true;
81
82
    public function __construct(
83
        RenderingContextInterface $renderingContext,
84
        ParsingState $state,
85
        Contexts $contexts,
86
        Source $source
87
    ) {
88
        $this->renderingContext = $renderingContext;
89
        $this->resolver = $renderingContext->getViewHelperResolver();
90
        $this->configuration = $renderingContext->buildParserConfiguration();
91
        $this->state = clone $state;
92
        $this->contexts = $contexts;
93
        $this->source = $source;
94
        $this->splitter = new Splitter($this->source, $this->contexts);
95
    }
96
97
    public function sequence(): ParsingState
98
    {
99
        #$sequence = $this->splitter->parse();
100
101
        // Please note: repeated calls to $this->state->getTopmostNodeFromStack() are indeed intentional. That method may
102
        // return different nodes at different times depending on what has occurred in other methods! Only the places
103
        // where $node is actually extracted is it (by design) safe to do so. DO NOT REFACTOR!
104
        // It is *also* intentional that this switch has no default case. The root context is very specific and will
105
        // only apply when the splitter is actually in root, which means there is no chance of it yielding an unexpected
106
        // character (because that implies a method called by this method already threw a SequencingException).
107
        foreach ($this->splitter->sequence as $symbol => $captured) {
108
            switch ($symbol) {
109
                case Splitter::BYTE_INLINE:
110
                    $node = $this->state->getNodeFromStack();
111
                    if ($this->splitter->index > 1 && $this->source->bytes[$this->splitter->index - 1] === Splitter::BYTE_BACKSLASH) {
112
                        $node->addChildNode(new TextNode(substr($captured, 0, -1) . '{'));
113
                        break;
114
                    }
115
                    if ($captured !== null) {
116
                        $node->addChildNode(new TextNode($captured));
117
                    }
118
                    $node->addChildNode($this->sequenceInlineNodes(false));
119
                    $this->splitter->switch($this->contexts->root);
120
                    break;
121
122
                case Splitter::BYTE_TAG:
123
                    if ($captured !== null) {
124
                        $this->state->getNodeFromStack()->addChildNode(new TextNode($captured));
125
                    }
126
127
                    $childNode = $this->sequenceTagNode();
128
                    $this->splitter->switch($this->contexts->root);
129
                    if ($childNode) {
130
                        $this->state->getNodeFromStack()->addChildNode($childNode);
131
                    }
132
                    break;
133
134
                case Splitter::BYTE_NULL:
135
                    if ($captured !== null) {
136
                        $this->state->getNodeFromStack()->addChildNode(new TextNode($captured));
137
                    }
138
                    break;
139
            }
140
        }
141
142
        return $this->state;
143
    }
144
145
    /**
146
     * @return NodeInterface|null
147
     */
148
    protected function sequenceTagNode(): ?NodeInterface
149
    {
150
        $arguments = [];
151
        $definitions = null;
152
        $text = '<';
153
        $namespace = null;
154
        $method = null;
155
        $bytes = &$this->source->bytes;
156
        $node = new RootNode();
157
        $selfClosing = false;
158
        $closing = false;
159
        #$escapingEnabledBackup = $this->escapingEnabled;
160
161
        $interceptionPoint = InterceptorInterface::INTERCEPT_OPENING_VIEWHELPER;
162
163
        $this->splitter->switch($this->contexts->tag);
164
        $this->splitter->sequence->next();
165
        foreach ($this->splitter->sequence as $symbol => $captured) {
166
            $text .= $captured;
167
            switch ($symbol) {
168
                case Splitter::BYTE_INLINE:
169
                    $contextBefore = $this->splitter->context;
170
                    $collected = $this->sequenceInlineNodes(isset($namespace) && isset($method));
171
                    $node->addChildNode(new TextNode($text));
172
                    $node->addChildNode($collected);
173
                    $text = '';
174
                    $this->splitter->switch($contextBefore);
175
                    break;
176
177
                case Splitter::BYTE_SEPARATOR_EQUALS:
178
                    $key = $captured;
179
                    if ($definitions !== null && !isset($definitions[$key])) {
180
                        throw $this->splitter->createUnsupportedArgumentError($key, $definitions);
181
                    }
182
                    break;
183
184
                case Splitter::BYTE_QUOTE_DOUBLE:
185
                case Splitter::BYTE_QUOTE_SINGLE:
186
                    $text .= chr($symbol);
187
                    if (!isset($key)) {
188
                        throw $this->splitter->createErrorAtPosition('Quoted value without a key is not allowed in tags', 1558952412);
189
                    } else {
190
                        $arguments[$key] = $this->sequenceQuotedNode(0, isset($namespace) && isset($method))->flatten(true);
191
                        $key = null;
192
                    }
193
                    break;
194
195
                case Splitter::BYTE_TAG_CLOSE:
196
                    $method = $method ?? $captured;
197
                    $text .= '/';
198
                    $closing = true;
199
                    $selfClosing = $bytes[$this->splitter->index - 1] !== Splitter::BYTE_TAG;
200
                    $interceptionPoint = InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER;
201
                    if ($this->splitter->context->context === Context::CONTEXT_ATTRIBUTES && $captured !== null) {
202
                        // We are still capturing arguments and the last yield contained a value. Null-coalesce key
203
                        // with captured string so object accessor becomes key name (ECMA shorthand literal)
204
                        $arguments[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
205
                        $key = null;
206
                    }
207
                    break;
208
209
                case Splitter::BYTE_SEPARATOR_COLON:
210
                    $text .= ':';
211
                    $namespace = $namespace ?? $captured;
212
                    break;
213
214
                case Splitter::BYTE_TAG_END:
215
                    $text .= '>';
216
                    $method = $method ?? $captured;
217
218
                    if (!isset($namespace) || !isset($method) || $this->splitter->context->context === Context::CONTEXT_DEAD || $this->resolver->isNamespaceIgnored($namespace)) {
219
                        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...
220
                    }
221
222
                    try {
223
                        $expectedClass = $this->resolver->resolveViewHelperClassName($namespace, $method);
224
                    } catch (\TYPO3Fluid\Fluid\Core\Exception $exception) {
225
                        throw $this->splitter->createErrorAtPosition($exception->getMessage(), $exception->getCode());
226
                    }
227
228
                    if ($closing && !$selfClosing) {
229
                        // Closing byte was more than two bytes back, meaning the tag is NOT self-closing, but is a
230
                        // closing tag for a previously opened+stacked node. Finalize the node now.
231
                        $closesNode = $this->state->popNodeFromStack();
232
                        if ($closesNode instanceof $expectedClass) {
233
                            $arguments = $closesNode->getParsedArguments();
234
                            $viewHelperNode = $closesNode;
235
                        } else {
236
                            throw $this->splitter->createErrorAtPosition(
237
                                sprintf(
238
                                    'Mismatched closing tag. Expecting: %s:%s (%s). Found: (%s).',
239
                                    $namespace,
240
                                    $method,
241
                                    $expectedClass,
242
                                    get_class($closesNode)
243
                                ),
244
                                1557700789
245
                            );
246
                        }
247
                    }
248
249
                    if ($this->splitter->context->context === Context::CONTEXT_ATTRIBUTES && $captured !== null) {
250
                        // We are still capturing arguments and the last yield contained a value. Null-coalesce key
251
                        // with captured string so object accessor becomes key name (ECMA shorthand literal)
252
                        $arguments[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
253
                    }
254
255
                    $viewHelperNode = $viewHelperNode ?? $this->resolver->createViewHelperInstanceFromClassName($expectedClass);
256
                    #$this->escapingEnabled = $escapingEnabledBackup;
257
258
                    if (!$closing) {
259
                        $this->callInterceptor($viewHelperNode, $interceptionPoint);
260
                        $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...
261
                        $this->state->pushNodeToStack($viewHelperNode);
262
                        return null;
263
                    }
264
265
                    $viewHelperNode = $viewHelperNode->postParse($arguments, $this->state, $this->renderingContext);
266
267
                    return $viewHelperNode;
268
269
                case Splitter::BYTE_WHITESPACE_TAB:
270
                case Splitter::BYTE_WHITESPACE_RETURN:
271
                case Splitter::BYTE_WHITESPACE_EOL:
272
                case Splitter::BYTE_WHITESPACE_SPACE:
273
                    if ($this->splitter->context->context === Context::CONTEXT_ATTRIBUTES) {
274
                        if ($captured !== null) {
275
                            $arguments[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
276
                            $key = null;
277
                        }
278
                    } else {
279
                        $text .= chr($symbol);
280
                        if (isset($namespace)) {
281
                            $method = $captured;
282
283
                            $this->escapingEnabled = false;
284
                            $viewHelperNode = $this->resolver->createViewHelperInstance($namespace, $method);
285
                            $definitions = $viewHelperNode->prepareArguments();
286
287
                            // A whitespace character, in tag context, means the beginning of an array sequence (which may
288
                            // or may not contain any items; the next symbol may be a tag end or tag close). We sequence the
289
                            // arguments array and create a ViewHelper node.
290
                            $this->splitter->switch($this->contexts->attributes);
291
                            break;
292
                        }
293
294
                        // A whitespace before a colon means the tag is not a namespaced tag. We will ignore everything
295
                        // inside this tag, except for inline syntax, until the tag ends. For this we use a special,
296
                        // limited variant of the root context where instead of scanning for "<" we scan for ">".
297
                        // We continue in this same loop because it still matches the potential symbols being yielded.
298
                        // Most importantly: this new reduced context will NOT match a colon which is the trigger symbol
299
                        // for a ViewHelper tag.
300
                        $this->splitter->switch($this->contexts->dead);
301
                    }
302
                    break;
303
            }
304
        }
305
306
        // This case on the surface of it, belongs as "default" case in the switch above. However, the only case that
307
        // would *actually* produce this error, is if the splitter reaches EOF (null byte) symbol before the tag was
308
        // closed. Literally every other possible error type will be thrown as more specific exceptions (e.g. invalid
309
        // argument, missing key, wrong quotes, bad inline and *everything* else with the exception of EOF). Even a
310
        // stray null byte would not be caught here as null byte is not part of the symbol collection for "tag" context.
311
        throw $this->splitter->createErrorAtPosition('Unexpected token in tag sequencing', 1557700786);
312
    }
313
314
    /**
315
     * @param bool $allowArray
316
     * @return NodeInterface
317
     */
318
    protected function sequenceInlineNodes(bool $allowArray = true): NodeInterface
319
    {
320
        $text = '{';
321
        $node = null;
322
        $key = null;
323
        $namespace = null;
324
        $method = null;
325
        $potentialAccessor = null;
326
        $callDetected = false;
327
        $hasPass = false;
328
        $hasColon = null;
329
        $hasWhitespace = false;
330
        $isArray = false;
331
        $array = [];
332
        $arguments = [];
333
        $ignoredEndingBraces = 0;
334
        $countedEscapes = 0;
335
336
        $this->splitter->switch($this->contexts->inline);
337
        $this->splitter->sequence->next();
338
        foreach ($this->splitter->sequence as $symbol => $captured) {
339
            $text .= $captured;
340
            switch ($symbol) {
341
                case Splitter::BYTE_BACKSLASH:
342
                    // Increase the number of counted escapes (is passed to sequenceNode() in the "QUOTE" cases and reset
343
                    // after the quoted string is extracted).
344
                    ++$countedEscapes;
345
                    break;
346
347
                case Splitter::BYTE_ARRAY_START:
348
349
                    $text .= chr($symbol);
350
                    $isArray = $allowArray;
351
352
                    #ArrayStart:
353
                    // Sequence the node. Pass the "use numeric keys?" boolean based on the current byte. Only array
354
                    // start creates numeric keys. Inline start with keyless values creates ECMA style {foo:foo, bar:bar}
355
                    // from {foo, bar}.
356
                    $array[$key ?? $captured ?? 0] = $node = $this->sequenceArrayNode(null, $symbol === Splitter::BYTE_ARRAY_START);
357
                    $this->splitter->switch($this->contexts->inline);
358
                    unset($key);
359
                    break;
360
361
                case Splitter::BYTE_INLINE:
362
                    // Encountering this case can mean different things: sub-syntax like {foo.{index}} or array, depending
363
                    // on presence of either a colon or comma before the inline. In protected mode it is simply added.
364
                    $text .= '{';
365
                    if (!$hasWhitespace && $text !== '{{') {
366
                        // Most likely, a nested object accessor syntax e.g. {foo.{bar}} - enter protected context since
367
                        // these accessors do not allow anything other than additional nested accessors.
368
                        $this->splitter->switch($this->contexts->accessor);
369
                        ++$ignoredEndingBraces;
370
                    } elseif ($this->splitter->context->context === Context::CONTEXT_PROTECTED) {
371
                        // Ignore one ending additional curly brace. Subtracted in the BYTE_INLINE_END case below.
372
                        // The expression in this case looks like {{inline}.....} and we capture the curlies.
373
                        $potentialAccessor .= $captured;
374
                        ++$ignoredEndingBraces;
375
                    } elseif ($allowArray || $isArray) {
376
                        $isArray = true;
377
                        $captured = $key ?? $captured ?? $potentialAccessor;
378
                        // This is a sub-syntax following a colon - meaning it is an array.
379
                        if ($captured !== null) {
380
                            #goto ArrayStart;
381
                            $array[$key ?? $captured ?? 0] = $node = $this->sequenceArrayNode(null, $symbol === Splitter::BYTE_ARRAY_START);
382
                            $this->splitter->switch($this->contexts->inline);
383
                        }
384
                    } else {
385
                        $childNodeToAdd = $this->sequenceInlineNodes($allowArray);
386
                        $node = isset($node) ? $node->addChildNode($childNodeToAdd) : (new RootNode())->addChildNode($childNodeToAdd);
387
                    }
388
                    break;
389
390
                case Splitter::BYTE_MINUS:
391
                    $text .= '-';
392
                    break;
393
394
                // Backtick may be encountered in two different contexts: normal inline context, in which case it has
395
                // the same meaning as any quote and causes sequencing of a quoted string. Or protected context, in
396
                // which case it also sequences a quoted node but appends the result instead of assigning to array.
397
                // Note that backticks do not support escapes (they are a new feature that does not require escaping).
398
                case Splitter::BYTE_BACKTICK:
399
                    if ($this->splitter->context->context === Context::CONTEXT_PROTECTED) {
400
                        $node->addChildNode(new TextNode($text));
401
                        $node->addChildNode($this->sequenceQuotedNode()->flatten());
402
                        $text = '';
403
                        break;
404
                    }
405
                // Fallthrough is intentional: if not in protected context, consider the backtick a normal quote.
406
407
                // Case not normally countered in straight up "inline" context, but when encountered, means we have
408
                // explicitly found a quoted array key - and we extract it.
409
                case Splitter::BYTE_QUOTE_SINGLE:
410
                case Splitter::BYTE_QUOTE_DOUBLE:
411
                    if (!$allowArray) {
412
                        $text .= chr($symbol);
413
                        break;
414
                    }
415
                    if (isset($key)) {
416
                        $array[$key] = $this->sequenceQuotedNode($countedEscapes)->flatten(true);
417
                        $key = null;
418
                    } else {
419
                        $key = $this->sequenceQuotedNode($countedEscapes)->flatten(true);
420
                    }
421
                    $countedEscapes = 0;
422
                    $isArray = $allowArray;
423
                    break;
424
425
                case Splitter::BYTE_SEPARATOR_COMMA:
426
                    if (!$allowArray) {
427
                        $text .= ',';
428
                        break;
429
                    }
430
                    if (isset($captured)) {
431
                        $array[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
432
                    }
433
                    $key = null;
434
                    $isArray = $allowArray;
435
                    break;
436
437
                case Splitter::BYTE_SEPARATOR_EQUALS:
438
                    $text .= '=';
439
                    if (!$allowArray) {
440
                        $node = new RootNode();
441
                        $this->splitter->switch($this->contexts->protected);
442
                        break;
443
                    }
444
                    $key = $captured;
445
                    $isArray = $allowArray;
446
                    break;
447
448
                case Splitter::BYTE_SEPARATOR_COLON:
449
                    $text .= ':';
450
                    $hasColon = true;
451
                    $namespace = $captured;
452
                    $key = $key ?? $captured;
453
                    $isArray = $isArray || ($allowArray && is_numeric($key));
454
                    break;
455
456
                case Splitter::BYTE_WHITESPACE_SPACE:
457
                case Splitter::BYTE_WHITESPACE_EOL:
458
                case Splitter::BYTE_WHITESPACE_RETURN:
459
                case Splitter::BYTE_WHITESPACE_TAB:
460
                    // If we already collected some whitespace we must enter protected context.
461
                    $text .= $this->source->source[$this->splitter->index - 1];
462
                    if ($hasWhitespace && !$hasPass && !$allowArray) {
463
                        // Protection mode: this very limited context does not allow tags or inline syntax, and will
464
                        // protect things like CSS and JS - and will only enter a more reactive context if encountering
465
                        // the backtick character, meaning a quoted string will be sequenced. This backtick-quoted
466
                        // string can then contain inline syntax like variable accessors.
467
                        $node = $node ?? new RootNode();
468
                        $this->splitter->switch($this->contexts->protected);
469
                        break;
470
                    }
471
                    $key = $key ?? $captured;
472
                    $hasWhitespace = true;
473
                    $isArray = $allowArray && ($hasColon ?? $isArray ?? is_numeric($captured));
474
                    $potentialAccessor = ($potentialAccessor ?? $captured);
475
                    break;
476
477
                case Splitter::BYTE_TAG_END:
478
                case Splitter::BYTE_PIPE:
479
                    // If there is an accessor on the left side of the pipe and $node is not defined, we create $node
480
                    // as an object accessor. If $node already exists we do nothing (and expect the VH trigger, the
481
                    // parenthesis start case below, to add $node as childnode and create a new $node).
482
                    $hasPass = true;
483
                    $isArray = $allowArray;
484
                    $callDetected = false;
485
                    $potentialAccessor = $potentialAccessor ?? $captured;
486
                    $text .=  $this->source->source[$this->splitter->index - 1];
487
                    if ($node instanceof ViewHelperInterface) {
488
                        $node->postParse($arguments, $this->state, $this->renderingContext);
489
                    }
490
                    if (isset($potentialAccessor)) {
491
                        $childNodeToAdd = new ObjectAccessorNode($potentialAccessor);
492
                        $node = isset($node) ? $node->addChildNode($childNodeToAdd) : $childNodeToAdd; //$node ?? (is_numeric($potentialAccessor) ? $potentialAccessor + 0 : new ObjectAccessorNode($potentialAccessor));
493
                    }
494
                    //!isset($potentialAccessor) ?: ($node = ($node ?? $this->createObjectAccessorNodeOrRawValue($potentialAccessor)));
495
                    unset($namespace, $method, $potentialAccessor, $key);
496
                    break;
497
498
                case Splitter::BYTE_PARENTHESIS_START:
499
                    $isArray = false;
500
                    // Special case: if a parenthesis start was preceded by whitespace but had no pass operator we are
501
                    // not dealing with a ViewHelper call and will continue the sequencing, grabbing the parenthesis as
502
                    // part of the expression.
503
                    $text .= '(';
504
                    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...
505
                        $this->splitter->switch($this->contexts->protected);
506
                        unset($namespace, $method);
507
                        break;
508
                    }
509
510
                    $callDetected = true;
511
                    $method = $captured;
512
                    $childNodeToAdd = $node;
513
                    try {
514
                        $node = $this->resolver->createViewHelperInstance($namespace, $method);
515
                        $definitions = $node->prepareArguments();
516
                    } catch (\TYPO3Fluid\Fluid\Core\Exception $exception) {
517
                        throw $this->splitter->createErrorAtPosition($exception->getMessage(), $exception->getCode());
518
                    }
519
                    $this->splitter->switch($this->contexts->array);
520
                    $arguments = $this->sequenceArrayNode($definitions)->getInternalArray();
521
                    $this->splitter->switch($this->contexts->inline);
522
                    if ($childNodeToAdd) {
523
                        $escapingEnabledBackup = $this->escapingEnabled;
524
                        $this->escapingEnabled = (bool)$node->isChildrenEscapingEnabled();
525
                        if ($childNodeToAdd instanceof ObjectAccessorNode) {
526
                            $this->callInterceptor($childNodeToAdd, InterceptorInterface::INTERCEPT_OBJECTACCESSOR);
527
                        }
528
                        $this->escapingEnabled = $escapingEnabledBackup;
529
                        $node->addChildNode($childNodeToAdd);
530
                    }
531
                    $text .= ')';
532
                    unset($potentialAccessor);
533
                    break;
534
535
                case Splitter::BYTE_INLINE_END:
536
                    $text .= '}';
537
                    if (--$ignoredEndingBraces >= 0) {
538
                        break;
539
                    }
540
                    $isArray = $allowArray && ($isArray ?: ($hasColon && !$hasPass && !$callDetected));
541
                    $potentialAccessor = $potentialAccessor ?? $captured;
542
543
                    // Decision: if we did not detect a ViewHelper we match the *entire* expression, from the cached
544
                    // starting index, to see if it matches a known type of expression. If it does, we must return the
545
                    // appropriate type of ExpressionNode.
546
                    if ($isArray) {
547
                        if ($captured !== null) {
548
                            $array[$key ?? $captured] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
549
                        }
550
                        return new ArrayNode($array);
551
                    } elseif ($callDetected) {
552
                        // The first-priority check is for a ViewHelper used right before the inline expression ends,
553
                        // in which case there is no further syntax to come.
554
                        $node = $node->postParse($arguments, $this->state, $this->renderingContext);
555
                        $interceptionPoint = InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER;
556
                    } elseif ($this->splitter->context->context === Context::CONTEXT_ACCESSOR) {
557
                        // If we are currently in "accessor" context we can now add the accessor by stripping the collected text.
558
                        $node = new ObjectAccessorNode(substr($text, 1, -1));
559
                        $interceptionPoint = InterceptorInterface::INTERCEPT_OBJECTACCESSOR;
560
                    } elseif ($this->splitter->context->context === Context::CONTEXT_PROTECTED || ($hasWhitespace && !$callDetected && !$hasPass)) {
561
                        // In order to qualify for potentially being an expression, the entire inline node must contain
562
                        // whitespace, must not contain parenthesis, must not contain a colon and must not contain an
563
                        // inline pass operand. This significantly limits the number of times this (expensive) routine
564
                        // has to be executed.
565
                        $interceptionPoint = InterceptorInterface::INTERCEPT_TEXT;
566
                        $childNodeToAdd = new TextNode($text);
567
                        foreach ($this->renderingContext->getExpressionNodeTypes() as $expressionNodeTypeClassName) {
568
                            $matchedVariables = [];
569
                            // TODO: rewrite expression nodes to receive a sub-Splitter that lets the expression node
570
                            // consume a symbol+capture sequence and either match or ignore it; then use the already
571
                            // consumed (possibly halted mid-way through iterator!) sequence to achieve desired behavior.
572
                            preg_match_all($expressionNodeTypeClassName::$detectionExpression, $text, $matchedVariables, PREG_SET_ORDER);
573
                            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...
574
                                try {
575
                                    $childNodeToAdd = new $expressionNodeTypeClassName($matchedVariableSet[0], $matchedVariableSet, $this->state);
576
                                    $interceptionPoint = InterceptorInterface::INTERCEPT_EXPRESSION;
577
                                } catch (ExpressionException $error) {
578
                                    $childNodeToAdd = new TextNode($this->renderingContext->getErrorHandler()->handleExpressionError($error));
579
                                }
580
                                break;
581
                            }
582
                        }
583
                        $node = isset($node) ? $node->addChildNode($childNodeToAdd) : $childNodeToAdd;
584
                    } elseif (!$hasPass && !$callDetected) {
585
                        // Third priority check is if there was no pass syntax and no ViewHelper, in which case we
586
                        // create a standard ObjectAccessorNode; alternatively, if nothing was captured (expression
587
                        // was empty, e.g. {} was used) we create a TextNode with the captured text to output "{}".
588
                        if (isset($potentialAccessor)) {
589
                            // If the accessor is set we can trust it is not a numeric value, since this will have
590
                            // set $isArray to TRUE if nothing else already did so.
591
                            $node = is_numeric($potentialAccessor) ? $potentialAccessor + 0 : new ObjectAccessorNode($potentialAccessor);
592
                            $interceptionPoint = InterceptorInterface::INTERCEPT_OBJECTACCESSOR;
593
                        } else {
594
                            $node = new TextNode($text);
595
                            $interceptionPoint = InterceptorInterface::INTERCEPT_TEXT;
596
                        }
597
                    } elseif ($hasPass && $this->resolver->isAliasRegistered((string)$potentialAccessor)) {
598
                        // Fourth priority check is for a pass to a ViewHelper alias, e.g. "{value | raw}" in which case
599
                        // we look for the alias used and create a ViewHelperNode with no arguments.
600
                        $childNodeToAdd = $node;
601
                        $node = $this->resolver->createViewHelperInstance(null, $potentialAccessor);
602
                        $node->addChildNode($childNodeToAdd);
603
                        $node = $node->postParse($arguments, $this->state, $this->renderingContext);
604
                        $interceptionPoint = InterceptorInterface::INTERCEPT_CLOSING_VIEWHELPER;
605
                    } else {
606
                        # TODO: should this be an error case, or should it result in a TextNode?
607
                        throw $this->splitter->createErrorAtPosition(
608
                            'Invalid inline syntax - not accessor, not expression, not array, not ViewHelper, but ' .
609
                            'contains the tokens used by these in a sequence that is not valid Fluid syntax. You can ' .
610
                            'most likely avoid this by adding whitespace inside the curly braces before the first ' .
611
                            'Fluid-like symbol in the expression. Symbols recognized as Fluid are: "' .
612
                            addslashes(implode('","', array_map('chr', $this->contexts->inline->bytes))) . '"',
613
                            1558782228
614
                        );
615
                    }
616
617
                    $escapingEnabledBackup = $this->escapingEnabled;
618
                    $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...
619
                    $this->callInterceptor($node, $interceptionPoint);
620
                    $this->escapingEnabled = $escapingEnabledBackup;
621
                    return $node;
622
            }
623
        }
624
625
        // See note in sequenceTagNode() end of method body. TL;DR: this is intentionally here instead of as "default"
626
        // case in the switch above for a very specific reason: the case is only encountered if seeing EOF before the
627
        // inline expression was closed.
628
        throw $this->splitter->createErrorAtPosition('Unterminated inline syntax', 1557838506);
629
    }
630
631
    /**
632
     * @param ArgumentDefinition[] $definitions
633
     * @param bool $numeric
634
     * @return ArrayNode
635
     */
636
    protected function sequenceArrayNode(array $definitions = null, bool $numeric = false): ArrayNode
637
    {
638
        $array = [];
639
640
        $keyOrValue = null;
641
        $key = null;
642
        $escapingEnabledBackup = $this->escapingEnabled;
643
        $this->escapingEnabled = false;
644
        $itemCount = -1;
645
        $countedEscapes = 0;
646
647
        $this->splitter->sequence->next();
648
        foreach ($this->splitter->sequence as $symbol => $captured) {
649
            switch ($symbol) {
650
                case Splitter::BYTE_SEPARATOR_COLON:
651
                case Splitter::BYTE_SEPARATOR_EQUALS:
652
                    // Colon or equals has same meaning (which allows tag syntax as argument syntax). Encountering this
653
                    // byte always means the preceding byte was a key. However, if nothing was captured before this,
654
                    // it means colon or equals was used without a key which is a syntax error.
655
                    $key = $key ?? $captured ?? (isset($keyOrValue) ? $keyOrValue->flatten(true) : null);
656
                    if (!isset($key)) {
657
                        throw $this->splitter->createErrorAtPosition('Unexpected colon or equals sign, no preceding key', 1559250839);
658
                    }
659
                    if ($definitions !== null && !$numeric && !isset($definitions[$key])) {
660
                        throw $this->splitter->createUnsupportedArgumentError((string)$key, $definitions);
661
                    }
662
                    break;
663
664
                case Splitter::BYTE_ARRAY_START:
665
                case Splitter::BYTE_INLINE:
666
                    // Minimal safeguards to improve error feedback. Theoretically such "garbage" could simply be ignored
667
                    // without causing problems to the parser, but it is probably best to report it as it could indicate
668
                    // the user expected X value but gets Y and doesn't notice why.
669
                    if ($captured !== null) {
670
                        throw $this->splitter->createErrorAtPosition('Unexpected content before array/inline start in associative array, ASCII: ' . ord($captured), 1559131849);
671
                    }
672
                    if (!isset($key) && !$numeric) {
673
                        throw $this->splitter->createErrorAtPosition('Unexpected array/inline start in associative array without preceding key', 1559131848);
674
                    }
675
676
                    // Encountering a curly brace or square bracket start byte will both cause a sub-array to be sequenced,
677
                    // the difference being that only the square bracket will cause third parameter ($numeric) passed to
678
                    // sequenceArrayNode() to be true, which in turn causes key-less items to be added with numeric indexes.
679
                    $key = $key ?? ++$itemCount;
680
                    $array[$key] = $this->sequenceArrayNode(null, $symbol === Splitter::BYTE_ARRAY_START);
681
                    $keyOrValue = null;
682
                    $key = null;
683
                    break;
684
685
                case Splitter::BYTE_QUOTE_SINGLE:
686
                case Splitter::BYTE_QUOTE_DOUBLE:
687
                    // Safeguard: if anything is captured before a quote this indicates garbage leading content. As with
688
                    // the garbage safeguards above, this one could theoretically be ignored in favor of silently making
689
                    // the odd syntax "just work".
690
                    if ($captured !== null) {
691
                        throw $this->splitter->createErrorAtPosition('Unexpected content before quote start in associative array, ASCII: ' . ord($captured), 1559145560);
692
                    }
693
694
                    // Quotes will always cause sequencing of the quoted string, but differs in behavior based on whether
695
                    // or not the $key is set. If $key is set, we know for sure we can assign a value. If it is not set
696
                    // we instead leave $keyOrValue defined so this will be processed by one of the next iterations.
697
                    $keyOrValue = $this->sequenceQuotedNode($countedEscapes);
698
                    if (isset($key)) {
699
                        $array[$key] = $keyOrValue->flatten(true);
700
                        $keyOrValue = null;
701
                        $key = null;
702
                        $countedEscapes = 0;
703
                    }
704
                    break;
705
706
                case Splitter::BYTE_SEPARATOR_COMMA:
707
                    // Comma separator: if we've collected a key or value, use it. Otherwise, use captured string.
708
                    // If neither key nor value nor captured string exists, ignore the comma (likely a tailing comma).
709
                    if (isset($keyOrValue)) {
710
                        // Key or value came as quoted string and exists in $keyOrValue
711
                        $potentialValue = $keyOrValue->flatten(true);
712
                        $key = $numeric ? ++$itemCount : $potentialValue;
713
                        $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 712 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...
714
                    } elseif (isset($captured)) {
715
                        $key = $key ?? ($numeric ? ++$itemCount : $captured);
716
                        if (!$numeric && isset($definitions) && !isset($definitions[$key])) {
717
                            throw $this->splitter->createUnsupportedArgumentError((string)$key, $definitions);
718
                        }
719
                        $array[$key] = is_numeric($captured) ? $captured + 0 : new ObjectAccessorNode($captured);
720
                    }
721
                    $keyOrValue = null;
722
                    $key = null;
723
                    break;
724
725
                case Splitter::BYTE_WHITESPACE_TAB:
726
                case Splitter::BYTE_WHITESPACE_RETURN:
727
                case Splitter::BYTE_WHITESPACE_EOL:
728
                case Splitter::BYTE_WHITESPACE_SPACE:
729
                    // Any whitespace attempts to set the key, if not already set. The captured string may be null as
730
                    // well, leaving the $key variable still null and able to be coalesced.
731
                    $key = $key ?? $captured;
732
                    break;
733
734
                case Splitter::BYTE_BACKSLASH:
735
                    // Escapes are simply counted and passed to the sequenceQuotedNode() method, causing that method
736
                    // to ignore exactly this number of backslashes before a matching quote is seen as closing quote.
737
                    ++$countedEscapes;
738
                    break;
739
740
                case Splitter::BYTE_INLINE_END:
741
                case Splitter::BYTE_ARRAY_END:
742
                case Splitter::BYTE_PARENTHESIS_END:
743
                    // Array end indication. Check if anything was collected previously or was captured currently,
744
                    // assign that to the array and return an ArrayNode with the full array inside.
745
                    $captured = $captured ?? (isset($keyOrValue) ? $keyOrValue->flatten(true) : null);
746
                    $key = $key ?? ($numeric ? ++$itemCount : $captured);
747
                    if (isset($captured, $key)) {
748
                        if (is_numeric($captured)) {
749
                            $array[$key] = $captured + 0;
750
                        } elseif (isset($keyOrValue)) {
751
                            $array[$key] = $keyOrValue->flatten();
752
                        } else {
753
                            $array[$key] = new ObjectAccessorNode($captured ?? $key);
754
                        }
755
                    }
756
                    if (!$numeric && isset($key, $definitions) && !isset($definitions[$key])) {
757
                        throw $this->splitter->createUnsupportedArgumentError((string)$key, $definitions);
758
                    }
759
                    $this->escapingEnabled = $escapingEnabledBackup;
760
                    return new ArrayNode($array);
761
            }
762
        }
763
764
        throw $this->splitter->createErrorAtPosition(
765
            'Unterminated array',
766
            1557748574
767
        );
768
    }
769
770
    /**
771
     * Sequence a quoted value
772
     *
773
     * The return can be either of:
774
     *
775
     * 1. A string value if source was for example "string"
776
     * 2. An integer if source was for example "1"
777
     * 3. A float if source was for example "1.25"
778
     * 4. A RootNode instance with multiple child nodes if source was for example "string {var}"
779
     *
780
     * The idea is to return the raw value if there is no reason for it to
781
     * be a node as such - which is only necessary if the quoted expression
782
     * contains other (dynamic) values like an inline syntax.
783
     *
784
     * @param int $leadingEscapes A backwards compatibility measure: when passed, this number of escapes must precede a closing quote for it to trigger node closing.
785
     * @param bool $allowArray
786
     * @return RootNode
787
     */
788
    protected function sequenceQuotedNode(int $leadingEscapes = 0, $allowArray = true): RootNode
789
    {
790
        $startingByte = $this->source->bytes[$this->splitter->index];
791
        $contextToRestore = $this->splitter->switch($this->contexts->quoted);
792
        $node = new RootNode();
793
        $this->splitter->sequence->next();
794
        $countedEscapes = 0;
795
796
        foreach ($this->splitter->sequence as $symbol => $captured) {
797
            switch ($symbol) {
798
799
                case Splitter::BYTE_ARRAY_START:
800
                    $countedEscapes = 0; // Theoretically not required but done in case of stray escapes (gets ignored)
801
                    if ($captured === null) {
802
                        // Array start "[" only triggers array sequencing if it is the very first byte in the quoted
803
                        // string - otherwise, it is added as part of the text.
804
                        $this->splitter->switch($this->contexts->array);
805
                        $node->addChildNode($this->sequenceArrayNode(null, $allowArray));
806
                        $this->splitter->switch($this->contexts->quoted);
807
                    } else {
808
                        $node->addChildNode(new TextNode($captured . '['));
809
                    }
810
                    break;
811
812
                case Splitter::BYTE_INLINE:
813
                    $countedEscapes = 0; // Theoretically not required but done in case of stray escapes (gets ignored)
814
                    // The quoted string contains a sub-expression. We extract the captured content so far and if it
815
                    // is not an empty string, add it as a child of the RootNode we're building, then we add the inline
816
                    // expression as next sibling and continue the loop.
817
                    if ($captured !== null) {
818
                        $childNode = new TextNode($captured);
819
                        $this->callInterceptor($childNode, InterceptorInterface::INTERCEPT_TEXT);
820
                        $node->addChildNode($childNode);
821
                    }
822
823
                    $node->addChildNode($this->sequenceInlineNodes());
824
                    $this->splitter->switch($this->contexts->quoted);
825
                    break;
826
827
                case Splitter::BYTE_BACKSLASH:
828
                    ++$countedEscapes;
829
                    if ($captured !== null) {
830
                        $node->addChildNode(new TextNode($captured));
831
                    }
832
                    break;
833
834
                // Note: although "case $startingByte:" could have been used here, it would not compile the switch
835
                // as a hash map and thus would not perform as well overall - when called frequently as it will be.
836
                // Backtick will only be encountered if the context is "protected" (insensitive inline sequencing)
837
                case Splitter::BYTE_QUOTE_SINGLE:
838
                case Splitter::BYTE_QUOTE_DOUBLE:
839
                case Splitter::BYTE_BACKTICK:
840
                    if ($symbol !== $startingByte || $countedEscapes !== $leadingEscapes) {
841
                        $node->addChildNode(new TextNode($captured . chr($symbol)));
842
                        $countedEscapes = 0; // If number of escapes do not match expected, reset the counter
843
                        break;
844
                    }
845
                    if ($captured !== null) {
846
                        $node->addChildNode(new TextNode($captured));
847
                    }
848
                    $this->splitter->switch($contextToRestore);
849
                    return $node;
850
            }
851
        }
852
853
        throw $this->splitter->createErrorAtPosition('Unterminated expression inside quotes', 1557700793);
854
    }
855
856
    /**
857
     * Call all interceptors registered for a given interception point.
858
     *
859
     * @param NodeInterface $node The syntax tree node which can be modified by the interceptors.
860
     * @param integer $interceptionPoint the interception point. One of the \TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface::INTERCEPT_* constants.
861
     * @return void
862
     */
863
    protected function callInterceptor(NodeInterface &$node, $interceptionPoint)
864
    {
865
        if ($this->escapingEnabled) {
866
            /** @var $interceptor InterceptorInterface */
867
            foreach ($this->configuration->getEscapingInterceptors($interceptionPoint) as $interceptor) {
868
                $node = $interceptor->process($node, $interceptionPoint, $this->state);
869
            }
870
        }
871
872
        /** @var $interceptor InterceptorInterface */
873
        foreach ($this->configuration->getInterceptors($interceptionPoint) as $interceptor) {
874
            $node = $interceptor->process($node, $interceptionPoint, $this->state);
875
        }
876
    }
877
}