Passed
Push — develop ( e943aa...6182f5 )
by Andrew
03:20
created

Settings   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 83
dl 0
loc 247
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 5 1
A rules() 0 50 1
A init() 0 7 3
1
<?php
2
/**
3
 * Webperf plugin for Craft CMS 3.x
4
 *
5
 * Monitor the performance of your webpages through real-world user timing data
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) 2019 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\webperf\models;
12
13
use Craft;
0 ignored issues
show
Bug introduced by
The type Craft 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...
14
use craft\base\Model;
0 ignored issues
show
Bug introduced by
The type craft\base\Model 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...
15
use craft\validators\ArrayValidator;
0 ignored issues
show
Bug introduced by
The type craft\validators\ArrayValidator 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...
16
use craft\validators\ColorValidator;
0 ignored issues
show
Bug introduced by
The type craft\validators\ColorValidator 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...
17
18
use yii\behaviors\AttributeTypecastBehavior;
0 ignored issues
show
Bug introduced by
The type yii\behaviors\AttributeTypecastBehavior 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...
19
20
use putyourlightson\blitz\Blitz;
0 ignored issues
show
Bug introduced by
The type putyourlightson\blitz\Blitz 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...
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
25
 * @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...
26
 */
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...
27
class Settings extends Model
28
{
29
    // Constants
30
    // =========================================================================
31
32
    const BLITZ_PLUGIN_HANDLE = 'blitz';
33
34
    // Public Properties
35
    // =========================================================================
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @var string The public-facing name of the plugin
39
     */
40
    public $pluginName = 'Webperf';
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @var bool Whether or not to include the beacon on the page
44
     */
45
    public $includeBeacon = true;
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @var bool Whether or not to include the Craft profiling of pages
49
     */
50
    public $includeCraftProfiling = true;
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @var bool If the site is static cached, turn this option on to prevent Webperf from generating a unique beacon token
54
     */
55
    public $staticCachedSite = false;
56
57
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
58
     * @var int The number of data samples to store
59
     */
60
    public $dataSamplesStoredLimit = 100000;
61
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @var bool Whether the DataSamples should be trimmed after each new DataSample is added
64
     */
65
    public $automaticallyTrimDataSamples = true;
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @var bool Whether outlier data samples that are 10x the mean should be deleted
69
     */
70
    public $trimOutlierDataSamples = true;
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
73
     * @var int The number of milliseconds required between recording of frontend beacon data samples
74
     */
75
    public $rateLimitMs = 500;
76
77
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
78
     * @var string API Key for WebPageTest.org
79
     */
80
    public $webpageTestApiKey = '';
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @var array [Regular expressions](https://regexr.com/) to match URLs to exclude from tracking
84
     */
85
    public $excludePatterns = [
86
        0 => [
87
            'pattern' => '/webperf/.*',
88
        ],
89
        1 => [
90
            'pattern' => '/cpresources/.*',
91
        ]
92
    ];
93
94
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
95
     * @var bool Whether Craft `warning` messages should be recorded in addition to `error` messages
96
     */
97
    public $includeCraftWarnings = false;
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
100
     * @var int The number of error samples to store
101
     */
102
    public $errorSamplesStoredLimit = 1000;
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
105
     * @var bool Whether the ErrorSamples should be trimmed after each new ErrorSample is added
106
     */
107
    public $automaticallyTrimErrorSamples = true;
108
109
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
110
     * @var bool Whether to filter bot user agents from generating profile hits or not
111
     *           NOT visible in the GUI currently
112
     */
113
    public $filterBotUserAgents = true;
114
115
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
116
     * @var bool Whether the performance summary sidebar should be shown on entry, category, and product pages
117
     */
118
    public $displaySidebar = true;
119
120
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
121
     * @var string The dashboard 'fast' color for charts
122
     */
123
    public $dashboardFastColor = '#00C800';
124
125
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
126
     * @var string The dashboard 'average' color for charts
127
     */
128
    public $dashboardAverageColor = '#FFFF00';
129
130
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
131
     * @var string The dashboard 'slow' color for charts
132
     */
133
    public $dashboardSlowColor = '#C80000';
134
135
    // Threshold levels
136
    // =========================================================================
137
138
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
139
     * @var int Threshold in seconds for the dns metric, beyond which it will be considered slow
140
     */
141
    public $dnsThreshold = 0.5;
142
143
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
144
     * @var int Threshold in seconds for the connect metric, beyond which it will be considered slow
145
     */
146
    public $connectThreshold = 0.5;
147
148
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
149
     * @var int Threshold in seconds for the first byte metric, beyond which it will be considered slow
150
     */
151
    public $firstByteThreshold = 2.0;
152
153
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
154
     * @var int Threshold in seconds for the first paint metric, beyond which it will be considered slow
155
     */
156
    public $firstPaintThreshold = 5.0;
157
158
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
159
     * @var int Threshold in seconds for the first contentful paint metric, beyond which it will be considered slow
160
     */
161
    public $firstContentfulPaintThreshold = 5.0;
162
163
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
164
     * @var int Threshold in seconds for the DOM interactive metric, beyond which it will be considered slow
165
     */
166
    public $domInteractiveThreshold = 5.0;
167
168
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
169
     * @var int Threshold in seconds for the page load metric, beyond which it will be considered slow
170
     */
171
    public $pageLoadThreshold = 10.0;
172
173
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
174
     * @var int Threshold in seconds for the Craft execution metric, beyond which it will be considered slow
175
     */
176
    public $craftTotalMsThreshold = 2.0;
177
178
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
179
     * @var int Threshold in seconds for the database queries metric, beyond which it will be considered slow
180
     */
181
    public $craftDbMsThreshold = 2.0;
182
183
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
184
     * @var int Threshold in seconds for the Twig rendering metric, beyond which it will be considered slow
185
     */
186
    public $craftTwigMsThreshold = 2.0;
187
188
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
189
     * @var int Threshold in seconds for the Craft other metric, beyond which it will be considered slow
190
     */
191
    public $craftOtherMsThreshold = 2.0;
192
193
    // Public Methods
194
    // =========================================================================
195
196
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
197
     * @inheritdoc
198
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
199
    public function init()
200
    {
201
        parent::init();
202
        // If Blitz is installed & enabled, flip the $staticCachedSite on
203
        $blitz = Craft::$app->getPlugins()->getPlugin(self::BLITZ_PLUGIN_HANDLE);
204
        if ($blitz && Blitz::$plugin->getSettings()->cachingEnabled) {
205
            $this->staticCachedSite = true;
206
        }
207
    }
208
209
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
210
     * @inheritdoc
211
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
212
    public function rules()
213
    {
214
        return [
215
            ['pluginName', 'string'],
216
            ['pluginName', 'default', 'value' => 'Webperf'],
217
            ['includeBeacon', 'boolean'],
218
            ['includeCraftProfiling', 'boolean'],
219
            ['staticCachedSite', 'boolean'],
220
            ['dataSamplesStoredLimit', 'integer'],
221
            ['dataSamplesStoredLimit', 'default', 'value' => 100000],
222
            ['automaticallyTrimDataSamples', 'boolean'],
223
            ['trimOutlierDataSamples', 'boolean'],
224
            ['rateLimitMs', 'integer'],
225
            ['rateLimitMs', 'default', 'value' => 500],
226
            ['webpageTestApiKey', 'string'],
227
            ['excludePatterns', ArrayValidator::class],
228
            ['includeCraftWarnings', 'boolean'],
229
            ['errorSamplesStoredLimit', 'integer'],
230
            ['errorSamplesStoredLimit', 'default', 'value' => 1000],
231
            ['automaticallyTrimErrorSamples', 'boolean'],
232
            ['filterBotUserAgents', 'boolean'],
233
            ['displaySidebar', 'boolean'],
234
            ['dashboardFastColor', 'default', 'value' => '#00C800'],
235
            ['dashboardAverageColor', 'default', 'value' => '#FFA500'],
236
            ['dashboardSlowColor', 'default', 'value' => '#C80000'],
237
            [
238
                [
239
                    'dashboardFastColor',
240
                    'dashboardAverageColor',
241
                    'dashboardSlowColor',
242
                ],
243
                ColorValidator::class
244
            ],
245
            [
246
                [
247
                    'dnsThreshold',
248
                    'connectThreshold',
249
                    'firstByteThreshold',
250
                    'firstPaintThreshold',
251
                    'firstContentfulPaintThreshold',
252
                    'domInteractiveThreshold',
253
                    'pageLoadThreshold',
254
                    'craftTotalMsThreshold',
255
                    'craftDbMsThreshold',
256
                    'craftTwigMsThreshold',
257
                    'craftOtherMsThreshold',
258
                ],
259
                'number',
260
                'min' => 0.1,
261
                'max' => 100,
262
            ]
263
        ];
264
    }
265
266
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
267
     * @return array
268
     */
269
    public function behaviors()
270
    {
271
        return [
272
            'typecast' => [
273
                'class' => AttributeTypecastBehavior::class,
274
                // 'attributeTypes' will be composed automatically according to `rules()`
275
            ],
276
        ];
277
    }
278
}
279