| Conditions | 4 |
| Paths | 1 |
| Total Lines | 66 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 72 | public function init(): void |
||
| 73 | { |
||
| 74 | parent::init(); |
||
| 75 | self::$plugin = $this; |
||
| 76 | |||
| 77 | Event::on( |
||
| 78 | CraftVariable::class, |
||
| 79 | CraftVariable::EVENT_INIT, |
||
| 80 | static function(Event $event): void { |
||
| 81 | /** @var CraftVariable $variable */ |
||
| 82 | $variable = $event->sender; |
||
| 83 | $variable->set('routeMap', RouteMapVariable::class); |
||
| 84 | } |
||
| 85 | ); |
||
| 86 | |||
| 87 | // Handler: Elements::EVENT_AFTER_SAVE_ELEMENT |
||
| 88 | Event::on( |
||
| 89 | Elements::class, |
||
| 90 | Elements::EVENT_AFTER_SAVE_ELEMENT, |
||
| 91 | static function(ElementEvent $event): void { |
||
| 92 | Craft::debug( |
||
| 93 | 'Elements::EVENT_AFTER_SAVE_ELEMENT', |
||
| 94 | __METHOD__ |
||
| 95 | ); |
||
| 96 | /** @var Element $element */ |
||
| 97 | $element = $event->element; |
||
| 98 | $bustCache = true; |
||
| 99 | // Only bust the cache if the element is ENABLED or LIVE |
||
| 100 | if (($element->getStatus() !== Element::STATUS_ENABLED) |
||
| 101 | && ($element->getStatus() !== Entry::STATUS_LIVE) |
||
| 102 | ) { |
||
| 103 | $bustCache = false; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($bustCache) { |
||
| 107 | Craft::debug( |
||
| 108 | 'Cache busted due to saving: ' . $element::class . ' - ' . $element->title, |
||
| 109 | __METHOD__ |
||
| 110 | ); |
||
| 111 | RouteMap::$plugin->routes->invalidateCache(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | ); |
||
| 115 | |||
| 116 | // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
||
| 117 | Event::on( |
||
| 118 | ClearCaches::class, |
||
| 119 | ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
||
| 120 | static function(RegisterCacheOptionsEvent $event): void { |
||
| 121 | $event->options[] = [ |
||
| 122 | 'key' => 'route-map', |
||
| 123 | 'label' => Craft::t('route-map', 'Route Map Cache'), |
||
| 124 | 'action' => function(): void { |
||
| 125 | RouteMap::$plugin->routes->invalidateCache(); |
||
| 126 | }, |
||
| 127 | ]; |
||
| 128 | } |
||
| 129 | ); |
||
| 130 | |||
| 131 | Craft::info( |
||
| 132 | Craft::t( |
||
| 133 | 'route-map', |
||
| 134 | '{name} plugin loaded', |
||
| 135 | ['name' => $this->name] |
||
| 136 | ), |
||
| 137 | __METHOD__ |
||
| 138 | ); |
||
| 144 |