| Conditions | 11 |
| Paths | 21 |
| Total Lines | 90 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | |||
| 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.