Issues (287)

web/twig/tokenparsers/CommentBlockTokenParser.php (11 issues)

1
<?php
2
0 ignored issues
show
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
25
/**
26
 * Marks a section of a template as being reusable.
27
 *
28
 * <pre>
29
 *  {% block head %}
30
 *    <link rel="stylesheet" href="style.css" />
31
 *    <title>{% block title %}{% endblock %} - My Webpage</title>
32
 *  {% endblock %}
33
 * </pre>
34
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @author tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
35
final class CommentBlockTokenParser extends AbstractTokenParser
36
{
37
    public function parse(Token $token): BlockReferenceNode
0 ignored issues
show
Missing doc comment for function parse()
Loading history...
38
    {
39
        $lineno = $token->getLine();
40
        $stream = $this->parser->getStream();
41
        $name = $stream->expect(/* Twig_Token::NAME_TYPE */ 5)->getValue();
42
        if ($this->parser->hasBlock($name)) {
43
            throw new SyntaxError(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext());
44
        }
45
46
        // Exclude certain blocks from being CommentBlockNodes
47
        $blockClass = CommentBlockNode::class;
48
        $settings = TemplateComments::$settings;
49
        foreach ($settings->excludeBlocksThatContain as $excludeString) {
50
            if (stripos($name, $excludeString) !== false) {
51
                $blockClass = BlockNode::class;
52
            }
53
        }
54
55
        $this->parser->setBlock($name, $block = new $blockClass($name, new Node(array()), $lineno));
56
        $this->parser->pushLocalScope();
57
        $this->parser->pushBlockStack($name);
58
59
        if ($stream->nextIf(/* Twig_Token::BLOCK_END_TYPE */ 3) !== null) {
60
            $body = $this->parser->subparse(fn(Token $token): bool => $this->decideBlockEnd($token), true);
61
            if (($token = $stream->nextIf(/* Twig_Token::NAME_TYPE */ 5)) !== null) {
62
                $value = $token->getValue();
63
64
                if ($value !== $name) {
65
                    throw new SyntaxError(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext());
66
                }
67
            }
68
        } else {
69
            $body = new Node(array(
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
70
                new PrintNode($this->parser->getExpressionParser()->parseExpression(), $lineno),
71
            ));
0 ignored issues
show
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...
72
        }
73
74
        $stream->expect(/* Twig_Token::BLOCK_END_TYPE */ 3);
75
76
        $block->setNode('body', $body);
77
        $this->parser->popBlockStack();
78
        $this->parser->popLocalScope();
79
80
        return new BlockReferenceNode($name, $lineno, $this->getTag());
81
    }
82
83
    public function decideBlockEnd(Token $token): bool
0 ignored issues
show
Missing doc comment for function decideBlockEnd()
Loading history...
84
    {
85
        return $token->test('endblock');
86
    }
87
88
    public function getTag(): string
0 ignored issues
show
Missing doc comment for function getTag()
Loading history...
89
    {
90
        return 'block';
91
    }
92
}
93