Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | * created: 12/6/15 11:15 PM |
||
18 | */ |
||
19 | |||
20 | namespace Youshido\Tests\StarWars\Schema; |
||
21 | |||
22 | use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
||
23 | use Youshido\GraphQL\Type\ListType\ListType; |
||
24 | use Youshido\GraphQL\Type\NonNullType; |
||
25 | use Youshido\GraphQL\Type\Scalar\IdType; |
||
26 | use Youshido\GraphQL\Type\Scalar\StringType; |
||
27 | |||
28 | class CharacterInterface extends AbstractInterfaceType |
||
29 | { |
||
30 | public function build($config): void |
||
31 | { |
||
32 | $config |
||
33 | ->addField('id', new NonNullType(new IdType())) |
||
34 | ->addField('name', new NonNullType(new StringType())) |
||
35 | ->addField('friends', [ |
||
36 | 'type' => new ListType(new self()), |
||
37 | 'resolve' => static function ($value) { |
||
38 | return $value['friends']; |
||
39 | }, |
||
40 | ]) |
||
41 | ->addField('appearsIn', new ListType(new EpisodeEnum())); |
||
42 | } |
||
43 | |||
44 | public function getDescription() |
||
45 | { |
||
46 | return 'A character in the Star Wars Trilogy'; |
||
47 | } |
||
48 | |||
49 | public function getName() |
||
50 | { |
||
51 | return 'Character'; |
||
52 | } |
||
53 | |||
54 | public function resolveType($object) |
||
55 | { |
||
56 | $humans = StarWarsData::humans(); |
||
57 | $droids = StarWarsData::droids(); |
||
58 | |||
59 | $id = $object['id'] ?? $object; |
||
60 | |||
61 | if (isset($humans[$id])) { |
||
62 | return new HumanType(); |
||
70 |