Passed
Push — v1 ( 7dfb05...0f87a2 )
by Andrew
07:09 queued 04:02
created

Recommendations::list()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\services;
12
13
use nystudio107\webperf\base\Recommendation;
14
use nystudio107\webperf\models\RecommendationDataSample;
15
use nystudio107\webperf\recommendations\CraftQueryCount;
16
use nystudio107\webperf\recommendations\CraftQueryTime;
17
use nystudio107\webperf\recommendations\CraftTotalTime;
18
use nystudio107\webperf\recommendations\CraftTwigTime;
19
use nystudio107\webperf\recommendations\DomInteractive;
20
use nystudio107\webperf\recommendations\FirstByte;
21
use nystudio107\webperf\recommendations\FirstContentfulPaint;
22
use nystudio107\webperf\recommendations\MemoryLimit;
23
24
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...
25
use craft\base\Component;
0 ignored issues
show
Bug introduced by
The type craft\base\Component 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...
26
use craft\db\Query;
0 ignored issues
show
Bug introduced by
The type craft\db\Query 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...
27
28
use yii\helpers\Markdown;
0 ignored issues
show
Bug introduced by
The type yii\helpers\Markdown 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...
29
30
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
 * @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...
32
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
33
 * @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...
34
 */
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...
35
class Recommendations extends Component
36
{
37
    // Constants
38
    // =========================================================================
39
40
    const RECOMMENDATIONS_LIST = [
41
        CraftQueryCount::class,
42
        CraftQueryTime::class,
43
        CraftTwigTime::class,
44
        CraftTotalTime::class,
45
        FirstByte::class,
46
        FirstContentfulPaint::class,
47
        DomInteractive::class,
48
        MemoryLimit::class,
49
    ];
50
51
    // Public Methods
52
    // =========================================================================
53
54
    /**
55
     * Return a list of recommendations
56
     *
57
     * @param RecommendationDataSample $sample
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     *
59
     * @return array
60
     */
61
    public function list(RecommendationDataSample $sample): array
62
    {
63
        $data = [];
64
        foreach (self::RECOMMENDATIONS_LIST as $recClass) {
65
            /** @var Recommendation $rec */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
66
            $rec = new $recClass(['sample' => $sample]);
67
            if ($rec->hasRecommendation) {
68
                $data[] = [
69
                    'summary' => Markdown::processParagraph($rec->summary),
70
                    'detail' => Markdown::processParagraph($rec->detail),
71
                    'learnMoreUrl' => $rec->learnMoreUrl,
72
                ];
73
            }
74
        }
75
        return $data;
76
    }
77
78
    /**
79
     * Return a list of recommendations
80
     *
81
     * @param string $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     * @param string $start
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
83
     * @param string $end
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @param int    $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     *
86
     * @return array
87
     */
88
    public function data(
89
        $pageUrl = '',
90
        string $start = '',
91
        string $end = '',
92
        $siteId = 0
93
    ): array {
94
        $data = [];
95
        $db = Craft::$app->getDb();
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
96
        // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00
97
        $end = date('Y-m-d', strtotime($end.'+1 day'));
98
        $pageUrl = urldecode($pageUrl);
99
        // Query the db table
100
        $query = (new Query())
101
            ->select([
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...
102
                'COUNT([[url]]) AS [[cnt]]',
103
104
                'AVG([[pageLoad]]) AS [[pageLoad]]',
105
                'AVG([[domInteractive]]) AS [[domInteractive]]',
106
                'AVG([[firstContentfulPaint]]) AS [[firstContentfulPaint]]',
107
                'AVG([[firstPaint]]) AS [[firstPaint]]',
108
                'AVG([[firstByte]]) AS [[firstByte]]',
109
                'AVG([[connect]]) AS [[connect]]',
110
                'AVG([[dns]]) AS [[dns]]',
111
112
                'AVG([[craftTotalMs]]) AS [[craftTotalMs]]',
113
                'AVG([[craftDbMs]]) AS [[craftDbMs]]',
114
                'AVG([[craftDbCnt]]) AS [[craftDbCnt]]',
115
                'AVG([[craftTwigMs]]) AS [[craftTwigMs]]',
116
                'AVG([[craftTwigCnt]]) AS [[craftTwigCnt]]',
117
                'AVG([[craftOtherMs]]) AS [[craftOtherMs]]',
118
                'AVG([[craftOtherCnt]]) AS [[craftOtherCnt]]',
119
                'AVG([[craftTotalMemory]]) AS [[craftTotalMemory]]',
120
            ])
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...
121
            ->from(['{{%webperf_data_samples}}'])
122
            ->where(['between', 'dateCreated', $start, $end]);
123
        if (!empty($pageUrl)) {
124
            $query->andWhere(['url' => $pageUrl]);
125
        }
126
        if ((int)$siteId !== 0) {
127
            $query->andWhere(['siteId' => $siteId]);
128
        }
129
        $stats = $query->all();
130
        if ($stats) {
131
            $data = $stats[0];
132
        }
133
134
        return $data;
135
    }
136
}
137