Conditions | 11 |
Paths | 21 |
Total Lines | 90 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
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); |
||
199 | if (!is_array($widgetConfiguration)) { |
||
200 | $widgetConfiguration = []; |
||
201 | } |
||
202 | $activeWidgetConfiguration = Json::decode(self::_php7JsonPatch($activeWidget->configuration_json), true); |
||
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 | |||
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.