Issues (287)

src/web/twig/CommentTemplateLoader.php (24 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 Craft;
14
use craft\web\twig\TemplateLoader;
15
use craft\web\twig\TemplateLoaderException;
16
use Twig\Source;
17
18
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
19
 * @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...
20
 * @package   TemplateComments
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @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...
22
 */
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...
23
class CommentTemplateLoader extends TemplateLoader
24
{
25
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $name should have a doc-comment as per coding-style.
Loading history...
26
     * @inheritdoc
27
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
28
    public function getSourceContext($name): Source
29
    {
30
        $template = $this->_resolveTemplate($name);
31
32
        if (!is_readable($template)) {
33
            throw new TemplateLoaderException($name, Craft::t('app', 'Tried to read the template at {path}, but could not. Check the permissions.', ['path' => $template]));
34
        }
35
        $escapedName = addslashes($name);
36
        $prefix = "{% comments '{$escapedName}' %}" . PHP_EOL;
37
        $suffix = PHP_EOL . "{% endcomments %}";
38
39
        return new Source($prefix . file_get_contents($template) . $suffix, $name, $template);
40
    }
41
42
    // Private Methods
43
    // =========================================================================
44
45
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $name should have a doc-comment as per coding-style.
Loading history...
46
     * @inheritdoc
47
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
48
    private function _resolveTemplate(string $name): string
49
    {
50
        $template = $this->view->resolveTemplate($name);
0 ignored issues
show
The method resolveTemplate() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        /** @scrutinizer ignore-call */ 
51
        $template = $this->view->resolveTemplate($name);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
52
        if ($template !== false) {
53
            return $template;
54
        }
55
56
        throw new TemplateLoaderException($name, Craft::t('app', 'Unable to find the template “{template}”.', ['template' => $name]));
57
    }
58
}
59