| Conditions | 15 |
| Paths | 121 |
| Total Lines | 64 |
| 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 |
||
| 104 | public function formProperties(array $group = null) |
||
| 105 | { |
||
| 106 | if (!key_exists(self::DATA_SOURCE_METADATA, array_flip($this->dataSources())) || |
||
| 107 | !$this->widgetMetadata()) { |
||
| 108 | return iterator_to_array($this->form()->formProperties()); |
||
| 109 | } |
||
| 110 | |||
| 111 | $propertyIdentPattern = '%1$s[%2$s]'; |
||
| 112 | $entry = null; |
||
| 113 | |||
| 114 | try { |
||
| 115 | $store = $this->storageProperty(); |
||
| 116 | } catch (\Exception $e) { |
||
| 117 | $store = null; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($store) { |
||
| 121 | $entry = $this->obj()[$store->ident()]; |
||
| 122 | if (is_string($entry)) { |
||
| 123 | $entry = $store->parseVal($entry); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | $props = $this->widgetMetadata()['properties']; |
||
| 128 | |||
| 129 | // We need to sort form properties by form group property order if a group exists |
||
| 130 | if (!empty($group)) { |
||
| 131 | $props = array_merge(array_flip($group), $props); |
||
| 132 | } |
||
| 133 | |||
| 134 | $out = []; |
||
| 135 | |||
| 136 | foreach ($props as $propertyIdent => $propertyMetadata) { |
||
| 137 | if (!is_array($propertyMetadata)) { |
||
| 138 | throw new \UnexpectedValueException(sprintf( |
||
| 139 | 'Invalid property data for "%1$s", received %2$s', |
||
| 140 | $propertyIdent, |
||
| 141 | (is_object($propertyMetadata) ? get_class($propertyMetadata) : gettype($propertyMetadata)) |
||
| 142 | )); |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($store) { |
||
| 146 | $propertyMetadata['input_name'] = sprintf( |
||
| 147 | $propertyIdentPattern, |
||
| 148 | $store['input_name'] ?: $store->ident(), |
||
| 149 | $propertyIdent |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!empty($entry) && isset($entry[$propertyIdent])) { |
||
| 154 | $val = $entry[$propertyIdent]; |
||
| 155 | |||
| 156 | $propertyMetadata['property_val'] = $val; |
||
| 157 | } |
||
| 158 | |||
| 159 | $formProperty = $this->form()->getOrCreateFormProperty($propertyIdent, $propertyMetadata); |
||
| 160 | |||
| 161 | if (!$formProperty->hidden()) { |
||
| 162 | $out[$propertyIdent] = $formProperty; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | return $out; |
||
| 167 | } |
||
| 168 | |||
| 183 |
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: