Issues (287)

src/web/twig/CommentsTwigExtension.php (27 issues)

1
<?php
2
/**
3
 * Template Comments plugin for Craft CMS
4
 *
5
 * Adds a HTML comment to demarcate each Twig template that is included or extended.
6
 *
7
 * @link      https://nystudio107.com/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c)  nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\templatecomments\web\twig;
12
13
use nystudio107\templatecomments\TemplateComments;
14
use nystudio107\templatecomments\web\twig\tokenparsers\CommentBlockTokenParser;
15
use nystudio107\templatecomments\web\twig\tokenparsers\CommentsTokenParser;
16
use Twig\Environment;
17
use Twig\Error\LoaderError;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFunction;
20
21
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
22
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   TemplateComments
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
26
class CommentsTwigExtension extends AbstractExtension
27
{
28
    // Public Methods
29
    // =========================================================================
30
31
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
32
     * @inheritdoc
33
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
34
    public function getName(): string
35
    {
36
        return 'template-comments';
37
    }
38
39
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
40
     * @inheritdoc
41
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
42
    public function getFunctions()
43
    {
44
        return [
45
            new TwigFunction('source', [$this, 'originalSource'], ['needs_environment' => true, 'is_safe' => ['all']]),
46
        ];
47
    }
48
49
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
50
     * @inheritdoc
51
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
52
    public function getTokenParsers(): array
53
    {
54
        $parsers = [];
55
        if (TemplateComments::$settings->templateCommentsEnabled) {
56
            $parsers[] = new CommentsTokenParser();
57
        }
58
        if (TemplateComments::$settings->blockCommentsEnabled) {
59
            $parsers[] = new CommentBlockTokenParser();
60
        }
61
62
        return $parsers;
63
    }
64
65
    /**
66
     * Returns a template content without rendering it.
67
     *
68
     * @param Environment $env The Twig environment
0 ignored issues
show
Expected 11 spaces after parameter name; 1 found
Loading history...
69
     * @param string $name The template name
0 ignored issues
show
Expected 10 spaces after parameter name; 1 found
Loading history...
Expected 6 spaces after parameter type; 1 found
Loading history...
70
     * @param bool $ignoreMissing Whether to ignore missing templates or not
0 ignored issues
show
Expected 8 spaces after parameter type; 1 found
Loading history...
71
     *
72
     * @return string The template source
73
     */
74
    public function originalSource(Environment $env, string $name, bool $ignoreMissing = false): string
75
    {
76
        $loader = TemplateComments::$originalTwigLoader;
77
        try {
78
            return $loader->getSourceContext($name)->getCode();
79
        } catch (LoaderError $e) {
80
            if (!$ignoreMissing) {
81
                throw $e;
82
            }
83
        }
84
85
        return '';
86
    }
87
}
88