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 |
||
| 20 | class TariffProfileGridView extends \hipanel\grid\BoxedGridView |
||
| 21 | { |
||
| 22 | public function columns() |
||
| 23 | { |
||
| 24 | return array_merge(parent::columns(), [ |
||
| 25 | 'name' => [ |
||
| 26 | 'class' => 'hipanel\grid\MainColumn', |
||
| 27 | 'filterAttribute' => 'name_like', |
||
| 28 | 'note' => null, |
||
| 29 | 'value' => function (TariffProfile $model) { |
||
| 30 | if (empty($model->name) || $model->isDefault()) { |
||
|
|
|||
| 31 | return Yii::t('hipanel.finance.tariffprofile', 'Default'); |
||
| 32 | } |
||
| 33 | |||
| 34 | return $model->name; |
||
| 35 | }, |
||
| 36 | ], |
||
| 37 | 'tariff_names' => [ |
||
| 38 | 'filter' => false, |
||
| 39 | 'format' => 'raw', |
||
| 40 | 'value' => function (TariffProfile $model) { |
||
| 41 | if (empty($model->tariffs)) { |
||
| 42 | return ''; |
||
| 43 | } |
||
| 44 | |||
| 45 | foreach ($model->tariffs as $type => $values) { |
||
| 46 | if (empty($values)) { |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | |||
| 50 | $links = []; |
||
| 51 | foreach ($values as $id => $name) { |
||
| 52 | $links[$id] = $this->tariffLink($id, $name); |
||
| 53 | } |
||
| 54 | $tariffs[$type] = $model->getAttributeLabel($type) . ': ' . implode(', ', $links); |
||
| 55 | } |
||
| 56 | |||
| 57 | return implode('<br>', $tariffs); |
||
| 58 | }, |
||
| 59 | ], |
||
| 60 | 'domain_tariff' => [ |
||
| 61 | 'attribute' => 'domain', |
||
| 62 | 'format' => 'raw', |
||
| 63 | View Code Duplication | 'value' => function (TariffProfile $model) { |
|
| 64 | if (empty($model->domain)) { |
||
| 65 | return ''; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $this->tariffLink($model->domain, $model->tariff_names[$model->domain]); |
||
| 69 | }, |
||
| 70 | ], |
||
| 71 | 'certificate_tariff' => [ |
||
| 72 | 'attribute' => 'certificate', |
||
| 73 | 'format' => 'raw', |
||
| 74 | View Code Duplication | 'value' => function (TariffProfile $model) { |
|
| 75 | if (empty($model->certificate)) { |
||
| 76 | return ''; |
||
| 77 | } |
||
| 78 | |||
| 79 | return $this->tariffLink($model->certificate, $model->tariff_names[$model->certificate]); |
||
| 80 | }, |
||
| 81 | ], |
||
| 82 | 'svds_tariff' => [ |
||
| 83 | 'attribute' => 'svds', |
||
| 84 | 'format' => 'raw', |
||
| 85 | View Code Duplication | 'value' => function (TariffProfile $model) { |
|
| 86 | if (empty($model->tariffs)) { |
||
| 87 | return ''; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (empty($model->tariffs[Tariff::TYPE_XEN])) { |
||
| 91 | return ''; |
||
| 92 | } |
||
| 93 | |||
| 94 | foreach ($model->tariffs[Tariff::TYPE_XEN] as $id => $name) { |
||
| 95 | $links[$id] = $this->tariffLink($id, $name); |
||
| 96 | } |
||
| 97 | |||
| 98 | return implode(', ', $links); |
||
| 99 | }, |
||
| 100 | ], |
||
| 101 | 'ovds_tariff' => [ |
||
| 102 | 'attribute' => 'ovds', |
||
| 103 | 'format' => 'raw', |
||
| 104 | View Code Duplication | 'value' => function (TariffProfile $model) { |
|
| 105 | if (empty($model->tariffs)) { |
||
| 106 | return ''; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (empty($model->tariffs[Tariff::TYPE_OPENVZ])) { |
||
| 110 | return ''; |
||
| 111 | } |
||
| 112 | |||
| 113 | foreach ($model->tariffs[Tariff::TYPE_OPENVZ] as $id => $name) { |
||
| 114 | $links[$id] = $this->tariffLink($id, $name); |
||
| 115 | } |
||
| 116 | |||
| 117 | return implode(', ', $links); |
||
| 118 | }, |
||
| 119 | ], |
||
| 120 | 'server_tariff' => [ |
||
| 121 | 'attribute' => 'server', |
||
| 122 | 'format' => 'raw', |
||
| 123 | View Code Duplication | 'value' => function (TariffProfile $model) { |
|
| 124 | if (empty($model->tariffs)) { |
||
| 125 | return ''; |
||
| 126 | } |
||
| 127 | |||
| 128 | if (empty($model->tariffs[Tariff::TYPE_SERVER])) { |
||
| 129 | return ''; |
||
| 130 | } |
||
| 131 | |||
| 132 | foreach ($model->tariffs[Tariff::TYPE_SERVER] as $id => $name) { |
||
| 133 | $links[$id] = $this->tariffLink($id, $name); |
||
| 134 | } |
||
| 135 | |||
| 136 | return implode(', ', $links); |
||
| 137 | }, |
||
| 138 | ], |
||
| 139 | 'actions' => [ |
||
| 140 | 'class' => MenuColumn::class, |
||
| 141 | 'menuClass' => ProfileActionsMenu::class, |
||
| 142 | ], |
||
| 143 | ]); |
||
| 144 | } |
||
| 145 | |||
| 146 | protected function tariffLink($id, $name) |
||
| 150 | } |
||
| 151 |
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@propertyannotation to your class or interface to document the existence of this variable.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.