| Conditions | 5 |
| Paths | 1 |
| Total Lines | 88 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 14 | ||
| Bugs | 3 | Features | 3 |
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 build(SchemaConfig $config) |
||
| 20 | { |
||
| 21 | $fetcher = new CallableFetcher( |
||
| 22 | function ($type, $id) { |
||
| 23 | switch ($type) { |
||
| 24 | case FactionType::TYPE_KEY: |
||
| 25 | return TestDataProvider::getFaction($id); |
||
| 26 | |||
| 27 | case |
||
|
|
|||
| 28 | ShipType::TYPE_KEY: |
||
| 29 | return TestDataProvider::getShip($id); |
||
| 30 | } |
||
| 31 | |||
| 32 | return null; |
||
| 33 | }, |
||
| 34 | function ($object) { |
||
| 35 | return $object && array_key_exists('ships', $object) ? new FactionType() : new ShipType(); |
||
| 36 | } |
||
| 37 | ); |
||
| 38 | |||
| 39 | $config->getQuery() |
||
| 40 | ->addField(new NodeField($fetcher)) |
||
| 41 | ->addField('rebels', [ |
||
| 42 | 'type' => new FactionType(), |
||
| 43 | 'resolve' => function () { |
||
| 44 | return TestDataProvider::getFaction('rebels'); |
||
| 45 | } |
||
| 46 | ]) |
||
| 47 | ->addField('empire', [ |
||
| 48 | 'type' => new FactionType(), |
||
| 49 | 'resolve' => function () { |
||
| 50 | return TestDataProvider::getFaction('empire'); |
||
| 51 | } |
||
| 52 | ]) |
||
| 53 | ->addField('factions', [ |
||
| 54 | 'type' => new ListType(new FactionType()), |
||
| 55 | 'args' => [ |
||
| 56 | 'names' => [ |
||
| 57 | 'type' => new ListType(new StringType()) |
||
| 58 | ] |
||
| 59 | ], |
||
| 60 | 'resolve' => function ($value = null, $args, $info) { |
||
| 61 | return TestDataProvider::getByNames($args['names']); |
||
| 62 | } |
||
| 63 | ]); |
||
| 64 | |||
| 65 | |||
| 66 | $config->getMutation() |
||
| 67 | ->addField( |
||
| 68 | RelayMutation::buildMutation( |
||
| 69 | 'introduceShip', |
||
| 70 | [ |
||
| 71 | new InputField(['name' => 'shipName', 'type' => new NonNullType(new StringType())]), |
||
| 72 | new InputField(['name' => 'factionId', 'type' => new NonNullType(new StringType())]) |
||
| 73 | ], |
||
| 74 | [ |
||
| 75 | 'newShipEdge' => [ |
||
| 76 | 'type' => Connection::edgeDefinition(new ShipType(), 'newShip'), |
||
| 77 | 'resolve' => function ($value) { |
||
| 78 | $allShips = TestDataProvider::getShips(); |
||
| 79 | $newShip = TestDataProvider::getShip($value['shipId']); |
||
| 80 | |||
| 81 | return [ |
||
| 82 | 'cursor' => ArrayConnection::cursorForObjectInConnection($allShips, $newShip), |
||
| 83 | 'node' => $newShip |
||
| 84 | ]; |
||
| 85 | } |
||
| 86 | ], |
||
| 87 | 'faction' => [ |
||
| 88 | 'type' => new FactionType(), |
||
| 89 | 'resolve' => function ($value) { |
||
| 90 | return TestDataProvider::getFaction($value['factionId']); |
||
| 91 | } |
||
| 92 | ] |
||
| 93 | ], |
||
| 94 | function ($value, $args, $info) { |
||
| 95 | $newShip = TestDataProvider::createShip($args['shipName'], $args['factionId']); |
||
| 96 | |||
| 97 | return [ |
||
| 98 | 'shipId' => $newShip['id'], |
||
| 99 | 'factionId' => $args['factionId'] |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | ) |
||
| 103 | ); |
||
| 104 | |||
| 105 | /** https://github.com/graphql/graphql-relay-js/blob/master/src/__tests__/starWarsSchema.js */ |
||
| 106 | } |
||
| 107 | |||
| 109 |
As per the PSR-2 coding standard, there must be a space after the
casekeyword, instead of the test immediately following it.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.