Passed
Push — v1 ( 06dc74...d9854b )
by Andrew
06:57 queued 03:49
created

Recommendations::data()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 47
rs 9.424
c 0
b 0
f 0
cc 4
nc 8
nop 4
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\MemoryLimit;
16
17
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...
18
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...
19
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...
20
21
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...
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @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...
25
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @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...
27
 */
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...
28
class Recommendations extends Component
29
{
30
    // Constants
31
    // =========================================================================
32
33
    const RECOMMENDATIONS_LIST = [
34
        MemoryLimit::class,
35
    ];
36
37
    // Public Methods
38
    // =========================================================================
39
40
    /**
41
     * Return a list of recommendations
42
     *
43
     * @param RecommendationDataSample $sample
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     *
45
     * @return array
46
     */
47
    public function list(RecommendationDataSample $sample): array
48
    {
49
        $data = [];
50
        foreach (self::RECOMMENDATIONS_LIST as $recClass) {
51
            /** @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...
52
            $rec = new $recClass(['sample' => $sample]);
53
            if ($rec->recommendation()) {
54
                $data[] = [
55
                    'summary' => Markdown::processParagraph($rec->summary()),
56
                    'detail' => Markdown::processParagraph($rec->detail()),
57
                    'learnMoreLink' => $rec->learnMoreLink(),
58
                ];
59
            }
60
        }
61
        return $data;
62
    }
63
64
    /**
65
     * Return a list of recommendations
66
     *
67
     * @param string $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
68
     * @param string $start
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
69
     * @param string $end
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     * @param int    $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     *
72
     * @return array
73
     */
74
    public function data(
75
        $pageUrl = '',
76
        string $start = '',
77
        string $end = '',
78
        $siteId = 0
79
    ): array {
80
        $data = [];
81
        // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00
82
        $end = date('Y-m-d', strtotime($end.'+1 day'));
83
        $pageUrl = urldecode($pageUrl);
84
        // Query the db table
85
        $query = (new Query())
86
            ->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...
87
                'COUNT(url) AS cnt',
88
89
                'AVG(pageLoad) AS pageLoad',
90
                'AVG(domInteractive) AS domInteractive',
91
                'AVG(firstContentfulPaint) AS firstContentfulPaint',
92
                'AVG(firstPaint) AS firstPaint',
93
                'AVG(firstByte) AS firstByte',
94
                'AVG(connect) AS connect',
95
                'AVG(dns) AS dns',
96
97
                'AVG(craftTotalMs) AS craftTotalMs',
98
                'AVG(craftDbMs) AS craftDbMs',
99
                'AVG(craftDbCnt) AS craftDbCnt',
100
                'AVG(craftTwigMs) AS craftTwigMs',
101
                'AVG(craftTwigCnt) AS craftTwigCnt',
102
                'AVG(craftOtherMs) AS craftOtherMs',
103
                'AVG(craftOtherCnt) AS craftOtherCnt',
104
                'AVG(craftTotalMemory) AS craftTotalMemory',
105
            ])
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...
106
            ->from(['{{%webperf_data_samples}}'])
107
            ->where(['between', 'dateCreated', $start, $end])
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
108
        ;
109
        if (!empty($pageUrl)) {
110
            $query->andWhere(['url' => $pageUrl]);
111
        }
112
        if ((int)$siteId !== 0) {
113
            $query->andWhere(['siteId' => $siteId]);
114
        }
115
        $stats = $query->all();
116
        if ($stats) {
117
            $data = $stats[0];
118
        }
119
120
        return $data;
121
    }
122
}
123