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