| Conditions | 19 |
| Paths | 18 |
| Total Lines | 82 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 231 | protected function initAjaxRequest() |
||
| 232 | { |
||
| 233 | |||
| 234 | $request = Craft::$app->getRequest(); |
||
| 235 | if (!$request->getIsPost() || !$request->getIsActionRequest()) { |
||
| 236 | return; |
||
| 237 | } |
||
| 238 | |||
| 239 | $actionSegments = $request->getActionSegments(); |
||
| 240 | $actionSegment = $actionSegments[\count($actionSegments) - 1] ?? null; |
||
| 241 | |||
| 242 | if (!$actionSegment) { |
||
| 243 | return; |
||
| 244 | } |
||
| 245 | |||
| 246 | if ($actionSegment === 'switch-entry-type') { |
||
| 247 | Craft::$app->getView()->registerJs('Craft.ReasonsPlugin.initPrimaryForm();'); |
||
| 248 | return; |
||
| 249 | } |
||
| 250 | |||
| 251 | if ($actionSegment === 'get-editor-html') { |
||
| 252 | |||
| 253 | $conditionalsKey = null; |
||
| 254 | $elementId = (int)$request->getBodyParam('elementId'); |
||
| 255 | |||
| 256 | if (!$elementId) { |
||
| 257 | |||
| 258 | // Probably a new element |
||
| 259 | $attributes = $request->getBodyParam('attributes', []); |
||
| 260 | $elementType = $request->getBodyParam('elementType'); |
||
| 261 | |||
| 262 | if ($elementType === Entry::class && $typeId = ($attributes['typeId'] ?? null)) { |
||
| 263 | $conditionalsKey = "entryType:$typeId"; |
||
| 264 | } else if ($elementType === Category::class && $groupId = ($attributes['groupId'] ?? null)) { |
||
| 265 | $conditionalsKey = "categoryGroup:$groupId"; |
||
| 266 | } |
||
| 267 | |||
| 268 | if (!$conditionalsKey) { |
||
| 269 | return; |
||
| 270 | } |
||
| 271 | |||
| 272 | } else if ($element = Craft::$app->getElements()->getElementById($elementId)) { |
||
| 273 | |||
| 274 | $elementType = \get_class($element); |
||
| 275 | |||
| 276 | switch ($elementType) { |
||
| 277 | case Entry::class: |
||
| 278 | /** @var Entry $element */ |
||
| 279 | $conditionalsKey = "entryType:{$element->typeId}"; |
||
| 280 | break; |
||
| 281 | |||
| 282 | case GlobalSet::class: |
||
| 283 | /** @var GlobalSet $element */ |
||
| 284 | $conditionalsKey = "globalSet:{$element->id}"; |
||
| 285 | break; |
||
| 286 | |||
| 287 | case Asset::class: |
||
| 288 | /** @var Asset $element */ |
||
| 289 | $conditionalsKey = "assetSource:{$element->volumeId}"; |
||
| 290 | break; |
||
| 291 | |||
| 292 | case Category::class: |
||
| 293 | /** @var Category $element */ |
||
| 294 | $conditionalsKey = "categoryGroup:{$element->groupId}"; |
||
| 295 | break; |
||
| 296 | |||
| 297 | case Tag::class: |
||
| 298 | /** @var Tag $element */ |
||
| 299 | $conditionalsKey = "tagGroup:{$element->groupId}"; |
||
| 300 | break; |
||
| 301 | |||
| 302 | case User::class: |
||
| 303 | $conditionalsKey = "users"; |
||
| 304 | break; |
||
| 305 | |||
| 306 | default: |
||
| 307 | return; |
||
| 308 | |||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | Craft::$app->getView()->registerJs("Craft.ReasonsPlugin.initElementEditor('{$conditionalsKey}');"); |
||
| 313 | |||
| 319 |