Conditions | 17 |
Paths | 1153 |
Total Lines | 112 |
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 |
||
212 | protected function processSaveAction() |
||
213 | { |
||
214 | |||
215 | $providerId = Craft::$app->request->getParam('identifier'); |
||
216 | $keyId = Craft::$app->request->getParam('keychain'); |
||
217 | $providerType = Craft::$app->request->getParam('providerType'); |
||
218 | $metadata = Craft::$app->request->getParam('metadata-text'); |
||
219 | $metadataUrl = Craft::$app->request->getParam('metadata-url-text'); |
||
220 | $metadataUrlInterval = Craft::$app->request->getParam('metadata-url-interval-text'); |
||
221 | $mapping = Craft::$app->request->getParam('mapping', []); |
||
222 | $label = Craft::$app->request->getRequiredParam('label'); |
||
223 | $nameIdOverride = Craft::$app->request->getParam('nameIdOverride'); |
||
224 | |||
225 | |||
226 | $plugin = $this->getPlugin(); |
||
227 | |||
228 | $recordClass = $this->getPlugin()->getProviderRecordClass(); |
||
229 | /** @var AbstractProvider $record */ |
||
230 | if ($providerId) { |
||
231 | $record = $recordClass::find()->where([ |
||
232 | 'id' => $providerId, |
||
233 | ])->one(); |
||
234 | |||
235 | if (! $record) { |
||
236 | throw new \Exception("Provider with ID: {$providerId} not found."); |
||
237 | } |
||
238 | } else { |
||
239 | $record = new $recordClass(); |
||
240 | |||
241 | //enabled is default |
||
242 | $record->enabled = true; |
||
243 | } |
||
244 | |||
245 | |||
246 | // Metadata |
||
247 | if (! $metadata && $metadataUrl) { |
||
248 | $metadataModel = $this->getPlugin()->getMetadata()->fetchByUrl($metadataUrl); |
||
249 | $record->metadata = $metadataModel->toXML()->ownerDocument->saveXML(); |
||
250 | $record->setMetadataModel($metadataModel); |
||
251 | } else { |
||
252 | $record->metadata = $metadata; |
||
253 | } |
||
254 | |||
255 | // Mapping |
||
256 | if (is_array($mapping)) { |
||
257 | $record->setMapping( |
||
258 | $mapping |
||
259 | ); |
||
260 | } |
||
261 | |||
262 | $record->providerType = $providerType; |
||
263 | $record->nameIdOverride = $nameIdOverride; |
||
264 | |||
265 | // IDP Plugin on SP Provider ONLY |
||
266 | if ($this->getPlugin()->getMyType() === SettingsInterface::IDP |
||
267 | && |
||
268 | $providerType === SettingsInterface::SP |
||
269 | ) { |
||
270 | // Encryption settings |
||
271 | $record->encryptAssertions = Craft::$app->request->getParam('encryptAssertions') ?: 0; |
||
272 | $record->encryptionMethod = Craft::$app->request->getParam('encryptionMethod'); |
||
273 | $record->setGroupOptions( |
||
274 | $groupOptions = new GroupOptions([ |
||
275 | 'options' => Craft::$app->request->getParam('groupOptions', []) ?: [], |
||
276 | ]) |
||
277 | ); |
||
278 | } |
||
279 | |||
280 | $record->setMetadataOptions( |
||
281 | new MetadataOptions([ |
||
282 | 'url' => $metadataUrl, |
||
283 | 'expiryInterval' => $metadataUrlInterval, |
||
284 | ]) |
||
285 | ); |
||
286 | |||
287 | // Group properties |
||
288 | $record->syncGroups = Craft::$app->request->getParam('syncGroups') ?: 0; |
||
289 | |||
290 | $record->groupsAttributeName = |
||
291 | Craft::$app->request->getParam('groupsAttributeName') ?: |
||
292 | AbstractProvider::DEFAULT_GROUPS_ATTRIBUTE_NAME; |
||
293 | |||
294 | /** |
||
295 | * check for label and add error if it's empty |
||
296 | */ |
||
297 | if ($label) { |
||
298 | $record->label = $label; |
||
299 | } else { |
||
300 | $record->addError('label', Craft::t($plugin->getHandle(), "Label is required.")); |
||
301 | } |
||
302 | |||
303 | |||
304 | if ($keyId) { |
||
305 | /** @var KeyChainRecord $keychain */ |
||
306 | if ($keychain = KeyChainRecord::find()->where([ |
||
307 | 'id' => $keyId, |
||
308 | ])->one()) { |
||
309 | $record->setKeychain( |
||
310 | $keychain |
||
311 | ); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Metadata should exist for the remote provider |
||
317 | */ |
||
318 | if ($plugin->getRemoteType() === $providerType && ! $record->metadata) { |
||
319 | $record->addError('metadata-text', Craft::t($plugin->getHandle(), "Metadata cannot be empty.")); |
||
320 | } |
||
321 | |||
322 | return $record; |
||
323 | } |
||
324 | |||
346 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: