| Conditions | 7 |
| Paths | 13 |
| Total Lines | 63 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 75 | public function init() |
||
| 76 | { |
||
| 77 | parent::init(); |
||
| 78 | self::$plugin = $this; |
||
| 79 | |||
| 80 | $request = Craft::$app->getRequest(); |
||
| 81 | // this will break the fields and plugins initialization |
||
| 82 | // https://github.com/craftcms/cms/issues/4944 |
||
| 83 | // https://github.com/mmikkel/CpFieldInspect-Craft/issues/11 |
||
| 84 | // $fields = Craft::$app->getFields()->getAllFields(); |
||
| 85 | if (/*!$user->getIsAdmin() || */ !$request->getIsCpRequest() || $request->getIsConsoleRequest()) { |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | // this is hacky and ugly but I don't know an alternative... we can't populate the user yet ¯\_(ツ)_/¯ |
||
| 90 | $session = Craft::$app->getSession(); |
||
| 91 | $id = $session->getHasSessionId() || $session->getIsActive() ? $session->get(Craft::$app->getUser()->idParam) : null; |
||
| 92 | if(empty($id) === true){ |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | $isAdmin = (new Query()) |
||
| 96 | ->select('admin') |
||
| 97 | ->from(Table::USERS) |
||
| 98 | ->where(['id' => $id]) |
||
| 99 | ->scalar(); |
||
| 100 | if((bool)$isAdmin === false){ |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | |||
| 104 | // Handler: EVENT_AFTER_LOAD_PLUGINS |
||
| 105 | Event::on( |
||
| 106 | Plugins::class, |
||
| 107 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
| 108 | function () { |
||
| 109 | $this->doIt(); |
||
| 110 | } |
||
| 111 | ); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Logging in Craft involves using one of the following methods: |
||
| 115 | * |
||
| 116 | * Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use. |
||
| 117 | * Craft::info(): record a message that conveys some useful information. |
||
| 118 | * Craft::warning(): record a warning message that indicates something unexpected has happened. |
||
| 119 | * Craft::error(): record a fatal error that should be investigated as soon as possible. |
||
| 120 | * |
||
| 121 | * Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log` |
||
| 122 | * |
||
| 123 | * It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets |
||
| 124 | * the category to the method (prefixed with the fully qualified class name) where the constant appears. |
||
| 125 | * |
||
| 126 | * To enable the Yii debug toolbar, go to your user account in the AdminCP and check the |
||
| 127 | * [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel |
||
| 128 | * |
||
| 129 | * http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html |
||
| 130 | */ |
||
| 131 | Craft::info( |
||
| 132 | Craft::t( |
||
| 133 | 'cp-field-inspect', |
||
| 134 | '{name} plugin loaded', |
||
| 135 | ['name' => $this->name] |
||
| 136 | ), |
||
| 137 | __METHOD__ |
||
| 138 | ); |
||
| 219 |