Passed
Push — develop ( 1c3c4e...f88f36 )
by Andrew
20:14
created

TemplateComments::installEventListeners()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 7
c 2
b 0
f 0
nc 5
nop 0
dl 0
loc 14
rs 8.8333
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;
12
13
use Craft;
14
use craft\base\Plugin;
15
use nystudio107\templatecomments\models\Settings;
16
use nystudio107\templatecomments\web\twig\CommentsTwigExtension;
17
use nystudio107\templatecomments\web\twig\CommentTemplateLoader;
0 ignored issues
show
Bug introduced by
The type nystudio107\templatecomm...g\CommentTemplateLoader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Class TemplateComments
21
 *
22
 * @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 for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   TemplateComments
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @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 for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 *
26
 */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
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...
27
class TemplateComments extends Plugin
28
{
29
    // Static Properties
30
    // =========================================================================
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @var TemplateComments
34
     */
35
    public static $plugin;
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @var Settings $settings
39
     */
40
    public static $settings;
41
42
    // Public Properties
43
    // =========================================================================
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var string
47
     */
48
    public $schemaVersion = '1.0.0';
49
50
    // Public Methods
51
    // =========================================================================
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @inheritdoc
55
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
56
    public function init()
57
    {
58
        parent::init();
59
        // Initialize properties
60
        self::$plugin = $this;
61
        self::$settings = $this->getSettings();
62
        // Add in our Craft components
63
        $this->addComponents();
64
65
        Craft::info(
66
            Craft::t(
67
                'templatecomments',
68
                '{name} plugin loaded',
69
                ['name' => $this->name]
70
            ),
71
            __METHOD__
72
        );
73
    }
74
75
    // Protected Methods
76
    // =========================================================================
77
78
    /**
79
     * Add in our Craft components
80
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
81
    protected function addComponents()
82
    {
83
        $request = Craft::$app->getRequest();
84
        if (!$request->getIsConsoleRequest()) {
85
            // Do nothing at all on AJAX requests
86
            if ($request->getIsAjax()) {
87
                return;
88
            }
89
            // Install only for site requests
90
            if ($request->getIsSiteRequest()) {
91
                $this->installSiteComponents();
92
            }
93
            // Install only for Control Panel requests
94
            if ($request->getIsCpRequest()) {
95
                $this->installCpComponents();
96
            }
97
        }
98
    }
99
100
    /**
101
     * Install components for site requests only
102
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
103
    protected function installSiteComponents()
104
    {
105
        if (self::$settings->siteTemplateComments) {
106
            $this->installTemplateComponents();
107
        }
108
    }
109
110
    /**
111
     * Install components for Control Panel requests only
112
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
113
    protected function installCpComponents()
114
    {
115
        if (self::$settings->cpTemplateComments) {
116
            $this->installTemplateComponents();
117
        }
118
    }
119
120
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
121
     * @inheritdoc
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    protected function createSettingsModel()
124
    {
125
        return new Settings();
126
    }
127
128
    // Private Methods
129
    // =========================================================================
130
131
    /**
132
     * Install our template components
133
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
134
    private function installTemplateComponents()
0 ignored issues
show
Coding Style introduced by
Private method name "TemplateComments::installTemplateComponents" must be prefixed with an underscore
Loading history...
135
    {
136
        $devMode = Craft::$app->getConfig()->getGeneral()->devMode;
137
        if (!self::$settings->onlyCommentsInDevMode
138
            || (self::$settings->onlyCommentsInDevMode && $devMode)) {
0 ignored issues
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
139
            Craft::$app->view->registerTwigExtension(new CommentsTwigExtension());
140
        }
141
    }
142
}
143