Conditions | 4 |
Paths | 5 |
Total Lines | 99 |
Code Lines | 65 |
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 |
||
98 | protected function enqueueAssets(): void |
||
99 | { |
||
100 | parent::enqueueAssets(); |
||
101 | |||
102 | $useGraphiQLExplorer = $this->useGraphiQLExplorer(); |
||
103 | |||
104 | // CSS |
||
105 | \wp_enqueue_style( |
||
106 | 'graphql-api-graphiql-client', |
||
107 | \GRAPHQL_API_URL . 'assets/css/graphiql-client.css', |
||
108 | array(), |
||
109 | \GRAPHQL_API_VERSION |
||
110 | ); |
||
111 | |||
112 | // Common settings to both clients |
||
113 | $scriptSettings = array( |
||
114 | 'nonce' => \wp_create_nonce('wp_rest'), |
||
115 | 'response' => $this->getResponse(), |
||
116 | ); |
||
117 | |||
118 | if (!$useGraphiQLExplorer) { |
||
119 | \wp_enqueue_style( |
||
120 | 'graphql-api-graphiql', |
||
121 | \GRAPHQL_API_URL . 'assets/css/vendors/graphiql.min.css', |
||
122 | array(), |
||
123 | \GRAPHQL_API_VERSION |
||
124 | ); |
||
125 | |||
126 | // JS: execute them all in the footer |
||
127 | $this->enqueueReactAssets(true); |
||
128 | |||
129 | \wp_enqueue_script( |
||
130 | 'graphql-api-graphiql', |
||
131 | \GRAPHQL_API_URL . 'assets/js/vendors/graphiql.min.js', |
||
132 | array('graphql-api-react-dom'), |
||
133 | \GRAPHQL_API_VERSION, |
||
134 | true |
||
135 | ); |
||
136 | \wp_enqueue_script( |
||
137 | 'graphql-api-graphiql-client', |
||
138 | \GRAPHQL_API_URL . 'assets/js/graphiql-client.js', |
||
139 | array('graphql-api-graphiql'), |
||
140 | \GRAPHQL_API_VERSION, |
||
141 | true |
||
142 | ); |
||
143 | |||
144 | // Load data into the script |
||
145 | \wp_localize_script( |
||
146 | 'graphql-api-graphiql-client', |
||
147 | 'graphQLByPoPGraphiQLSettings', |
||
148 | array_merge( |
||
149 | [ |
||
150 | 'defaultQuery' => $this->getDefaultQuery(), |
||
151 | 'endpoint' => EndpointHelpers::getAdminGraphQLEndpoint(), |
||
152 | ], |
||
153 | $scriptSettings |
||
154 | ) |
||
155 | ); |
||
156 | } else { |
||
157 | // Print the HTML from the Client |
||
158 | $htmlContent = $this->getGraphiQLWithExplorerClientHTML(); |
||
159 | // Extract the JS/CSS assets, from either the <head> or the <head> |
||
160 | $matches = []; |
||
161 | preg_match_all('/<link[^>]+href="([^">]+)"/s', $htmlContent, $matches); |
||
162 | $cssFileURLs = $matches[1]; |
||
163 | foreach ($cssFileURLs as $index => $cssFileURL) { |
||
164 | \wp_enqueue_style( |
||
165 | 'graphql-api-graphiql-with-explorer-' . $index, |
||
166 | $cssFileURL, |
||
167 | array(), |
||
168 | \GRAPHQL_API_VERSION |
||
169 | ); |
||
170 | } |
||
171 | preg_match_all('/<script[^>]+src="([^">]+)"/s', $htmlContent, $matches); |
||
172 | $jsFileURLs = $matches[1]; |
||
173 | foreach ($jsFileURLs as $index => $jsFileURL) { |
||
174 | \wp_enqueue_script( |
||
175 | 'graphql-api-graphiql-with-explorer-' . $index, |
||
176 | $jsFileURL, |
||
177 | array(), |
||
178 | \GRAPHQL_API_VERSION, |
||
179 | true |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | // Override styles for the admin, so load last |
||
184 | \wp_enqueue_style( |
||
185 | 'graphql-api-graphiql-with-explorer-client', |
||
186 | \GRAPHQL_API_URL . 'assets/css/graphiql-with-explorer-client.css', |
||
187 | array(), |
||
188 | \GRAPHQL_API_VERSION |
||
189 | ); |
||
190 | |||
191 | // Load data into the script. Because no script is enqueued since it is |
||
192 | // in the body, then localize it to React |
||
193 | \wp_localize_script( |
||
194 | 'graphql-api-graphiql-with-explorer-0', |
||
195 | 'graphiQLWithExplorerClientForWP', |
||
196 | $scriptSettings |
||
197 | ); |
||
258 |