Conditions | 4 |
Paths | 1 |
Total Lines | 64 |
Code Lines | 36 |
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 |
||
91 | public function testInterfaceResolveType() |
||
92 | { |
||
93 | $log = []; |
||
94 | |||
95 | $interfaceResult = new InterfaceType([ |
||
96 | 'name' => 'InterfaceResult', |
||
97 | 'fields' => [ |
||
98 | 'name' => Type::string(), |
||
99 | ], |
||
100 | 'resolveType' => static function ($result, $root, ResolveInfo $info) use (&$a, &$b, &$c, &$log) : Type { |
||
101 | $log[] = [$result, $info->path]; |
||
102 | if (stristr($result['name'], 'A')) { |
||
103 | return $a; |
||
104 | } |
||
105 | if (stristr($result['name'], 'B')) { |
||
106 | return $b; |
||
107 | } |
||
108 | if (stristr($result['name'], 'C')) { |
||
109 | return $c; |
||
110 | } |
||
111 | }, |
||
112 | ]); |
||
113 | |||
114 | $a = new ObjectType(['name' => 'A', 'fields' => ['name' => Type::string()], 'interfaces' => [$interfaceResult]]); |
||
115 | $b = new ObjectType(['name' => 'B', 'fields' => ['name' => Type::string()], 'interfaces' => [$interfaceResult]]); |
||
116 | $c = new ObjectType(['name' => 'C', 'fields' => ['name' => Type::string()], 'interfaces' => [$interfaceResult]]); |
||
117 | |||
118 | $exampleType = new ObjectType([ |
||
119 | 'name' => 'Example', |
||
120 | 'fields' => [ |
||
121 | 'field' => [ |
||
122 | 'type' => Type::nonNull(Type::listOf(Type::nonNull($interfaceResult))), |
||
123 | 'resolve' => static function () : array { |
||
124 | return [ |
||
125 | ['name' => 'A 1'], |
||
126 | ['name' => 'B 2'], |
||
127 | ['name' => 'C 3'], |
||
128 | ]; |
||
129 | }, |
||
130 | ], |
||
131 | ], |
||
132 | ]); |
||
133 | |||
134 | $schema = new Schema([ |
||
135 | 'query' => $exampleType, |
||
136 | 'types' => [$a, $b, $c], |
||
137 | ]); |
||
138 | |||
139 | $query = ' |
||
140 | query { |
||
141 | field { |
||
142 | name |
||
143 | } |
||
144 | } |
||
145 | '; |
||
146 | |||
147 | GraphQL::executeQuery($schema, $query); |
||
148 | |||
149 | $expected = [ |
||
150 | [['name' => 'A 1'], ['field', 0]], |
||
151 | [['name' => 'B 2'], ['field', 1]], |
||
152 | [['name' => 'C 3'], ['field', 2]], |
||
153 | ]; |
||
154 | self::assertEquals($expected, $log); |
||
155 | } |
||
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: