| Conditions | 11 |
| Paths | 1 |
| Total Lines | 49 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 178 | public function getMapFunc() |
||
| 179 | { |
||
| 180 | $objectCreator = $this->getObjectCreator(); |
||
| 181 | $propertyMaps = $this->getMappedPropertyMaps(); |
||
| 182 | $sourceType = $this->getSourceType(); |
||
| 183 | $destinationType = $this->getDestinationType(); |
||
| 184 | $beforeMapFunc = $this->getBeforeMapFunc(); |
||
| 185 | $afterMapFunc = $this->getAfterMapFunc(); |
||
| 186 | |||
| 187 | return function ($source, $destination = null) use ($objectCreator, $propertyMaps, $sourceType, $destinationType, $beforeMapFunc, $afterMapFunc) { |
||
| 188 | |||
| 189 | if ($destination === null) { |
||
| 190 | $destination = $objectCreator->create($source); |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!$destination instanceof $destinationType) { |
||
| 194 | $type = is_object($destination) ? get_class($destination) : gettype($destination); |
||
| 195 | $message = sprintf('Constructed object type expected %s, but actual %s', $destinationType, $type); |
||
| 196 | throw new ValidationException($message); |
||
| 197 | } |
||
| 198 | |||
| 199 | if (!$source instanceof $sourceType) { |
||
| 200 | $type = is_object($source) ? get_class($source) : gettype($source); |
||
| 201 | $message = sprintf('Source object type expected %s, but actual %s', $destinationType, $type); |
||
| 202 | throw new ValidationException($message); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($beforeMapFunc) { |
||
| 206 | $beforeMapFunc($source, $destination); |
||
| 207 | } |
||
| 208 | |||
| 209 | foreach ($propertyMaps as $propertyMap) { |
||
| 210 | $value = $propertyMap->getSourceGetter()->getValue($source); |
||
| 211 | if ($propertyMap->hasValueConverter()) { |
||
| 212 | $value = $propertyMap->getValueConverter()->convert($value); |
||
| 213 | } |
||
| 214 | if ($value === null) { |
||
| 215 | $value = $propertyMap->getNullSubtitute(); |
||
| 216 | } |
||
| 217 | $propertyMap->getDestinationSetter()->setValue($destination, $value); |
||
| 218 | } |
||
| 219 | |||
| 220 | if ($afterMapFunc) { |
||
| 221 | $afterMapFunc($source, $destination); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $destination; |
||
| 225 | }; |
||
| 226 | } |
||
| 227 | } |
||
| 228 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.