Conditions | 5 |
Paths | 4 |
Total Lines | 69 |
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 |
||
121 | public static function handleChangedToken(ConfigEvent $event) |
||
122 | { |
||
123 | Event::off( |
||
124 | Token::class, |
||
125 | Token::EVENT_AFTER_INSERT, |
||
126 | [ |
||
127 | ManageTokenProjectConfig::class, |
||
128 | 'save' |
||
129 | ] |
||
130 | ); |
||
131 | |||
132 | Event::off( |
||
133 | Token::class, |
||
134 | Token::EVENT_AFTER_UPDATE, |
||
135 | [ |
||
136 | ManageTokenProjectConfig::class, |
||
137 | 'save' |
||
138 | ] |
||
139 | ); |
||
140 | |||
141 | // Get the UID that was matched in the config path |
||
142 | $uid = $event->tokenMatches[0]; |
||
143 | |||
144 | if (null === ($token = Token::findOne([ |
||
145 | 'uid' => $uid, |
||
146 | 'enabled' => null |
||
147 | ]))) { |
||
148 | $token = new Token(); |
||
149 | } |
||
150 | |||
151 | // Compare dates from config |
||
152 | $configDateUpdated = $event->newValue['dateUpdated'] ?? null; |
||
153 | $tokenDateUpdated = $token->dateUpdated ?? null; |
||
154 | |||
155 | // If the token has been updated more recently in the database, use it |
||
156 | if ($configDateUpdated && $tokenDateUpdated && strtotime($tokenDateUpdated) > strtotime($configDateUpdated)) { |
||
157 | $event->newValue = array_merge( |
||
158 | $event->newValue, |
||
159 | [ |
||
160 | 'accessToken' => $token->accessToken, |
||
161 | ] |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | // Ignore |
||
166 | unset($event->newValue['dateUpdated']); |
||
167 | |||
168 | Craft::configure($token, $event->newValue); |
||
169 | |||
170 | $token->save(); |
||
171 | |||
172 | Event::on( |
||
173 | Token::class, |
||
174 | Token::EVENT_AFTER_INSERT, |
||
175 | [ |
||
176 | ManageTokenProjectConfig::class, |
||
177 | 'save' |
||
178 | ] |
||
179 | ); |
||
180 | |||
181 | Event::on( |
||
182 | Token::class, |
||
183 | Token::EVENT_AFTER_UPDATE, |
||
184 | [ |
||
185 | ManageTokenProjectConfig::class, |
||
186 | 'save' |
||
187 | ] |
||
188 | ); |
||
189 | } |
||
190 | |||
246 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.