Conditions | 10 |
Paths | 50 |
Total Lines | 124 |
Code Lines | 92 |
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 |
||
101 | public function configure(DefinitionInterface $definition, Endpoint $endpoint, array $config): void |
||
102 | { |
||
103 | if (!$config) { |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | if (!$definition instanceof QueryDefinition && !$definition instanceof FieldDefinition) { |
||
108 | return; |
||
109 | } |
||
110 | |||
111 | $search = new ArgumentDefinition(); |
||
112 | $search->setName('search'); |
||
113 | $search->setType('string'); |
||
114 | $search->setNonNull(false); |
||
115 | $search->setDescription('Search in current list by given string'); |
||
116 | $definition->addArgument($search); |
||
117 | |||
118 | $first = new ArgumentDefinition(); |
||
119 | $first->setName('first'); |
||
120 | $first->setType('int'); |
||
121 | $first->setNonNull(false); |
||
122 | $first->setDescription('Returns the first *n* elements from the list.'); |
||
123 | $definition->addArgument($first); |
||
124 | |||
125 | $last = new ArgumentDefinition(); |
||
126 | $last->setName('last'); |
||
127 | $last->setType('int'); |
||
128 | $last->setNonNull(false); |
||
129 | $last->setDescription('Returns the last *n* elements from the list.'); |
||
130 | $definition->addArgument($last); |
||
131 | |||
132 | $after = new ArgumentDefinition(); |
||
133 | $after->setName('after'); |
||
134 | $after->setType('string'); |
||
135 | $after->setNonNull(false); |
||
136 | $after->setDescription('Returns the last *n* elements from the list.'); |
||
137 | $definition->addArgument($after); |
||
138 | |||
139 | $before = new ArgumentDefinition(); |
||
140 | $before->setName('before'); |
||
141 | $before->setType('string'); |
||
142 | $before->setNonNull(false); |
||
143 | $before->setDescription('Returns the last *n* elements from the list.'); |
||
144 | $definition->addArgument($before); |
||
145 | |||
146 | if (!$definition->hasArgument('orderBy')) { |
||
147 | $orderBy = new ArgumentDefinition(); |
||
148 | $orderBy->setName('orderBy'); |
||
149 | $orderBy->setType(OrderBy::class); |
||
150 | $orderBy->setNonNull(false); |
||
151 | $orderBy->setList(true); |
||
152 | $orderBy->setDescription('Ordering options for this list.'); |
||
153 | $definition->addArgument($orderBy); |
||
154 | } else { |
||
155 | //if exist move to the end |
||
156 | $orderBy = $definition->getArgument('orderBy'); |
||
157 | $definition->removeArgument('orderBy'); |
||
158 | $definition->addArgument($orderBy); |
||
159 | } |
||
160 | |||
161 | $target = null; |
||
162 | if ($definition instanceof FieldDefinition) { |
||
163 | $target = $definition->getType(); |
||
164 | } |
||
165 | |||
166 | $target = $config['target'] ?? $target; |
||
167 | if ($endpoint->hasTypeForClass($target)) { |
||
168 | $target = $endpoint->getTypeForClass($target); |
||
169 | } |
||
170 | |||
171 | $connection = new ObjectDefinition(); |
||
172 | $connection->setName(ucfirst($target).'Connection'); |
||
173 | |||
174 | if (!$endpoint->hasType($connection->getName())) { |
||
175 | $endpoint->addType($connection); |
||
176 | |||
177 | $totalCount = new FieldDefinition(); |
||
178 | $totalCount->setName('totalCount'); |
||
179 | $totalCount->setType('Int'); |
||
180 | $totalCount->setNonNull(true); |
||
181 | $connection->addField($totalCount); |
||
182 | |||
183 | $pageInfo = new FieldDefinition(); |
||
184 | $pageInfo->setName('pageInfo'); |
||
185 | $pageInfo->setType('PageInfo'); |
||
186 | $pageInfo->setNonNull(true); |
||
187 | $connection->addField($pageInfo); |
||
188 | |||
189 | $edgeObject = new ObjectDefinition(); |
||
190 | $edgeObject->setName(ucfirst($target).'Edge'); |
||
191 | if (!$endpoint->hasType($edgeObject->getName())) { |
||
192 | $endpoint->addType($edgeObject); |
||
193 | |||
194 | $node = new FieldDefinition(); |
||
195 | $node->setName('node'); |
||
196 | $node->setType($target); |
||
197 | $node->setNonNull(true); |
||
198 | $edgeObject->addField($node); |
||
199 | |||
200 | $cursor = new FieldDefinition(); |
||
201 | $cursor->setName('cursor'); |
||
202 | $cursor->setType('string'); |
||
203 | $cursor->setNonNull(true); |
||
204 | $edgeObject->addField($cursor); |
||
205 | } |
||
206 | |||
207 | $edges = new FieldDefinition(); |
||
208 | $edges->setName('edges'); |
||
209 | $edges->setType($edgeObject->getName()); |
||
210 | $edges->setList(true); |
||
211 | $connection->addField($edges); |
||
212 | } |
||
213 | |||
214 | $definition->setType($connection->getName()); |
||
215 | $definition->setList(false); |
||
216 | $definition->setMeta('node', $target); |
||
217 | $definition->setMeta('pagination', $config); |
||
218 | |||
219 | if (!$definition->getResolver()) { |
||
220 | $definition->setResolver(AllNodesWithPagination::class); |
||
221 | } |
||
222 | |||
223 | //TODO: add some config to customize filter |
||
224 | $this->addFilters($definition, $target, $endpoint); |
||
225 | } |
||
286 |