| Conditions | 9 |
| Paths | 6 |
| Total Lines | 63 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 44 | public function preUpdate() |
||
| 45 | { |
||
| 46 | /** @var ConfigProvider $extendConfigProvider */ |
||
| 47 | $extendConfigProvider = $this->configManager->getProvider('extend'); |
||
| 48 | /** @var ConfigProvider $activityConfigProvider */ |
||
| 49 | $activityConfigProvider = $this->configManager->getProvider('activity'); |
||
| 50 | |||
| 51 | $contactingActivityClasses = $this->activityContactProvider->getSupportedActivityClasses(); |
||
| 52 | |||
| 53 | $entityConfigs = $extendConfigProvider->getConfigs(); |
||
| 54 | foreach ($entityConfigs as $entityConfig) { |
||
| 55 | if ($entityConfig->is('is_extend')) { |
||
| 56 | $entityClassName = $entityConfig->getId()->getClassName(); |
||
| 57 | // Skipp excluded entity |
||
| 58 | if (TargetExcludeList::isExcluded($entityClassName)) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Check if entity has any activity from contact activities group |
||
| 64 | */ |
||
| 65 | $entityActivities = $activityConfigProvider->getConfig($entityClassName)->get('activities'); |
||
| 66 | if (!$entityActivities |
||
| 67 | || !array_intersect($contactingActivityClasses, $entityActivities) |
||
| 68 | ) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** @var ConfigInterface[] $entityFields */ |
||
| 73 | $entityFields = $extendConfigProvider->getConfigs($entityClassName); |
||
| 74 | $entityFieldNames = array_map( |
||
| 75 | function (ConfigInterface $item) { |
||
| 76 | return $item->getId()->getFieldName(); |
||
|
|
|||
| 77 | }, |
||
| 78 | $entityFields |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Check if entity already has all needed fields. |
||
| 83 | * If at least one is not present we should check and add it too. |
||
| 84 | */ |
||
| 85 | if (false === (bool) array_diff( |
||
| 86 | array_keys(ActivityScope::$fieldsConfiguration), |
||
| 87 | array_intersect($entityFieldNames, array_keys(ActivityScope::$fieldsConfiguration)) |
||
| 88 | )) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | foreach (ActivityScope::$fieldsConfiguration as $fieldName => $fieldConfig) { |
||
| 93 | if (!in_array($fieldName, $entityFieldNames)) { |
||
| 94 | $this->configManager->createConfigFieldModel( |
||
| 95 | $entityClassName, |
||
| 96 | $fieldName, |
||
| 97 | $fieldConfig['type'], |
||
| 98 | $fieldConfig['mode'] |
||
| 99 | ); |
||
| 100 | |||
| 101 | $this->updateConfigs($entityClassName, $fieldName, $fieldConfig['options']); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 130 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: