Conditions | 12 |
Paths | 1 |
Total Lines | 67 |
Code Lines | 40 |
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 |
||
125 | protected function addElementEventListeners() |
||
126 | { |
||
127 | Event::on( |
||
128 | Elements::class, |
||
129 | Elements::EVENT_AFTER_SAVE_ELEMENT, |
||
130 | function (ElementEvent $event) { |
||
131 | $element = $event->element; |
||
132 | if (!$element || ElementHelper::isDraftOrRevision($element)) { |
||
133 | return; |
||
134 | } |
||
135 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
136 | } |
||
137 | ); |
||
138 | |||
139 | Event::on( |
||
140 | Elements::class, |
||
141 | Elements::EVENT_BEFORE_DELETE_ELEMENT, |
||
142 | function (ElementEvent $event) { |
||
143 | $element = $event->element; |
||
144 | if (!$element || ElementHelper::isDraftOrRevision($element)) { |
||
145 | return; |
||
146 | } |
||
147 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
148 | } |
||
149 | ); |
||
150 | |||
151 | Event::on( |
||
152 | Structures::class, |
||
153 | Structures::EVENT_AFTER_MOVE_ELEMENT, |
||
154 | function (MoveElementEvent $event) { |
||
155 | $element = $event->element; |
||
156 | if (!$element || ElementHelper::isDraftOrRevision($element)) { |
||
157 | return; |
||
158 | } |
||
159 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
160 | } |
||
161 | ); |
||
162 | |||
163 | Event::on( |
||
164 | Elements::class, |
||
165 | Elements::EVENT_AFTER_PERFORM_ACTION, |
||
166 | function (ElementActionEvent $event) { |
||
167 | |||
168 | /* @var ElementQueryInterface|null $criteria */ |
||
169 | $criteria = $event->criteria; |
||
170 | |||
171 | if (!$criteria) { |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | /* @var ElementActionInterface|null $action */ |
||
176 | $action = $event->action; |
||
177 | |||
178 | if (!$action || !\in_array(\get_class($action), [ |
||
179 | SetStatus::class, |
||
180 | ])) { |
||
181 | return; |
||
182 | } |
||
183 | |||
184 | /** @var ElementInterface[] $elements */ |
||
185 | $elements = $criteria->all(); |
||
186 | |||
187 | foreach ($elements as $element) { |
||
188 | if (ElementHelper::isDraftOrRevision($element)) { |
||
189 | continue; |
||
190 | } |
||
191 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
192 | } |
||
222 |