GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ModuleCompiler::compileLoadTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
namespace TwigJs\Compiler;
3
4
use TwigJs\JsCompiler;
5
use Twig_NodeInterface;
6
7
abstract class ModuleCompiler
8
{
9
    protected $functionName;
10
11
    abstract protected function compileClassHeader(JsCompiler $compiler, Twig_NodeInterface $node);
12
    abstract protected function compileClassFooter(JsCompiler $compiler, Twig_NodeInterface $node);
13
14
    public function getType()
15
    {
16
        return 'Twig_Node_Module';
17
    }
18
19
    public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
20
    {
21
        if (!$node instanceof \Twig_Node_Module) {
22
            throw new \RuntimeException(
23
                sprintf(
24
                    '$node must be an instanceof of \Module, but got "%s".',
25
                    get_class($node)
26
                )
27
            );
28
        }
29
        $this->compileTemplate($compiler, $node);
30
    }
31
32
    protected function compileTemplate(JsCompiler $compiler, \Twig_NodeInterface $node)
33
    {
34
        $this->compileClassHeader($compiler, $node);
35
36
        $this->compileGetParent($compiler, $node);
37
        $this->compileDisplayHeader($compiler, $node);
38
        $this->compileDisplayBody($compiler, $node);
39
        $this->compileDisplayFooter($compiler, $node);
40
41
        $compiler->subcompile($node->getNode('blocks'));
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
43
        $this->compileMacros($compiler, $node);
44
45
        $this->compileGetTemplateName($compiler, $node);
46
47
        $this->compileIsTraitable($compiler, $node);
48
        $this->compileClassFooter($compiler, $node);
49
    }
50
51
    protected function compileGetParent(JsCompiler $compiler, \Twig_NodeInterface $node)
52
    {
53
        $compiler
54
            ->write("/**\n", " * @inheritDoc\n", " */\n")
55
            ->write($this->functionName.".prototype.getParent_ = function(context) {\n")
56
            ->indent()
57
            ->write('return ')
58
        ;
59
60
        if (!$this->hasParentNode($node)) {
61
            $compiler->repr(false);
62
        } else {
63
            $compiler
64
                ->setTemplateName(true)
65
                ->subcompile($node->getNode('parent'))
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
66
                ->setTemplateName(false)
67
            ;
68
        }
69
70
        $compiler
71
            ->raw(";\n")
72
            ->outdent()
73
            ->write("};\n\n")
74
        ;
75
    }
76
77
    protected function compileDisplayBody(JsCompiler $compiler, \Twig_NodeInterface $node)
78
    {
79
        $compiler
80
            ->enterScope()
81
            ->subcompile($node->getNode('body'))
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
82
            ->leaveScope()
83
        ;
84
85
        if ($this->hasParentNode($node)) {
86
            $compiler
87
                ->write("this.getParent(context).render_(sb, context, twig.extend({}, this.getBlocks(), blocks));\n")
88
            ;
89
        }
90
    }
91
92
    protected function compileConstructor(JsCompiler $compiler, \Twig_NodeInterface $node)
93
    {
94
        $compiler->raw("\n");
95
96
        $countTraits = count($node->getNode('traits'));
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
97
        if ($countTraits) {
98
            // traits
99
            foreach ($node->getNode('traits') as $i => $trait) {
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
100
                $this->compileLoadTemplate($compiler, $trait->getNode('template'), sprintf('trait_%s', $i));
101
102
                $compiler
103
                    ->addDebugInfo($trait->getNode('template'))
104
                    ->write(sprintf("if (!trait_%s.isTraitable()) {\n", $i))
105
                    ->indent()
106
                    ->write(
107
                        "throw Error('Template \"' + trait_".$i.".getTemplateName() + " .
108
                        "'\" cannot be used as a trait.');\n"
109
                    )->outdent()
110
                    ->write("}\n")
111
                    ->write(sprintf("var trait_%s_blocks = trait_%s.getBlocks();\n\n", $i, $i))
112
                ;
113
114
                foreach ($trait->getNode('targets') as $key => $value) {
115
                    $compiler
116
                        ->write(sprintf("trait_%s_blocks[", $i))
117
                        ->subcompile($value)
118
                        ->raw(sprintf("] = trait_%s_blocks[", $i))
119
                        ->string($key)
120
                        ->raw(sprintf("]; delete trait_%s_blocks[", $i))
121
                        ->string($key)
122
                        ->raw("];\n\n")
123
                    ;
124
                }
125
            }
126
127
            $compiler
128
                ->write("var traits = twig.extend({},\n")
129
                ->indent()
130
            ;
131
132
            for ($i = 0; $i < $countTraits; $i++) {
133
                $compiler
134
                    ->write(sprintf("trait_%s_blocks".($i == $countTraits - 1 ? '' : ',')."\n", $i))
135
                ;
136
            }
137
138
            $compiler
139
                ->outdent()
140
                ->write(");\n")
141
                ->write("this.setTraits(traits);\n\n")
142
            ;
143
144
            $compiler
145
                ->write("this.setBlocks(twig.extend({}, traits, {\n")
146
            ;
147
        } else {
148
            $compiler
149
                ->write("this.setBlocks({\n")
150
            ;
151
        }
152
153
        // blocks
154
        $compiler
155
            ->indent()
156
        ;
157
158
        $first = true;
159
        foreach ($node->getNode('blocks') as $name => $node) {
160
            if (!$first) {
161
                $compiler->raw(",\n");
162
            }
163
            $first = false;
164
165
            $compiler
166
                ->write(sprintf("'%s': twig.bind(this.block_%s, this)", $name, $name))
167
            ;
168
        }
169
170
        if (!$first) {
171
            $compiler->raw("\n");
172
        }
173
174
        if ($countTraits) {
175
            $compiler
176
                ->outdent()
177
                ->write("}));\n")
178
            ;
179
        } else {
180
            $compiler
181
                ->outdent()
182
                ->write("});\n")
183
            ;
184
        }
185
    }
186
187
    protected function compileDisplayHeader(JsCompiler $compiler, \Twig_NodeInterface $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node 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...
188
    {
189
        $compiler
190
            ->write("/**\n", " * @inheritDoc\n", " */\n")
191
            ->write($this->functionName.".prototype.render_ = function(sb, context, blocks) {\n")
192
            ->indent()
193
            ->write("blocks = typeof(blocks) == \"undefined\" ? {} : blocks;"."\n")
194
        ;
195
    }
196
197
    protected function compileDisplayFooter(JsCompiler $compiler, \Twig_NodeInterface $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node 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...
198
    {
199
        $compiler
200
            ->outdent()
201
            ->write("};\n\n")
202
        ;
203
    }
204
205
    protected function compileMacros(JsCompiler $compiler, \Twig_NodeInterface $node)
206
    {
207
        $compiler->subcompile($node->getNode('macros'));
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
208
    }
209
210
    protected function compileGetTemplateName(JsCompiler $compiler, \Twig_NodeInterface $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node 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...
211
    {
212
        $compiler
213
            ->write("/**\n", " * @inheritDoc\n", " */\n")
214
            ->write($this->functionName.".prototype.getTemplateName = function() {\n")
215
            ->indent()
216
            ->write('return '.json_encode($this->functionName).";\n")
217
            ->outdent()
218
            ->write("};\n\n")
219
        ;
220
    }
221
222
    protected function compileIsTraitable(JsCompiler $compiler, \Twig_NodeInterface $node)
223
    {
224
        // A template can be used as a trait if all of the following is true:
225
        //   * it has no parent
226
        //   * it has no macros
227
        //   * it has no body
228
        //
229
        // Put another way, a template can be used as a trait if it
230
        // only contains blocks and use statements.
231
        $traitable = null === $this->hasParentNode($node) && 0 === count($node->getNode('macros'));
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
232
        if ($traitable) {
233
            if (!count($nodes = $node->getNode('body'))) {
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
234
                $nodes = new Twig_Node(array($node->getNode('body')));
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
235
            }
236
237
            foreach ($nodes as $node) {
238
                if (!count($node)) {
239
                    continue;
240
                }
241
242
                if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) {
0 ignored issues
show
Bug introduced by
The class TwigJs\Compiler\Twig_Node_Text does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
243
                    continue;
244
                }
245
246
                if ($node instanceof Twig_Node_BlockReference) {
0 ignored issues
show
Bug introduced by
The class TwigJs\Compiler\Twig_Node_BlockReference does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
247
                    continue;
248
                }
249
250
                $traitable = false;
251
                break;
252
            }
253
        }
254
255
        $compiler
256
            ->write(
257
                "/**\n",
258
                " * Returns whether this template can be used as trait.\n",
259
                " *\n",
260
                " * @return {boolean}\n",
261
                " */\n"
262
            )->write($this->functionName.".prototype.isTraitable = function() {\n")
263
            ->indent()
264
            ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
265
            ->outdent()
266
            ->write("};\n")
267
        ;
268
    }
269
270
    public function compileLoadTemplate(JsCompiler $compiler, $node, $var)
271
    {
272
        $compiler->isTemplateName = true;
273
        $compiler
274
            ->write(sprintf("var %s = this.env_.createTemplate(", $var))
275
            ->subcompile($node)
276
            ->raw(");\n")
277
        ;
278
        $compiler->isTemplateName = false;
279
    }
280
281 View Code Duplication
    private function hasParentNode(\Twig_NodeInterface $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
282
    {
283
        if (!$node->hasNode('parent')) {
284
            return false;
285
        }
286
287
        if (null === $node->getNode('parent')) {
0 ignored issues
show
Bug introduced by
The method getNode() does not exist on Twig_NodeInterface. Did you maybe mean getNodeTag()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
288
            return false;
289
        }
290
291
        return true;
292
    }
293
}
294