Passed
Push — v1 ( d9854b...3980d8 )
by Andrew
09:00 queued 05:56
created

MemoryLimit::recommendation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\recommendations;
12
13
use nystudio107\webperf\base\Recommendation;
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
17
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
 * @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...
19
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
20
 * @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...
21
 */
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...
22
class MemoryLimit extends Recommendation
23
{
24
    // Constants
25
    // =========================================================================
26
27
    const MIN_CRAFT_MEMORY = 64 * 1024 * 1024;
28
    const MAX_CRAFT_MEMORY = 1024 * 1024 * 1024;
29
30
    // Public Methods
31
    // =========================================================================
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @inheritdoc
35
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
36
    public function evaluate()
37
    {
38
        $phpMemoryLimit = $this->memoryLimit();
39
        if ($phpMemoryLimit) {
40
            $ratio = $phpMemoryLimit / $this->sample->craftTotalMemory;
41
            $displayCraftTotalMemory = (($this->sample->craftTotalMemory / 1024) / 1024) . 'M';
42
            $displayPhpMemoryLimit = (($phpMemoryLimit / 1024) / 1024) . 'M';
43
            $displayCraftMinMemory = ((self::MIN_CRAFT_MEMORY / 1024) / 1024) . 'M';
44
            $displayCraftMaxMemory = ((self::MAX_CRAFT_MEMORY / 1024) / 1024) . 'M';
0 ignored issues
show
Unused Code introduced by
The assignment to $displayCraftMaxMemory is dead and can be removed.
Loading history...
45
            $this->summary = Craft::t(
46
                'webperf',
47
                'Check the `memory_limit` setting in your `php.ini` file',
48
                []
49
            );
50
            // See if they have enough memory allocated
51
            if ($phpMemoryLimit < self::MIN_CRAFT_MEMORY) {
52
                $this->hasRecommendation = true;
53
                $this->detail = Craft::t(
54
                    'webperf',
55
                    'Pixel & Tonic recommends at least {displayCraftMinMemory} allocated to PHP for Craft CMS 3. You have only {displayPhpMemoryLimit} allocated in your `php.ini` file.',
56
                    [
57
                        'displayPhpMemoryLimit' => $displayPhpMemoryLimit,
58
                        'displayCraftMinMemory' => $displayCraftMinMemory,
59
                    ]
60
                );
61
                $this->learnMoreUrl = 'https://docs.craftcms.com/v3/requirements.html';
62
63
                return;
64
            }
65
            // See if they have too much memory allocated
66
            if ($phpMemoryLimit >= self::MAX_CRAFT_MEMORY) {
67
                $this->hasRecommendation = true;
68
                $this->detail = Craft::t(
69
                    'webperf',
70
                    'Your `php.ini` file has `memory_limit` set to {displayPhpMemoryLimit}. This may be set too high, since it is a per-process memory limit, and memory-intensive image transforms are done in a process separate from PHP.',
71
                    [
72
                        'displayPhpMemoryLimit' => $displayPhpMemoryLimit,
73
                    ]
74
                );
75
                $this->learnMoreUrl = 'https://docs.craftcms.com/v3/requirements.html';
76
77
                return;
78
            }
79
            // See if they have too much memory allocated
80
            if ($ratio < 1.5) {
81
                $this->hasRecommendation = true;
82
                $this->detail = Craft::t(
83
                    'webperf',
84
                    'Your `php.ini` file has `memory_limit` set to {displayPhpMemoryLimit}, but Craft is using {displayCraftTotalMemory}. Consider raising your `memory_limit`  to maintain a `1.5x` buffer of available memory.',
85
                    [
86
                        'displayPhpMemoryLimit' => $displayPhpMemoryLimit,
87
                        'displayCraftTotalMemory' => $displayCraftTotalMemory,
88
                    ]
89
                );
90
                $this->learnMoreUrl = 'https://docs.craftcms.com/v3/requirements.html';
91
92
                return;
93
            }
94
        }
95
    }
96
97
    // Protected Methods
98
    // =========================================================================
99
100
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
101
     * @return int
102
     */
103
    protected function memoryLimit(): int
104
    {
105
        $memoryLimit = ini_get('memory_limit');
106
        if (preg_match('/^(\d+)(.)$/', $memoryLimit, $matches)) {
107
            if (strtoupper($matches[2]) === 'M') {
108
                $memoryLimit = $matches[1] * 1024 * 1024; // nnnM -> nnn MB
109
            } elseif (strtoupper($matches[2]) === 'K') {
110
                $memoryLimit = $matches[1] * 1024; // nnnK -> nnn KB
111
            }
112
        }
113
114
        return $memoryLimit;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $memoryLimit could return the type string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
115
    }
116
}
117