Completed
Push — master ( 4f1415...383c20 )
by Dmitry
05:05
created

PricePresenter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 5.88 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 3
loc 51
c 0
b 0
f 0
ccs 0
cts 21
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B renderPrice() 3 33 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace hipanel\modules\finance\grid\presenters\price;
4
5
use hipanel\modules\finance\models\Price;
6
use hipanel\widgets\ArraySpoiler;
7
use Yii;
8
use yii\bootstrap\Html;
9
10
/**
11
 * Class PricePresenter contains methods that present price properties.
12
 * You can override this class to add custom presentations support.
13
 *
14
 * @author Dmytro Naumenko <[email protected]>
15
 */
16
class PricePresenter
17
{
18
    /**
19
     * @var \yii\i18n\Formatter
20
     */
21
    private $formatter;
22
23
    public function __construct()
24
    {
25
        $this->formatter = Yii::$app->formatter;
26
    }
27
28
    /**
29
     * @param Price $price
30
     * @return string
31
     * @throws \yii\base\InvalidConfigException
32
     */
33
    public function renderPrice($price): string
34
    {
35
        $unit = $formula = '';
36 View Code Duplication
        if ($price->getUnitLabel()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            $unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]);
38
        }
39
40
        if ($price->isOveruse()) {
41
            $prepaid = ', ' . Yii::t('hipanel:finance', 'prepaid {amount,number}', ['amount' => $price->quantity]);
42
        } else {
43
            $prepaid = ' ' . Yii::t('hipanel:finance', 'monthly');
44
        }
45
46
        if (count($price->formulaLines()) > 0) {
47
            $formula = ArraySpoiler::widget([
48
                'data' => $price->formulaLines(),
49
                'formatter' => function ($v) {
50
                    return Html::tag('kbd', $v, ['class' => 'javascript']);
51
                },
52
                'visibleCount' => 0,
53
                'delimiter' => '<br />',
54
                'button' => [
55
                    'label' => '&sum;',
56
                    'popoverOptions' => [
57
                        'placement' => 'bottom',
58
                        'html' => true,
59
                    ],
60
                ],
61
            ]);
62
        }
63
64
        return Html::tag('strong', $this->formatter->asCurrency($price->price, $price->currency)) . $unit . $prepaid . $formula;
65
    }
66
}
67