Passed
Push — v1 ( 76f4e4...83a102 )
by Andrew
19:06 queued 02:54
created

Settings::behaviors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
292
    }
293
}
294