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 | * Workaround for php 7 |
||
| 120 | * |
||
| 121 | * In php7 json_decode(null) produces JSON_ERROR_SYNTAX |
||
| 122 | * |
||
| 123 | * @todo rewrite all json_decode calls in that manner |
||
| 124 | * |
||
| 125 | * @param null $json |
||
| 126 | * @return null|string |
||
| 127 | */ |
||
| 128 | private static function _php7JsonPatch($json = null) |
||
| 129 | { |
||
| 130 | return $json === null ? '[]' : $json; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Renders specified theme part with all it's widget corresponding current theme variation |
||
| 135 | * @param string $key Theme part key(ie. header or pre-footer) |
||
| 136 | * @param array $params |
||
| 137 | * @return string |
||
| 138 | * @throws InvalidConfigException |
||
| 139 | */ |
||
| 140 | public static function renderPart($key, $params=[]) |
||
| 141 | { |
||
| 142 | $parts = static::getAllParts(); |
||
| 143 | Yii::beginProfile('Render theme part:' . $key); |
||
| 144 | Yii::trace('Render theme part:' . $key); |
||
| 145 | /** @var array $model */ |
||
| 146 | $model = null; |
||
| 147 | foreach ($parts as $part) { |
||
| 148 | if ($part['key'] === $key) { |
||
| 149 | $model = $part; |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | if ($model === null) { |
||
| 154 | throw new InvalidConfigException("Can't find part with key $key"); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** @var ViewElementsGathener $viewElementsGathener */ |
||
| 158 | $viewElementsGathener = Yii::$app->viewElementsGathener; |
||
| 159 | |||
| 160 | |||
| 161 | if (static::shouldCache($model)) { |
||
| 162 | $result = Yii::$app->cache->get(static::getCacheKey($model)); |
||
| 163 | if ($result !== false) { |
||
| 164 | Yii::endProfile('Render theme part:' . $key); |
||
| 165 | $cachedData = $viewElementsGathener->getCachedData('ThemePart:'.$key); |
||
| 166 | if (is_array($cachedData)) { |
||
| 167 | $viewElementsGathener->repeatGatheredData(Yii::$app->view, $cachedData); |
||
| 168 | } |
||
| 169 | return $result; |
||
| 170 | } |
||
| 171 | $viewElementsGathener->startGathering( |
||
| 172 | 'ThemePart:'.$key, |
||
| 173 | static::getCacheDependency($model) |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | $model['id'] = intval($model['id']); |
||
| 178 | |||
| 179 | $widgets = array_reduce( |
||
| 180 | ThemeActiveWidgets::getActiveWidgets(), |
||
| 181 | function ($carry, $item) use($model) { |
||
| 182 | if ($item['part_id'] === $model['id']) { |
||
| 183 | $carry[]=$item; |
||
| 184 | } |
||
| 185 | return $carry; |
||
| 186 | }, |
||
| 187 | [] |
||
| 188 | ); |
||
| 189 | ArrayHelper::multisort($widgets, 'sort_order'); |
||
| 190 | |||
| 191 | $result = array_reduce( |
||
| 192 | $widgets, |
||
| 193 | function($carry, ThemeActiveWidgets $activeWidget) use ($model, $params) { |
||
| 194 | /** @var ThemeWidgets $widgetModel */ |
||
| 195 | $widgetModel = $activeWidget->widget; |
||
| 196 | /** @var BaseWidget $widgetClassName */ |
||
| 197 | $widgetClassName = $widgetModel->widget; |
||
| 198 | $widgetConfiguration = Json::decode(self::_php7JsonPatch($widgetModel->configuration_json), true); |
||
|
1 ignored issue
–
show
|
|||
| 199 | if (!is_array($widgetConfiguration)) { |
||
| 200 | $widgetConfiguration = []; |
||
| 201 | } |
||
| 202 | $activeWidgetConfiguration = Json::decode(self::_php7JsonPatch($activeWidget->configuration_json), true); |
||
|
1 ignored issue
–
show
|
|||
| 203 | if (!is_array($activeWidgetConfiguration)) { |
||
| 204 | $activeWidgetConfiguration = []; |
||
| 205 | } |
||
| 206 | $merged = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration); |
||
| 207 | $config = ArrayHelper::merge($merged, $params); |
||
| 208 | $config['themeWidgetModel'] = $widgetModel; |
||
| 209 | $config['partRow'] = $model; |
||
| 210 | $config['activeWidget'] = $activeWidget; |
||
| 211 | |||
| 212 | $carry .= $widgetClassName::widget($config); |
||
| 213 | return $carry; |
||
| 214 | }, |
||
| 215 | '' |
||
| 216 | ); |
||
| 217 | |||
| 218 | if (static::shouldCache($model)) { |
||
| 219 | Yii::$app->cache->set( |
||
| 220 | static::getCacheKey($model), |
||
| 221 | $result, |
||
| 222 | $model['cache_lifetime'], |
||
| 223 | static::getCacheDependency($model) |
||
| 224 | ); |
||
| 225 | $viewElementsGathener->endGathering(); |
||
| 226 | } |
||
| 227 | Yii::endProfile('Render theme part:' . $key); |
||
| 228 | return $result; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param array $attributesRow |
||
| 233 | * @return bool True if we should cache this widget |
||
| 234 | */ |
||
| 235 | public static function shouldCache($attributesRow) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param array $attributesRow |
||
| 242 | * @return string Cache key for this widget |
||
| 243 | */ |
||
| 244 | public static function getCacheKey($attributesRow) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param array $attributesRow |
||
| 254 | * @return string[] Array of cache tags |
||
| 255 | */ |
||
| 256 | public static function getCacheTags($attributesRow) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param array $attributesRow |
||
| 266 | * @return TagDependency TagDependency for cache storing |
||
| 267 | */ |
||
| 268 | public static function getCacheDependency($attributesRow) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Search |
||
| 277 | * @param $params |
||
| 278 | * @return ActiveDataProvider |
||
| 279 | */ |
||
| 280 | public function search($params) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @inheritdoc |
||
| 308 | */ |
||
| 309 | View Code Duplication | public function beforeDelete() |
|
| 327 | } |
||
| 328 |
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.