Passed
Push — v1 ( 3980d8...7dfb05 )
by Andrew
12:06 queued 07:51
created

Recommendations::data()   B

Complexity

Conditions 8
Paths 50

Size

Total Lines 82
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 82
rs 7.6501
c 0
b 0
f 0
cc 8
nc 50
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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();
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
        if ($db->getIsMysql()) {
100
            // Query the db table
101
            $query = (new Query())
102
                ->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...
103
                    'COUNT(url) AS cnt',
104
105
                    'AVG(pageLoad) AS pageLoad',
106
                    'AVG(domInteractive) AS domInteractive',
107
                    'AVG(firstContentfulPaint) AS firstContentfulPaint',
108
                    'AVG(firstPaint) AS firstPaint',
109
                    'AVG(firstByte) AS firstByte',
110
                    'AVG(connect) AS connect',
111
                    'AVG(dns) AS dns',
112
113
                    'AVG(craftTotalMs) AS craftTotalMs',
114
                    'AVG(craftDbMs) AS craftDbMs',
115
                    'AVG(craftDbCnt) AS craftDbCnt',
116
                    'AVG(craftTwigMs) AS craftTwigMs',
117
                    'AVG(craftTwigCnt) AS craftTwigCnt',
118
                    'AVG(craftOtherMs) AS craftOtherMs',
119
                    'AVG(craftOtherCnt) AS craftOtherCnt',
120
                    'AVG(craftTotalMemory) AS craftTotalMemory',
121
                ])
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...
122
                ->from(['{{%webperf_data_samples}}'])
123
                ->where(['between', 'dateCreated', $start, $end]);
124
            if (!empty($pageUrl)) {
125
                $query->andWhere(['url' => $pageUrl]);
126
            }
127
            if ((int)$siteId !== 0) {
128
                $query->andWhere(['siteId' => $siteId]);
129
            }
130
            $stats = $query->all();
131
        }
132
        if ($db->getIsPgsql()) {
133
            // Query the db table
134
            $query = (new Query())
135
                ->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...
136
                    'COUNT("url") AS cnt',
137
138
                    'AVG("pageLoad") AS pageLoad',
139
                    'AVG("domInteractive") AS domInteractive',
140
                    'AVG("firstContentfulPaint") AS firstContentfulPaint',
141
                    'AVG("firstPaint") AS firstPaint',
142
                    'AVG("firstByte") AS firstByte',
143
                    'AVG("connect") AS connect',
144
                    'AVG("dns") AS dns',
145
146
                    'AVG("craftTotalMs") AS craftTotalMs',
147
                    'AVG("craftDbMs") AS craftDbMs',
148
                    'AVG("craftDbCnt") AS craftDbCnt',
149
                    'AVG("craftTwigMs") AS craftTwigMs',
150
                    'AVG("craftTwigCnt") AS craftTwigCnt',
151
                    'AVG("craftOtherMs") AS craftOtherMs',
152
                    'AVG("craftOtherCnt") AS craftOtherCnt',
153
                    'AVG("craftTotalMemory") AS craftTotalMemory',
154
                ])
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...
155
                ->from(['{{%webperf_data_samples}}'])
156
                ->where(['between', 'dateCreated', $start, $end]);
157
            if (!empty($pageUrl)) {
158
                $query->andWhere(['url' => $pageUrl]);
159
            }
160
            if ((int)$siteId !== 0) {
161
                $query->andWhere(['siteId' => $siteId]);
162
            }
163
            $stats = $query->all();
164
        }
165
        if ($stats) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stats does not seem to be defined for all execution paths leading up to this point.
Loading history...
166
            $data = $stats[0];
167
        }
168
169
        return $data;
170
    }
171
}
172