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