| Conditions | 19 |
| Paths | 2307 |
| Total Lines | 126 |
| 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 |
||
| 216 | protected function processSaveAction() |
||
| 217 | { |
||
| 218 | |||
| 219 | $providerId = Craft::$app->request->getParam('identifier'); |
||
| 220 | $entityId = Craft::$app->request->getParam('entityId'); |
||
| 221 | $keyId = Craft::$app->request->getParam('keychain'); |
||
| 222 | $providerType = Craft::$app->request->getParam('providerType'); |
||
| 223 | $providerSite = Craft::$app->request->getParam('providerSite'); |
||
| 224 | $metadata = Craft::$app->request->getParam('metadata-text'); |
||
| 225 | $metadataUrl = Craft::$app->request->getParam('metadata-url-text'); |
||
| 226 | $metadataUrlInterval = Craft::$app->request->getParam('metadata-url-interval-text'); |
||
| 227 | $mapping = Craft::$app->request->getParam('mapping', []); |
||
| 228 | $label = Craft::$app->request->getRequiredParam('label'); |
||
| 229 | $nameIdOverride = Craft::$app->request->getParam('nameIdOverride'); |
||
| 230 | |||
| 231 | |||
| 232 | $plugin = $this->getPlugin(); |
||
| 233 | |||
| 234 | $recordClass = $this->getPlugin()->getProviderRecordClass(); |
||
| 235 | /** @var AbstractProvider $record */ |
||
| 236 | if ($providerId) { |
||
| 237 | $record = $recordClass::find()->where([ |
||
| 238 | 'id' => $providerId, |
||
| 239 | ])->one(); |
||
| 240 | |||
| 241 | if (! $record) { |
||
| 242 | throw new \Exception("Provider with ID: {$providerId} not found."); |
||
| 243 | } |
||
| 244 | } else { |
||
| 245 | $record = new $recordClass(); |
||
| 246 | |||
| 247 | //enabled is default |
||
| 248 | $record->enabled = true; |
||
| 249 | } |
||
| 250 | |||
| 251 | $record->entityId = $entityId; |
||
| 252 | if ($providerSite) { |
||
| 253 | if ($site = Site::findOne([ |
||
| 254 | 'id' => $providerSite, |
||
| 255 | ])) { |
||
| 256 | $record->setSite($site); |
||
| 257 | } else { |
||
| 258 | // @todo add |
||
| 259 | var_dump('wat?'); |
||
| 260 | exit; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | // Metadata |
||
| 265 | if (! $metadata && $metadataUrl) { |
||
| 266 | $metadataModel = $this->getPlugin()->getMetadata()->fetchByUrl($metadataUrl); |
||
| 267 | $record->metadata = $metadataModel->toXML()->ownerDocument->saveXML(); |
||
| 268 | $record->setMetadataModel($metadataModel); |
||
| 269 | } else { |
||
| 270 | $record->metadata = $metadata; |
||
| 271 | } |
||
| 272 | |||
| 273 | // Mapping |
||
| 274 | if (is_array($mapping)) { |
||
| 275 | $record->setMapping( |
||
| 276 | $mapping |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | |||
| 280 | $record->providerType = $providerType; |
||
| 281 | $record->nameIdOverride = $nameIdOverride; |
||
| 282 | |||
| 283 | // IDP Plugin on SP Provider ONLY |
||
| 284 | if ($this->getPlugin()->getMyType() === SettingsInterface::IDP |
||
| 285 | && |
||
| 286 | $providerType === SettingsInterface::SP |
||
| 287 | ) { |
||
| 288 | // Encryption settings |
||
| 289 | $record->encryptAssertions = Craft::$app->request->getParam('encryptAssertions') ?: 0; |
||
| 290 | $record->encryptionMethod = Craft::$app->request->getParam('encryptionMethod'); |
||
| 291 | $record->setGroupOptions( |
||
| 292 | $groupOptions = new GroupOptions([ |
||
| 293 | 'options' => Craft::$app->request->getParam('groupOptions', []) ?: [], |
||
| 294 | ]) |
||
| 295 | ); |
||
| 296 | } |
||
| 297 | |||
| 298 | $record->setMetadataOptions( |
||
| 299 | new MetadataOptions([ |
||
| 300 | 'url' => $metadataUrl, |
||
| 301 | 'expiryInterval' => $metadataUrlInterval, |
||
| 302 | ]) |
||
| 303 | ); |
||
| 304 | |||
| 305 | // Group properties |
||
| 306 | $record->syncGroups = Craft::$app->request->getParam('syncGroups') ?: 0; |
||
| 307 | |||
| 308 | $record->groupsAttributeName = |
||
| 309 | Craft::$app->request->getParam('groupsAttributeName') ?: |
||
| 310 | AbstractProvider::DEFAULT_GROUPS_ATTRIBUTE_NAME; |
||
| 311 | |||
| 312 | /** |
||
| 313 | * check for label and add error if it's empty |
||
| 314 | */ |
||
| 315 | if ($label) { |
||
| 316 | $record->label = $label; |
||
| 317 | } else { |
||
| 318 | $record->addError('label', Craft::t($plugin->getHandle(), "Label is required.")); |
||
| 319 | } |
||
| 320 | |||
| 321 | |||
| 322 | if ($keyId) { |
||
| 323 | /** @var KeyChainRecord $keychain */ |
||
| 324 | if ($keychain = KeyChainRecord::find()->where([ |
||
| 325 | 'id' => $keyId, |
||
| 326 | ])->one()) { |
||
| 327 | $record->setKeychain( |
||
| 328 | $keychain |
||
| 329 | ); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Metadata should exist for the remote provider |
||
| 335 | */ |
||
| 336 | if ($plugin->getRemoteType() === $providerType && ! $record->metadata) { |
||
| 337 | $record->addError('metadata-text', Craft::t($plugin->getHandle(), "Metadata cannot be empty.")); |
||
| 338 | } |
||
| 339 | |||
| 340 | return $record; |
||
| 341 | } |
||
| 342 | |||
| 366 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: