| Conditions | 13 |
| Paths | 65 |
| Total Lines | 89 |
| Code Lines | 44 |
| 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 |
||
| 18 | public function configureFields(\ReflectionClass $class, ShowMapper $showMapper): void |
||
| 19 | { |
||
| 20 | $propertiesAndMethodsWithPosition = []; |
||
| 21 | $propertiesAndMethodsWithoutPosition = []; |
||
| 22 | |||
| 23 | // |
||
| 24 | // Properties |
||
| 25 | // |
||
| 26 | |||
| 27 | foreach ($class->getProperties() as $property) { |
||
| 28 | foreach ($this->getPropertyAnnotations($property) as $annotation) { |
||
| 29 | if (!$annotation instanceof ShowField && !$annotation instanceof ShowAssociationField) { |
||
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | // the name property changes for ShowAssociationField |
||
| 34 | $name = $property->getName(); |
||
| 35 | if ($annotation instanceof ShowAssociationField) { |
||
| 36 | $name .= '.'.$annotation->getField(); |
||
| 37 | } |
||
| 38 | |||
| 39 | if (!$annotation->hasPosition()) { |
||
| 40 | $propertiesAndMethodsWithoutPosition[] = [ |
||
| 41 | 'name' => $name, |
||
| 42 | 'settings' => $annotation->getSettings(), |
||
| 43 | ]; |
||
| 44 | |||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) { |
||
| 49 | throw new \InvalidArgumentException(sprintf( |
||
| 50 | 'Position "%s" is already in use by "%s", try setting a different position for "%s".', |
||
| 51 | $annotation->position, |
||
| 52 | $propertiesAndMethodsWithPosition[$annotation->position]['name'], |
||
| 53 | $property->getName() |
||
| 54 | )); |
||
| 55 | } |
||
| 56 | |||
| 57 | $propertiesAndMethodsWithPosition[$annotation->position] = [ |
||
| 58 | 'name' => $name, |
||
| 59 | 'settings' => $annotation->getSettings(), |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | // |
||
| 65 | // Methods |
||
| 66 | // |
||
| 67 | |||
| 68 | foreach ($class->getMethods() as $method) { |
||
| 69 | if ($annotation = $this->getMethodAnnotation($method, ShowField::class)) { |
||
| 70 | $name = $method->getName(); |
||
| 71 | |||
| 72 | if (!$annotation->hasPosition()) { |
||
| 73 | $propertiesAndMethodsWithoutPosition[] = [ |
||
| 74 | 'name' => $name, |
||
| 75 | 'settings' => $annotation->getSettings(), |
||
| 76 | ]; |
||
| 77 | |||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) { |
||
| 82 | throw new \InvalidArgumentException(sprintf( |
||
| 83 | 'Position "%s" is already in use by "%s", try setting a different position for "%s".', |
||
| 84 | $annotation->position, |
||
| 85 | $propertiesAndMethodsWithPosition[$annotation->position]['name'], |
||
| 86 | $name |
||
| 87 | )); |
||
| 88 | } |
||
| 89 | |||
| 90 | $propertiesAndMethodsWithPosition[$annotation->position] = [ |
||
| 91 | 'name' => $name, |
||
| 92 | 'settings' => $annotation->getSettings(), |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // |
||
| 98 | // Sorting |
||
| 99 | // |
||
| 100 | |||
| 101 | \ksort($propertiesAndMethodsWithPosition); |
||
| 102 | |||
| 103 | $propertiesAndMethods = \array_merge($propertiesAndMethodsWithPosition, $propertiesAndMethodsWithoutPosition); |
||
| 104 | |||
| 105 | foreach ($propertiesAndMethods as $propertyAndMethod) { |
||
| 106 | $showMapper->add($propertyAndMethod['name'], ...$propertyAndMethod['settings']); |
||
|
|
|||
| 107 | } |
||
| 110 |