Passed
Push — v1 ( 2498ee...2209c2 )
by Andrew
13:32 queued 07:58
created

CommentsTwigExtension::originalSource()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Template Comments plugin for Craft CMS 3.x
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
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
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\CommentsTokenParser;
15
use nystudio107\templatecomments\web\twig\tokenparsers\CommentBlockTokenParser;
16
17
use Craft;
18
19
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
21
 * @package   TemplateComments
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
22
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
23
 */
0 ignored issues
show
Coding Style introduced by
Missing @category 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...
24
class CommentsTwigExtension extends \Twig_Extension
25
{
26
    // Public Methods
27
    // =========================================================================
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @inheritdoc
31
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
32
    public function getName()
33
    {
34
        return 'template-comments';
35
    }
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @inheritdoc
39
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
40
    public function getFunctions()
41
    {
42
        return [
43
            new \Twig_Function('source', [$this, 'originalSource'], ['needs_environment' => true, 'is_safe' => ['all']]),
44
        ];
45
    }
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @inheritdoc
49
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
50
    public function getTokenParsers(): array
51
    {
52
        $parsers = [];
53
        if (TemplateComments::$settings->templateCommentsEnabled) {
54
            $parsers[] = new CommentsTokenParser();
55
        }
56
        if (TemplateComments::$settings->blockCommentsEnabled) {
57
            $parsers[] = new CommentBlockTokenParser();
58
        }
59
60
        return $parsers;
61
    }
62
63
    /**
64
     * Returns a template content without rendering it.
65
     *
66
     * @param \Twig_Environment $env
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
67
     * @param string           $name          The template name
0 ignored issues
show
Coding Style introduced by
Expected 12 spaces after parameter type; 11 found
Loading history...
68
     * @param bool             $ignoreMissing Whether to ignore missing templates or not
0 ignored issues
show
Coding Style introduced by
Expected 14 spaces after parameter type; 13 found
Loading history...
69
     *
70
     * @return string The template source
71
     */
72
    function originalSource($env, $name, $ignoreMissing = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
    {
74
        $loader = TemplateComments::$originalTwigLoader;
75
        try {
76
            return $loader->getSourceContext($name)->getCode();
77
        } catch (\Twig_Error_Loader $e) {
78
            if (!$ignoreMissing) {
79
                throw $e;
80
            }
81
        }
82
    }
83
}
84