Completed
Push — master ( b87a46...8351d4 )
by Dmitry
04:00
created

CpuResourceDecorator::getPrepaidQuantity()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
rs 9.4285
ccs 0
cts 9
cp 0
cc 3
eloc 6
nc 4
nop 0
crap 12
1
<?php
2
3
namespace hipanel\modules\finance\models\decorators\server;
4
5
use Yii;
6
7
class CpuResourceDecorator extends AbstractServerResourceDecorator
8
{
9
    const UNIT_CORE = 0;
10
    const UNIT_MHZ = 1;
11
12
    public function displayTitle()
13
    {
14
        return Yii::t('hipanel/server/order', 'CPU');
15
    }
16
17
    public function displayPrepaidAmount()
18
    {
19
        if ($this->getCpuUnit() === self::UNIT_CORE) {
20
            return Yii::t('hipanel/server/order', '{0, plural, one{# core} other{# cores}}',
21
                $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...
22
        }
23
24
        return Yii::t('hipanel/server/order', '{0} MHz', Yii::$app->formatter->asInteger($this->getPrepaidQuantity()));
25
    }
26
27
    public function getPrepaidQuantity()
28
    {
29
        if ($this->getCpuUnit() === self::UNIT_CORE) {
30
            preg_match('/((\d+) cores?)$/i', $this->resource->part->partno, $matches);
0 ignored issues
show
Documentation introduced by
The property part does not exist on object<hipanel\modules\f...\models\ServerResource>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
31
        } else {
32
            preg_match('/((\d+) MHz)$/i', $this->resource->part->partno, $matches);
0 ignored issues
show
Documentation introduced by
The property part does not exist on object<hipanel\modules\f...\models\ServerResource>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
33
34
        }
35
36
        return $matches[2] === null ? 0 : $matches[2];
37
    }
38
39
    private function getCpuUnit()
40
    {
41
        if (strpos($this->resource->part->partno, 'core') !== false) {
0 ignored issues
show
Documentation introduced by
The property part does not exist on object<hipanel\modules\f...\models\ServerResource>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
            return self::UNIT_CORE;
43
        }
44
45
        return self::UNIT_MHZ;
46
    }
47
}
48