CpuResourceDecorator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 40
ccs 0
cts 28
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A displayTitle() 0 4 1
A displayPrepaidAmount() 0 9 2
A getPrepaidQuantity() 0 10 3
A getCpuUnit() 0 8 2
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\models\decorators\server;
12
13
use Yii;
14
15
class CpuResourceDecorator extends AbstractServerResourceDecorator
16
{
17
    const UNIT_CORE = 0;
18
    const UNIT_MHZ = 1;
19
20
    public function displayTitle()
21
    {
22
        return Yii::t('hipanel:server:order', 'CPU');
23
    }
24
25
    public function displayPrepaidAmount()
26
    {
27
        if ($this->getCpuUnit() === self::UNIT_CORE) {
28
            return Yii::t('hipanel:server:order', '{0, plural, one{# core} other{# cores}}',
29
                $this->getPrepaidQuantity());
0 ignored issues
show
Documentation introduced by
$this->getPrepaidQuantity() is of type integer|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
        }
31
32
        return Yii::t('hipanel:server:order', '{0} MHz', Yii::$app->formatter->asInteger($this->getPrepaidQuantity()));
33
    }
34
35
    public function getPrepaidQuantity()
36
    {
37
        if ($this->getCpuUnit() === self::UNIT_CORE) {
38
            preg_match('/((\d+) cores?)$/i', $this->resource->part->partno, $matches);
39
        } else {
40
            preg_match('/((\d+) MHz)$/i', $this->resource->part->partno, $matches);
41
        }
42
43
        return $matches[2] === null ? 0 : $matches[2];
44
    }
45
46
    private function getCpuUnit()
47
    {
48
        if (strpos($this->resource->part->partno, 'core') !== false) {
49
            return self::UNIT_CORE;
50
        }
51
52
        return self::UNIT_MHZ;
53
    }
54
}
55