Conditions | 10 |
Paths | 1 |
Total Lines | 105 |
Code Lines | 65 |
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 |
||
70 | public function init() |
||
71 | { |
||
72 | parent::init(); |
||
73 | |||
74 | $this->setComponents([ |
||
75 | 'reasons' => ReasonsService::class, |
||
76 | ]); |
||
77 | |||
78 | Event::on( |
||
79 | Plugins::class, |
||
80 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
81 | function (PluginEvent $event) { |
||
82 | if ($event->plugin === $this) { |
||
83 | $this->reasons->clearCache(); |
||
84 | } |
||
85 | } |
||
86 | ); |
||
87 | |||
88 | Event::on( |
||
89 | Plugins::class, |
||
90 | Plugins::EVENT_AFTER_UNINSTALL_PLUGIN, |
||
91 | function (PluginEvent $event) { |
||
92 | if ($event->plugin === $this) { |
||
93 | $this->reasons->clearCache(); |
||
94 | Craft::$app->getProjectConfig()->remove('reasons_conditionals'); |
||
95 | } |
||
96 | } |
||
97 | ); |
||
98 | |||
99 | // Save or delete conditionals when field layout is saved |
||
100 | Event::on( |
||
101 | Fields::class, |
||
102 | Fields::EVENT_AFTER_SAVE_FIELD_LAYOUT, |
||
103 | function (FieldLayoutEvent $event) { |
||
104 | if (Craft::$app->getRequest()->getIsConsoleRequest()) { |
||
105 | return; |
||
106 | } |
||
107 | $fieldLayout = $event->layout; |
||
108 | if (!$fieldLayout) { |
||
109 | return; |
||
110 | } |
||
111 | $conditionals = Craft::$app->getRequest()->getBodyParam('_reasons', null); |
||
112 | if ($conditionals === null) { |
||
113 | return; |
||
114 | } |
||
115 | try { |
||
116 | if ($conditionals) { |
||
117 | $this->reasons->saveFieldLayoutConditionals($fieldLayout, $conditionals); |
||
118 | } else { |
||
119 | $this->reasons->deleteFieldLayoutConditionals($fieldLayout); |
||
120 | } |
||
121 | } catch (\Throwable $e) { |
||
122 | Craft::error($e->getMessage(), __METHOD__); |
||
123 | if (Craft::$app->getConfig()->getGeneral()->devMode) { |
||
124 | throw $e; |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | ); |
||
129 | |||
130 | // Delete conditionals when field layouts are deleted |
||
131 | Event::on( |
||
132 | Fields::class, |
||
133 | Fields::EVENT_AFTER_DELETE_FIELD_LAYOUT, |
||
134 | function (FieldLayoutEvent $event) { |
||
135 | $fieldLayout = $event->layout; |
||
136 | if (!$fieldLayout) { |
||
137 | return; |
||
138 | } |
||
139 | $this->reasons->deleteFieldLayoutConditionals($fieldLayout); |
||
140 | } |
||
141 | ); |
||
142 | |||
143 | // Clear data caches when fields are saved |
||
144 | Event::on( |
||
145 | Fields::class, |
||
146 | Fields::EVENT_AFTER_SAVE_FIELD, |
||
147 | [$this->reasons, 'clearCache'] |
||
148 | ); |
||
149 | |||
150 | // Clear data caches when fields are deleted |
||
151 | Event::on( |
||
152 | Fields::class, |
||
153 | Fields::EVENT_AFTER_DELETE_FIELD, |
||
154 | [$this->reasons, 'clearCache'] |
||
155 | ); |
||
156 | |||
157 | // Support Project Config rebuild |
||
158 | Event::on( |
||
159 | ProjectConfig::class, |
||
160 | ProjectConfig::EVENT_REBUILD, |
||
161 | [$this->reasons, 'onProjectConfigRebuild'] |
||
162 | ); |
||
163 | |||
164 | // Listen for appropriate Project Config changes |
||
165 | Craft::$app->getProjectConfig() |
||
166 | ->onAdd('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigChange']) |
||
167 | ->onUpdate('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigChange']) |
||
168 | ->onRemove('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigDelete']); |
||
169 | |||
170 | // Queue up asset bundle or handle AJAX action requests |
||
171 | Event::on( |
||
172 | Application::class, |
||
173 | Application::EVENT_INIT, |
||
174 | [$this, 'initReasons'] |
||
175 | ); |
||
319 |