Completed
Pull Request — master (#470)
by Claus
01:59
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 array 
30
     */
31
    protected $stack = [];
32
33
    public function __construct(RenderingContextInterface $renderingContext)
34
    {
35
        $this->renderingContext = $renderingContext;
36
        $this->configuration = $renderingContext->getParserConfiguration();
37
    }
38
39
    public function getComponentBeingParsed(): ?ComponentInterface
40
    {
41
        return end($this->stack) ?: null;
42
    }
43
44
    public function parseFile(string $templatePathAndFilename, ?Configuration $configuration = null): ComponentInterface
0 ignored issues
show
Unused Code introduced by
The parameter $configuration 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...
45
    {
46
        $hash = sha1_file($templatePathAndFilename);
47
        return $this->stack[$hash] ?? $this->getOrParseAndStoreTemplate(
48
            $this->createIdentifierForFile($templatePathAndFilename, ''),
49
            function () use ($templatePathAndFilename): string {
50
                return file_get_contents($templatePathAndFilename);
51
            }
52
        );
53
    }
54
55
    /**
56
     * Parses a given template string and returns a parsed template object.
57
     *
58
     * The resulting ParsedTemplate can then be rendered by calling evaluate() on it.
59
     *
60
     * Normally, you should use a subclass of AbstractTemplateView instead of calling the
61
     * TemplateParser directly.
62
     *
63
     * @param string $templateString The template to parse as a string
64
     * @param Configuration|null Template parsing configuration to use
65
     * @return ComponentInterface Parsed template
66
     * @throws Exception
67
     */
68
    public function parse(string $templateString, ?Configuration $configuration = null): ComponentInterface
69
    {
70
        $hash = sha1($templateString);
71
        $source = new Source($templateString);
72
        $contexts = new Contexts();
73
        $sequencer = new Sequencer(
74
            $this->renderingContext,
75
            $contexts,
76
            $source,
77
            $configuration ?? $this->configuration
78
        );
79
        // Recursion support: triggering parsing of the same source file from within the file returns a reference
80
        // to the still unfinished EntryNode created by the Sequencer. The returned instance still does not have all
81
        // child nodes until the Sequencer has finished. The second time the template is parsed the temporary EntryNode
82
        // is returned to prevent infinite recursion.
83
        // The first instance now contains a circular reference to itself, as a branch nested in children somewhere.
84
        // Any subsequent usages of the same source creates additional references to the original/root/parent.
85
        $this->stack[$hash] = $sequencer->getComponent();
86
        $component = $sequencer->sequence();
87
        array_pop($this->stack);
88
        return $component;
89
    }
90
91
    /**
92
     * @param string $templateIdentifier
93
     * @param \Closure $templateSourceClosure Closure which returns the template source if needed
94
     * @return ComponentInterface
95
     */
96
    public function getOrParseAndStoreTemplate(string $templateIdentifier, \Closure $templateSourceClosure): ComponentInterface
97
    {
98
        if (!$this->configuration->isFeatureEnabled(Configuration::FEATURE_RUNTIME_CACHE)) {
99
            return $this->parseTemplateSource($templateIdentifier, $templateSourceClosure);
100
        }
101
        static $cache = [];
102
        if (!isset($cache[$templateIdentifier])) {
103
            $cache[$templateIdentifier] = $this->parseTemplateSource($templateIdentifier, $templateSourceClosure);
104
        }
105
        return $cache[$templateIdentifier];
106
    }
107
108
    /**
109
     * @param string $templateIdentifier
110
     * @param \Closure $templateSourceClosure
111
     * @return ComponentInterface
112
     */
113
    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...
114
    {
115
        $parsedTemplate = $this->parse(
116
            $templateSourceClosure($this->renderingContext),
117
            $this->renderingContext->getParserConfiguration()
118
        );
119
        //$parsedTemplate->setIdentifier($templateIdentifier);
120
        return $parsedTemplate;
121
    }
122
123
    /**
124
     * Returns a unique identifier for the given file in the format
125
     * <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<SHA1>
126
     * The SH1 hash is a checksum that is based on the file path and last modification date
127
     *
128
     * @param string $pathAndFilename
129
     * @param string $prefix
130
     * @return string
131
     */
132
    protected function createIdentifierForFile(string $pathAndFilename, string $prefix): string
133
    {
134
        return sprintf('%s_%s', $prefix, sha1($pathAndFilename));
135
    }
136
}
137