| Conditions | 10 |
| Paths | 1 |
| Total Lines | 115 |
| Code Lines | 72 |
| 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 |
||
| 79 | public function init() |
||
| 80 | { |
||
| 81 | parent::init(); |
||
| 82 | self::$plugin = $this; |
||
| 83 | |||
| 84 | $this->setComponents([ |
||
| 85 | 'reasons' => ReasonsService::class, |
||
| 86 | ]); |
||
| 87 | |||
| 88 | Event::on( |
||
| 89 | Plugins::class, |
||
| 90 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
| 91 | function (PluginEvent $event) { |
||
| 92 | if ($event->plugin === $this) { |
||
| 93 | $this->reasons->clearCache(); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | ); |
||
| 97 | |||
| 98 | Event::on( |
||
| 99 | Plugins::class, |
||
| 100 | Plugins::EVENT_AFTER_UNINSTALL_PLUGIN, |
||
| 101 | function (PluginEvent $event) { |
||
| 102 | if ($event->plugin === $this) { |
||
| 103 | $this->reasons->clearCache(); |
||
| 104 | Craft::$app->getProjectConfig()->remove('reasons_conditionals'); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | ); |
||
| 108 | |||
| 109 | // Save or delete conditionals when field layout is saved |
||
| 110 | Event::on( |
||
| 111 | Fields::class, |
||
| 112 | Fields::EVENT_AFTER_SAVE_FIELD_LAYOUT, |
||
| 113 | function (FieldLayoutEvent $event) { |
||
| 114 | if (Craft::$app->getRequest()->getIsConsoleRequest()) { |
||
| 115 | return; |
||
| 116 | } |
||
| 117 | $fieldLayout = $event->layout; |
||
| 118 | if (!$fieldLayout) { |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | $conditionals = Craft::$app->getRequest()->getBodyParam('_reasons', null); |
||
| 122 | if ($conditionals === null) { |
||
| 123 | return; |
||
| 124 | } |
||
| 125 | try { |
||
| 126 | if ($conditionals) { |
||
| 127 | $this->reasons->saveFieldLayoutConditionals($fieldLayout, $conditionals); |
||
| 128 | } else { |
||
| 129 | $this->reasons->deleteFieldLayoutConditionals($fieldLayout); |
||
| 130 | } |
||
| 131 | } catch (\Throwable $e) { |
||
| 132 | Craft::error($e->getMessage(), __METHOD__); |
||
| 133 | if (Craft::$app->getConfig()->getGeneral()->devMode) { |
||
| 134 | throw $e; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | ); |
||
| 139 | |||
| 140 | // Delete conditionals when field layouts are deleted |
||
| 141 | Event::on( |
||
| 142 | Fields::class, |
||
| 143 | Fields::EVENT_AFTER_DELETE_FIELD_LAYOUT, |
||
| 144 | function (FieldLayoutEvent $event) { |
||
| 145 | $fieldLayout = $event->layout; |
||
| 146 | if (!$fieldLayout) { |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | $this->reasons->deleteFieldLayoutConditionals($fieldLayout); |
||
| 150 | } |
||
| 151 | ); |
||
| 152 | |||
| 153 | // Clear data caches when fields are saved |
||
| 154 | Event::on( |
||
| 155 | Fields::class, |
||
| 156 | Fields::EVENT_AFTER_SAVE_FIELD, |
||
| 157 | [$this->reasons, 'clearCache'] |
||
| 158 | ); |
||
| 159 | |||
| 160 | // Clear data caches when fields are deleted |
||
| 161 | Event::on( |
||
| 162 | Fields::class, |
||
| 163 | Fields::EVENT_AFTER_DELETE_FIELD, |
||
| 164 | [$this->reasons, 'clearCache'] |
||
| 165 | ); |
||
| 166 | |||
| 167 | // Support Project Config rebuild |
||
| 168 | Event::on( |
||
| 169 | ProjectConfig::class, |
||
| 170 | ProjectConfig::EVENT_REBUILD, |
||
| 171 | [$this->reasons, 'onProjectConfigRebuild'] |
||
| 172 | ); |
||
| 173 | |||
| 174 | // Listen for appropriate Project Config changes |
||
| 175 | Craft::$app->projectConfig |
||
| 176 | ->onAdd('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigChange']) |
||
|
|
|||
| 177 | ->onUpdate('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigChange']) |
||
| 178 | ->onRemove('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigDelete']); |
||
| 179 | |||
| 180 | // Queue up asset bundle or handle AJAX action requests |
||
| 181 | Event::on( |
||
| 182 | Plugins::class, |
||
| 183 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
| 184 | [$this, 'initReasons'] |
||
| 185 | ); |
||
| 186 | |||
| 187 | Craft::info( |
||
| 188 | Craft::t( |
||
| 189 | 'reasons', |
||
| 190 | '{name} plugin loaded', |
||
| 191 | ['name' => $this->name] |
||
| 192 | ), |
||
| 193 | __METHOD__ |
||
| 194 | ); |
||
| 327 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.