Conditions | 8 |
Paths | 18 |
Total Lines | 67 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 1 |
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 |
||
42 | public static function generate(string $typeName, array $typeFields): string |
||
43 | { |
||
44 | $arguments = []; |
||
45 | |||
46 | foreach ($typeFields as $fieldName => $field) { |
||
47 | $className = get_class($field); |
||
48 | // We can generate queries for all but Object types, as Object types are relations |
||
49 | if (! in_array($className, self::$supportedGraphQLTypes)) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | if ($field instanceof FullTextSearch) { |
||
54 | $arguments[] = sprintf('%s: %s @fulltext', $fieldName, $field->name); |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | // Add all our custom directives |
||
59 | $arguments[] = sprintf('%s: %s @eq', $fieldName, $field->name); |
||
60 | $arguments[] = sprintf('%s_not: %s @not', $fieldName, $field->name); |
||
61 | $arguments[] = sprintf('%s_in: [%s] @in', $fieldName, $field->name); |
||
62 | $arguments[] = sprintf('%s_not_in: [%s] @not_in', $fieldName, $field->name); |
||
63 | |||
64 | if ($field instanceof EnumType) { |
||
65 | continue; |
||
66 | } |
||
67 | |||
68 | if ($field instanceof StringType) { |
||
69 | $arguments[] = sprintf('%s_contains: %s @contains', $fieldName, $field->name); |
||
70 | $arguments[] = sprintf('%s_not_contains: %s @not_contains', $fieldName, $field->name); |
||
71 | $arguments[] = sprintf('%s_starts_with: %s @starts_with', $fieldName, $field->name); |
||
72 | $arguments[] = sprintf('%s_not_starts_with: %s @not_starts_with', $fieldName, $field->name); |
||
73 | $arguments[] = sprintf('%s_ends_with: %s @not_ends_with', $fieldName, $field->name); |
||
74 | |||
75 | continue; |
||
76 | } |
||
77 | |||
78 | |||
79 | $arguments[] = sprintf('%s_lt: %s @lt', $fieldName, $field->name); |
||
80 | $arguments[] = sprintf('%s_lte: %s @lte', $fieldName, $field->name); |
||
81 | $arguments[] = sprintf('%s_gt: %s @gt', $fieldName, $field->name); |
||
82 | $arguments[] = sprintf('%s_gte: %s @gte', $fieldName, $field->name); |
||
83 | } |
||
84 | |||
85 | if (count($arguments) < 1) { |
||
86 | return ''; |
||
87 | } |
||
88 | |||
89 | $allQueryName = str_plural(lcfirst($typeName)); |
||
90 | $queryArguments = sprintf('(%s)', implode(', ', $arguments)); |
||
91 | $allQuery = sprintf(' %1$s%2$s: [%3$s]! @all(model: "%3$s")', $allQueryName, $queryArguments, $typeName); |
||
92 | |||
93 | $paginatedQueryName = str_plural(lcfirst($typeName)) . 'Paginated'; |
||
94 | $paginatedQuery = sprintf(' %1$s%2$s: [%3$s]! @paginate(model: "%3$s")', $paginatedQueryName, $queryArguments, $typeName); |
||
95 | |||
96 | if (config('lighthouse-utils.authorization')) { |
||
97 | $allPermission = sprintf('All%1$s', str_plural($typeName)); |
||
98 | $allQuery .= sprintf(' @can(if: "%1$s", model: "User")', $allPermission); |
||
99 | |||
100 | $paginatePermission = sprintf('paginate%1$s', str_plural($typeName)); |
||
101 | $paginatedQuery .= sprintf(' @can(if: "%1$s", model: "User")', $paginatePermission); |
||
102 | |||
103 | GraphQLSchema::register($allQueryName, $typeName, 'query', $allPermission ?? null); |
||
104 | GraphQLSchema::register($paginatedQueryName, $typeName, 'query', $paginatePermission ?? null); |
||
105 | } |
||
106 | |||
107 | |||
108 | return $allQuery ."\r\n". $paginatedQuery; |
||
109 | } |
||
111 |