CommentBlockTokenParser::parse()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 6
eloc 25
c 3
b 1
f 0
nc 12
nop 1
dl 0
loc 40
rs 8.8977
1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) Fabien Potencier
7
 * (c) Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace nystudio107\templatecomments\web\twig\tokenparsers;
14
15
use nystudio107\templatecomments\TemplateComments;
16
use nystudio107\templatecomments\web\twig\nodes\CommentBlockNode;
17
use Twig\Error\SyntaxError;
18
use Twig\Node\BlockNode;
19
use Twig\Node\BlockReferenceNode;
20
use Twig\Node\Node;
21
use Twig\Node\PrintNode;
22
use Twig\Token;
23
use Twig\TokenParser\AbstractTokenParser;
24
use function sprintf;
25
26
/**
27
 * Marks a section of a template as being reusable.
28
 *
29
 * <pre>
30
 *  {% block head %}
31
 *    <link rel="stylesheet" href="style.css" />
32
 *    <title>{% block title %}{% endblock %} - My Webpage</title>
33
 *  {% endblock %}
34
 * </pre>
35
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
36
final class CommentBlockTokenParser extends AbstractTokenParser
37
{
38
    public function parse(Token $token): Node
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function parse()
Loading history...
39
    {
40
        $lineno = $token->getLine();
41
        $stream = $this->parser->getStream();
42
        $name = $stream->expect(Token::NAME_TYPE)->getValue();
43
44
        // Exclude certain blocks from being CommentBlockNodes
45
        $blockClass = CommentBlockNode::class;
46
        $settings = TemplateComments::$settings;
47
        foreach ($settings->excludeBlocksThatContain as $excludeString) {
48
            if (stripos($name, $excludeString) !== false) {
49
                $blockClass = BlockNode::class;
50
            }
51
        }
52
53
        $this->parser->setBlock($name, $block = new $blockClass($name, new Node([]), $lineno));
54
        $this->parser->pushLocalScope();
55
        $this->parser->pushBlockStack($name);
56
57
        if ($stream->nextIf(Token::BLOCK_END_TYPE)) {
58
            $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
59
            if ($token = $stream->nextIf(Token::NAME_TYPE)) {
60
                $value = $token->getValue();
61
62
                if ($value != $name) {
63
                    throw new SyntaxError(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext());
64
                }
65
            }
66
        } else {
67
            $body = new Node([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
68
                new PrintNode($this->parser->getExpressionParser()->parseExpression(), $lineno),
69
            ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
70
        }
71
        $stream->expect(Token::BLOCK_END_TYPE);
72
73
        $block->setNode('body', $body);
74
        $this->parser->popBlockStack();
75
        $this->parser->popLocalScope();
76
77
        return new BlockReferenceNode($name, $lineno);
78
    }
79
80
    public function decideBlockEnd(Token $token): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function decideBlockEnd()
Loading history...
81
    {
82
        return $token->test('endblock');
83
    }
84
85
    public function getTag(): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getTag()
Loading history...
86
    {
87
        return 'block';
88
    }
89
}
90