Conditions | 4 |
Paths | 1 |
Total Lines | 67 |
Code Lines | 33 |
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 |
||
22 | public function testUnionResolveType() |
||
23 | { |
||
24 | $a = new ObjectType(['name' => 'A', 'fields' => ['name' => Type::string()]]); |
||
25 | $b = new ObjectType(['name' => 'B', 'fields' => ['name' => Type::string()]]); |
||
26 | $c = new ObjectType(['name' => 'C', 'fields' => ['name' => Type::string()]]); |
||
27 | |||
28 | $log = []; |
||
29 | |||
30 | $unionResult = new UnionType([ |
||
31 | 'name' => 'UnionResult', |
||
32 | 'types' => [$a, $b, $c], |
||
33 | 'resolveType' => static function ($result, $root, ResolveInfo $info) use ($a, $b, $c, &$log) : Type { |
||
34 | $log[] = [$result, $info->path]; |
||
35 | if (stristr($result['name'], 'A')) { |
||
36 | return $a; |
||
37 | } |
||
38 | if (stristr($result['name'], 'B')) { |
||
39 | return $b; |
||
40 | } |
||
41 | if (stristr($result['name'], 'C')) { |
||
42 | return $c; |
||
43 | } |
||
|
|||
44 | }, |
||
45 | ]); |
||
46 | |||
47 | $exampleType = new ObjectType([ |
||
48 | 'name' => 'Example', |
||
49 | 'fields' => [ |
||
50 | 'field' => [ |
||
51 | 'type' => Type::nonNull(Type::listOf(Type::nonNull($unionResult))), |
||
52 | 'resolve' => static function () : array { |
||
53 | return [ |
||
54 | ['name' => 'A 1'], |
||
55 | ['name' => 'B 2'], |
||
56 | ['name' => 'C 3'], |
||
57 | ]; |
||
58 | }, |
||
59 | ], |
||
60 | ], |
||
61 | ]); |
||
62 | |||
63 | $schema = new Schema(['query' => $exampleType]); |
||
64 | |||
65 | $query = ' |
||
66 | query { |
||
67 | field { |
||
68 | ... on A { |
||
69 | name |
||
70 | } |
||
71 | ... on B { |
||
72 | name |
||
73 | } |
||
74 | ... on C { |
||
75 | name |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | '; |
||
80 | |||
81 | GraphQL::executeQuery($schema, $query); |
||
82 | |||
83 | $expected = [ |
||
84 | [['name' => 'A 1'], ['field', 0]], |
||
85 | [['name' => 'B 2'], ['field', 1]], |
||
86 | [['name' => 'C 3'], ['field', 2]], |
||
87 | ]; |
||
88 | self::assertEquals($expected, $log); |
||
89 | } |
||
157 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: