We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 7 |
| Paths | 1 |
| Total Lines | 67 |
| Code Lines | 40 |
| 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 |
||
| 14 | protected function map() |
||
| 15 | { |
||
| 16 | return [ |
||
| 17 | 'Query' => [ |
||
| 18 | self::RESOLVE_FIELD => function ($value, Argument $args, \ArrayObject $context, ResolveInfo $info) { |
||
| 19 | if ('character' === $info->fieldName) { |
||
| 20 | $characters = Characters::getCharacters(); |
||
| 21 | $id = (int) $args['id']; |
||
| 22 | if (isset($characters[$id])) { |
||
| 23 | return $characters[$id]; |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | return null; |
||
| 28 | }, |
||
| 29 | 'findHumansByDateOfBirth' => function ($value, Argument $args) { |
||
| 30 | $years = $args['years']; |
||
| 31 | |||
| 32 | return array_filter(Characters::getHumans(), function ($human) use ($years) { |
||
| 33 | return in_array($human['dateOfBirth'], $years); |
||
| 34 | }); |
||
| 35 | }, |
||
| 36 | 'humans' => [Characters::class, 'getHumans'], |
||
| 37 | 'direwolves' => [Characters::class, 'getDirewolves'], |
||
| 38 | ], |
||
| 39 | 'Character' => [ |
||
| 40 | self::RESOLVE_TYPE => function ($value) { |
||
| 41 | return Characters::TYPE_HUMAN === $value['type'] ? 'Human' : 'Direwolf'; |
||
| 42 | }, |
||
| 43 | ], |
||
| 44 | 'Human' => [ |
||
| 45 | 'direwolf' => function ($value) { |
||
| 46 | $direwolves = Characters::getDirewolves(); |
||
| 47 | if (isset($direwolves[$value['direwolf']])) { |
||
| 48 | return $direwolves[$value['direwolf']]; |
||
| 49 | } else { |
||
| 50 | return null; |
||
| 51 | } |
||
| 52 | }, |
||
| 53 | ], |
||
| 54 | // enum internal values |
||
| 55 | 'Status' => [ |
||
| 56 | 'ALIVE' => 1, |
||
| 57 | 'DECEASED' => 0, |
||
| 58 | ], |
||
| 59 | // custom scalar |
||
| 60 | 'Year' => [ |
||
| 61 | self::SERIALIZE => function ($value) { |
||
| 62 | return sprintf('%s AC', $value); |
||
| 63 | }, |
||
| 64 | self::PARSE_VALUE => function ($value) { |
||
| 65 | if (!is_string($value)) { |
||
| 66 | throw new Error(sprintf('Cannot represent following value as a valid year: %s.', Utils::printSafeJson($value))); |
||
| 67 | } |
||
| 68 | |||
| 69 | return (int) str_replace(' AC', '', $value); |
||
| 70 | }, |
||
| 71 | self::PARSE_LITERAL => function ($valueNode) { |
||
| 72 | if (!$valueNode instanceof StringValueNode) { |
||
| 73 | throw new Error('Query error: Can only parse strings got: '.$valueNode->kind, [$valueNode]); |
||
| 74 | } |
||
| 75 | |||
| 76 | return (int) str_replace(' AC', '', $valueNode->value); |
||
| 77 | }, |
||
| 78 | ], |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | } |
||
| 82 |