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 |
||
| 26 | class PricePresenter |
||
| 27 | { |
||
| 28 | protected Formatter $formatter; |
||
|
|
|||
| 29 | |||
| 30 | protected User $user; |
||
| 31 | |||
| 32 | protected string $priceAttribute = 'price'; |
||
| 33 | |||
| 34 | public function __construct(Formatter $formatter, User $user) |
||
| 35 | { |
||
| 36 | $this->formatter = $formatter; |
||
| 37 | $this->user = $user; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $priceAttribute |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function setPriceAttribute(string $priceAttribute): self |
||
| 45 | { |
||
| 46 | $this->priceAttribute = $priceAttribute; |
||
| 47 | |||
| 48 | return $this; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param Price $price |
||
| 53 | * @throws \yii\base\InvalidConfigException |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function renderPrice(Price $price): string |
||
| 57 | { |
||
| 58 | $unit = $formula = ''; |
||
| 59 | if ($price->getUnitLabel()) { |
||
| 60 | $unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (\count($price->formulaLines()) > 0) { |
||
| 64 | $formula = ArraySpoiler::widget([ |
||
| 65 | 'data' => $price->formulaLines(), |
||
| 66 | 'formatter' => function ($v) { |
||
| 67 | return Html::tag('kbd', $v, ['class' => 'javascript']); |
||
| 68 | }, |
||
| 69 | 'visibleCount' => 0, |
||
| 70 | 'delimiter' => '<br />', |
||
| 71 | 'button' => [ |
||
| 72 | 'label' => '∑', |
||
| 73 | 'popoverOptions' => [ |
||
| 74 | 'placement' => 'bottom', |
||
| 75 | 'html' => true, |
||
| 76 | ], |
||
| 77 | ], |
||
| 78 | ]); |
||
| 79 | } |
||
| 80 | |||
| 81 | return Html::tag('strong', $this->formatter->asCurrency($price->{$this->priceAttribute}, $price->currency)) . $unit . $formula; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param Price $price |
||
| 86 | * @param string $attribute |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function renderInfo(Price $price, string $attribute = 'quantity'): string |
||
| 90 | { |
||
| 91 | if (!$price->isQuantityPredefined()) { |
||
| 92 | return Yii::t('hipanel:finance', '{icon} Quantity: {quantity}', [ |
||
| 93 | 'icon' => Html::tag('i', '', ['class' => 'fa fa-calculator']), |
||
| 94 | 'quantity' => Html::tag('b', '<i class="fa fa-spin fa-refresh"></i>', ['data-dynamic-quantity' => true]), |
||
| 95 | ]); |
||
| 96 | } |
||
| 97 | if ($price->isOveruse()) { |
||
| 98 | return Yii::t('hipanel:finance', '{coins} {amount,number} {unit}', [ |
||
| 99 | 'coins' => Html::tag('i', '', ['class' => 'fa fa-money', 'title' => Yii::t('hipanel.finance.price', 'Prepaid amount')]), |
||
| 100 | 'amount' => $price->{$attribute}, |
||
| 101 | 'unit' => $price->getUnitLabel(), |
||
| 102 | ]); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($price->getSubtype() === 'hardware' && $this->user->can('part.read')) { |
||
| 106 | return $price->object->label; |
||
| 107 | } |
||
| 108 | |||
| 109 | return ''; // Do not display any information unless we are sure what we are displaying |
||
| 110 | } |
||
| 111 | } |
||
| 112 |