| Conditions | 4 |
| Paths | 3 |
| Total Lines | 54 |
| Code Lines | 35 |
| 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 |
||
| 62 | protected function buildFieldMap($fieldsOrThunk): array |
||
| 63 | { |
||
| 64 | $fields = resolveThunk($fieldsOrThunk); |
||
| 65 | |||
| 66 | invariant( |
||
| 67 | isAssocArray($fields), |
||
| 68 | \sprintf( |
||
| 69 | '%s fields must be an associative array with field names as key or a callable which returns such an array.', |
||
| 70 | $this->getName() |
||
| 71 | ) |
||
| 72 | ); |
||
| 73 | |||
| 74 | $fieldMap = []; |
||
| 75 | |||
| 76 | foreach ($fields as $fieldName => $fieldConfig) { |
||
| 77 | invariant( |
||
| 78 | \is_array($fieldConfig), |
||
| 79 | \sprintf('%s.%s field config must be an array', $this->getName(), $fieldName) |
||
| 80 | ); |
||
| 81 | |||
| 82 | invariant( |
||
| 83 | !isset($fieldConfig['isDeprecated']), |
||
| 84 | \sprintf( |
||
| 85 | '%s.%s should provide "deprecationReason" instead of "isDeprecated".', |
||
| 86 | $this->getName(), |
||
| 87 | $fieldName |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | if (isset($fieldConfig['resolve'])) { |
||
| 92 | invariant( |
||
| 93 | null === $fieldConfig['resolve'] || \is_callable($fieldConfig['resolve']), |
||
| 94 | \sprintf( |
||
| 95 | '%s.%s field resolver must be a function if provided, but got: %s', |
||
| 96 | $this->getName(), |
||
| 97 | $fieldName, |
||
| 98 | toString($fieldConfig['resolve']) |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | $fieldMap[$fieldName] = new Field( |
||
| 104 | $fieldName, |
||
| 105 | $fieldConfig['description'] ?? null, |
||
| 106 | $fieldConfig['type'] ?? null, |
||
| 107 | $fieldConfig['args'] ?? [], |
||
| 108 | $fieldConfig['resolve'] ?? null, |
||
| 109 | $fieldConfig['subscribe'] ?? null, |
||
| 110 | $fieldConfig['deprecationReason'] ?? null, |
||
| 111 | $fieldConfig['astNode'] ?? null |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $fieldMap; |
||
| 116 | } |
||
| 118 |