| Conditions | 7 |
| Paths | 40 |
| Total Lines | 54 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 53 | public static function renderPluginTemplate(string $templatePath, array $params = []): Markup |
||
| 54 | { |
||
| 55 | // Stash the old template mode, and set it Control Panel template mode |
||
| 56 | $oldMode = Craft::$app->view->getTemplateMode(); |
||
| 57 | try { |
||
| 58 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
||
| 59 | } catch (Exception $e) { |
||
| 60 | Craft::error($e->getMessage(), __METHOD__); |
||
| 61 | } |
||
| 62 | |||
| 63 | // Render the template with our vars passed in |
||
| 64 | try { |
||
| 65 | $htmlText = Craft::$app->view->renderTemplate('youtubeliveembed/' . $templatePath, $params); |
||
| 66 | $templateRendered = true; |
||
| 67 | } catch (\Exception $e) { |
||
| 68 | $htmlText = Craft::t( |
||
| 69 | 'youtubeliveembed', |
||
| 70 | 'Error rendering `{template}` -> {error}', |
||
| 71 | ['template' => $templatePath, 'error' => $e->getMessage()] |
||
| 72 | ); |
||
| 73 | Craft::error($htmlText, __METHOD__); |
||
| 74 | $templateRendered = false; |
||
| 75 | } |
||
| 76 | |||
| 77 | // If we couldn't find a plugin template, look for a frontend template |
||
| 78 | if (!$templateRendered) { |
||
| 79 | try { |
||
| 80 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
||
| 81 | } catch (Exception $e) { |
||
| 82 | Craft::error($e->getMessage(), __METHOD__); |
||
| 83 | } |
||
| 84 | // Render the template with our vars passed in |
||
| 85 | try { |
||
| 86 | $htmlText = Craft::$app->view->renderTemplate($templatePath, $params); |
||
| 87 | $templateRendered = true; |
||
| 88 | } catch (\Exception $e) { |
||
| 89 | $htmlText = Craft::t( |
||
| 90 | 'youtubeliveembed', |
||
| 91 | 'Error rendering `{template}` -> {error}', |
||
| 92 | ['template' => $templatePath, 'error' => $e->getMessage()] |
||
| 93 | ); |
||
| 94 | Craft::error($htmlText, __METHOD__); |
||
| 95 | $templateRendered = false; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // Restore the old template mode |
||
| 100 | try { |
||
| 101 | Craft::$app->view->setTemplateMode($oldMode); |
||
| 102 | } catch (Exception $e) { |
||
| 103 | Craft::error($e->getMessage(), __METHOD__); |
||
| 104 | } |
||
| 105 | |||
| 106 | return Template::raw($htmlText); |
||
| 107 | } |
||
| 109 |