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 |
||
| 29 | class ThemeParts extends \yii\db\ActiveRecord |
||
| 30 | { |
||
| 31 | use IdentityMap; |
||
| 32 | public static $allParts = null; |
||
| 33 | /** |
||
| 34 | * @inheritdoc |
||
| 35 | */ |
||
| 36 | public function behaviors() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @inheritdoc |
||
| 47 | */ |
||
| 48 | public static function tableName() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @inheritdoc |
||
| 55 | */ |
||
| 56 | public function rules() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @inheritdoc |
||
| 67 | */ |
||
| 68 | public function attributeLabels() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns all db-stored theme parts in array representation |
||
| 85 | * |
||
| 86 | * @param bool $force True if you want to refresh static-variable cache |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public static function getAllParts($force = false) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Renders specified theme part with all it's widget corresponding current theme variation |
||
| 120 | * @param string $key Theme part key(ie. header or pre-footer) |
||
| 121 | * @param array $params |
||
| 122 | * @return string |
||
| 123 | * @throws InvalidConfigException |
||
| 124 | */ |
||
| 125 | public static function renderPart($key, $params=[]) |
||
| 126 | { |
||
| 127 | $parts = static::getAllParts(); |
||
| 128 | Yii::beginProfile('Render theme part:' . $key); |
||
| 129 | Yii::trace('Render theme part:' . $key); |
||
| 130 | /** @var array $model */ |
||
| 131 | $model = null; |
||
| 132 | foreach ($parts as $part) { |
||
| 133 | if ($part['key'] === $key) { |
||
| 134 | $model = $part; |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | if ($model === null) { |
||
| 139 | throw new InvalidConfigException("Can't find part with key $key"); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** @var ViewElementsGathener $viewElementsGathener */ |
||
| 143 | $viewElementsGathener = Yii::$app->viewElementsGathener; |
||
| 144 | |||
| 145 | |||
| 146 | if (static::shouldCache($model)) { |
||
| 147 | $result = Yii::$app->cache->get(static::getCacheKey($model)); |
||
| 148 | if ($result !== false) { |
||
| 149 | Yii::endProfile('Render theme part:' . $key); |
||
| 150 | $cachedData = $viewElementsGathener->getCachedData('ThemePart:'.$key); |
||
| 151 | if (is_array($cachedData)) { |
||
| 152 | $viewElementsGathener->repeatGatheredData(Yii::$app->view, $cachedData); |
||
| 153 | } |
||
| 154 | return $result; |
||
| 155 | } |
||
| 156 | $viewElementsGathener->startGathering( |
||
| 157 | 'ThemePart:'.$key, |
||
| 158 | static::getCacheDependency($model) |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | $model['id'] = intval($model['id']); |
||
| 163 | |||
| 164 | $widgets = array_reduce( |
||
| 165 | ThemeActiveWidgets::getActiveWidgets(), |
||
| 166 | function ($carry, $item) use($model) { |
||
| 167 | if ($item['part_id'] === $model['id']) { |
||
| 168 | $carry[]=$item; |
||
| 169 | } |
||
| 170 | return $carry; |
||
| 171 | }, |
||
| 172 | [] |
||
| 173 | ); |
||
| 174 | ArrayHelper::multisort($widgets, 'sort_order'); |
||
| 175 | |||
| 176 | $result = array_reduce( |
||
| 177 | $widgets, |
||
| 178 | function($carry, ThemeActiveWidgets $activeWidget) use ($model, $params) { |
||
| 179 | /** @var ThemeWidgets $widgetModel */ |
||
| 180 | $widgetModel = $activeWidget->widget; |
||
| 181 | /** @var BaseWidget $widgetClassName */ |
||
| 182 | $widgetClassName = $widgetModel->widget; |
||
| 183 | $widgetConfiguration = Json::decode($widgetModel->configuration_json, true); |
||
| 184 | if (!is_array($widgetConfiguration)) { |
||
| 185 | $widgetConfiguration = []; |
||
| 186 | } |
||
| 187 | $activeWidgetConfiguration = Json::decode($activeWidget->configuration_json, true); |
||
| 188 | if (!is_array($activeWidgetConfiguration)) { |
||
| 189 | $activeWidgetConfiguration = []; |
||
| 190 | } |
||
| 191 | $merged = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration); |
||
| 192 | $config = ArrayHelper::merge($merged, $params); |
||
| 193 | $config['themeWidgetModel'] = $widgetModel; |
||
| 194 | $config['partRow'] = $model; |
||
| 195 | $config['activeWidget'] = $activeWidget; |
||
| 196 | |||
| 197 | $carry .= $widgetClassName::widget($config); |
||
| 198 | return $carry; |
||
| 199 | }, |
||
| 200 | '' |
||
| 201 | ); |
||
| 202 | |||
| 203 | if (static::shouldCache($model)) { |
||
| 204 | Yii::$app->cache->set( |
||
| 205 | static::getCacheKey($model), |
||
| 206 | $result, |
||
| 207 | $model['cache_lifetime'], |
||
| 208 | static::getCacheDependency($model) |
||
| 209 | ); |
||
| 210 | $viewElementsGathener->endGathering(); |
||
| 211 | } |
||
| 212 | Yii::endProfile('Render theme part:' . $key); |
||
| 213 | return $result; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param array $attributesRow |
||
| 218 | * @return bool True if we should cache this widget |
||
| 219 | */ |
||
| 220 | public static function shouldCache($attributesRow) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $attributesRow |
||
| 227 | * @return string Cache key for this widget |
||
| 228 | */ |
||
| 229 | public static function getCacheKey($attributesRow) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param array $attributesRow |
||
| 239 | * @return string[] Array of cache tags |
||
| 240 | */ |
||
| 241 | public static function getCacheTags($attributesRow) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param array $attributesRow |
||
| 251 | * @return TagDependency TagDependency for cache storing |
||
| 252 | */ |
||
| 253 | public static function getCacheDependency($attributesRow) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Search |
||
| 262 | * @param $params |
||
| 263 | * @return ActiveDataProvider |
||
| 264 | */ |
||
| 265 | public function search($params) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @inheritdoc |
||
| 293 | */ |
||
| 294 | View Code Duplication | public function beforeDelete() |
|
| 312 | } |
||
| 313 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.