Completed
Pull Request — master (#470)
by Claus
01:32
created

TemplateParser::textAndShorthandSyntaxHandler()   C

Complexity

Conditions 16
Paths 26

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
nc 26
nop 3
dl 0
loc 71
rs 5.5666
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
namespace TYPO3Fluid\Fluid\Core\Parser;
4
5
/*
6
 * This file belongs to the package "TYPO3 Fluid".
7
 * See LICENSE.txt that was shipped with this package.
8
 */
9
10
use TYPO3Fluid\Fluid\Component\ComponentInterface;
11
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
12
13
/**
14
 * Template parser building up an object syntax tree
15
 */
16
class TemplateParser
17
{
18
    /**
19
     * @var Configuration
20
     */
21
    protected $configuration;
22
23
    /**
24
     * @var RenderingContextInterface
25
     */
26
    protected $renderingContext;
27
28
    /**
29
     * @var ComponentInterface[]
30
     */
31
    protected $stack = [];
32
33
    /**
34
     * @var Source[]
35
     */
36
    protected $sources = [];
37
38
    public function __construct(RenderingContextInterface $renderingContext)
39
    {
40
        $this->renderingContext = $renderingContext;
41
        $this->configuration = $renderingContext->getParserConfiguration();
42
    }
43
44
    public function getComponentBeingParsed(): ?ComponentInterface
45
    {
46
        return end($this->stack) ?: null;
47
    }
48
49
    public function parseFile(string $templatePathAndFilename, ?Configuration $configuration = null): ComponentInterface
50
    {
51
        $hash = sha1_file($templatePathAndFilename);
52
        $source = $this->sources[$hash] ?? ($this->sources[$hash] = new FileSource($templatePathAndFilename));
53
        if (!($configuration ?? $this->configuration)->isFeatureEnabled(Configuration::FEATURE_RUNTIME_CACHE)) {
54
            return ($this->stack[$hash] ?? $this->parse($source));
55
        }
56
        static $cache = [];
57
        if (isset($cache[$hash])) {
58
            return $cache[$hash];
59
        }
60
        return $cache[$hash] = ($this->stack[$hash] ?? $this->parse($source));
61
    }
62
63
    /**
64
     * Parses a given template string and returns a parsed template object.
65
     *
66
     * The resulting ParsedTemplate can then be rendered by calling evaluate() on it.
67
     *
68
     * Normally, you should use a subclass of AbstractTemplateView instead of calling the
69
     * TemplateParser directly.
70
     *
71
     * @param Source $source Template source instance
72
     * @param Configuration|null Template parsing configuration to use
73
     * @return ComponentInterface Parsed template
74
     * @throws Exception
75
     */
76
    public function parse(Source $source, ?Configuration $configuration = null): ComponentInterface
77
    {
78
        $hash = sha1($source->source);
79
        $contexts = new Contexts();
80
        $sequencer = new Sequencer(
81
            $this->renderingContext,
82
            $contexts,
83
            $source,
84
            $configuration ?? $this->configuration
85
        );
86
        // Recursion support: triggering parsing of the same source file from within the file returns a reference
87
        // to the still unfinished EntryNode created by the Sequencer. The returned instance still does not have all
88
        // child nodes until the Sequencer has finished. The second time the template is parsed the temporary EntryNode
89
        // is returned to prevent infinite recursion.
90
        // The first instance now contains a circular reference to itself, as a branch nested in children somewhere.
91
        // Any subsequent usages of the same source creates additional references to the original/root/parent.
92
        $this->stack[$hash] = $sequencer->getComponent();
93
        $component = $sequencer->sequence();
94
        array_pop($this->stack);
95
        return $component;
96
    }
97
98
    /**
99
     * @param string $templateIdentifier
100
     * @param \Closure $templateSourceClosure Closure which returns the template source if needed
101
     * @return ComponentInterface
102
     */
103
    public function getOrParseAndStoreTemplate(string $templateIdentifier, \Closure $templateSourceClosure): ComponentInterface
104
    {
105
        if (!$this->configuration->isFeatureEnabled(Configuration::FEATURE_RUNTIME_CACHE)) {
106
            return $this->parseTemplateSource($templateIdentifier, $templateSourceClosure);
107
        }
108
        static $cache = [];
109
        if (!isset($cache[$templateIdentifier])) {
110
            $cache[$templateIdentifier] = $this->parseTemplateSource($templateIdentifier, $templateSourceClosure);
111
        }
112
        return $cache[$templateIdentifier];
113
    }
114
115
    /**
116
     * @param string $templateIdentifier
117
     * @param \Closure $templateSourceClosure
118
     * @return ComponentInterface
119
     */
120
    protected function parseTemplateSource(string $templateIdentifier, \Closure $templateSourceClosure): ComponentInterface
0 ignored issues
show
Unused Code introduced by
The parameter $templateIdentifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        $parsedTemplate = $this->parse(
123
            new Source($templateSourceClosure($this->renderingContext)),
124
            $this->renderingContext->getParserConfiguration()
125
        );
126
        //$parsedTemplate->setIdentifier($templateIdentifier);
127
        return $parsedTemplate;
128
    }
129
}
130