Conditions | 21 |
Paths | 1201 |
Total Lines | 153 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
80 | public function call($name, $arguments, $expectedClass = null) |
||
81 | { |
||
82 | if (! isset($this->methods[$name])) { |
||
83 | $this->client->getLogger()->error( |
||
84 | 'Service method unknown', |
||
85 | array( |
||
86 | 'service' => $this->serviceName, |
||
87 | 'resource' => $this->resourceName, |
||
88 | 'method' => $name |
||
89 | ) |
||
90 | ); |
||
91 | |||
92 | throw new Google_Exception( |
||
93 | "Unknown function: " . |
||
94 | "{$this->serviceName}->{$this->resourceName}->{$name}()" |
||
95 | ); |
||
96 | } |
||
97 | $method = $this->methods[$name]; |
||
98 | $parameters = $arguments[0]; |
||
99 | |||
100 | // postBody is a special case since it's not defined in the discovery |
||
101 | // document as parameter, but we abuse the param entry for storing it. |
||
102 | $postBody = null; |
||
103 | if (isset($parameters['postBody'])) { |
||
104 | if ($parameters['postBody'] instanceof Google_Model) { |
||
105 | // In the cases the post body is an existing object, we want |
||
106 | // to use the smart method to create a simple object for |
||
107 | // for JSONification. |
||
108 | $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); |
||
109 | } else if (is_object($parameters['postBody'])) { |
||
110 | // If the post body is another kind of object, we will try and |
||
111 | // wrangle it into a sensible format. |
||
112 | $parameters['postBody'] = |
||
113 | $this->convertToArrayAndStripNulls($parameters['postBody']); |
||
114 | } |
||
115 | $postBody = (array) $parameters['postBody']; |
||
116 | unset($parameters['postBody']); |
||
117 | } |
||
118 | |||
119 | // TODO: optParams here probably should have been |
||
120 | // handled already - this may well be redundant code. |
||
121 | if (isset($parameters['optParams'])) { |
||
122 | $optParams = $parameters['optParams']; |
||
123 | unset($parameters['optParams']); |
||
124 | $parameters = array_merge($parameters, $optParams); |
||
125 | } |
||
126 | |||
127 | if (!isset($method['parameters'])) { |
||
128 | $method['parameters'] = array(); |
||
129 | } |
||
130 | |||
131 | $method['parameters'] = array_merge( |
||
132 | $this->stackParameters, |
||
133 | $method['parameters'] |
||
134 | ); |
||
135 | |||
136 | foreach ($parameters as $key => $val) { |
||
137 | if ($key != 'postBody' && ! isset($method['parameters'][$key])) { |
||
138 | $this->client->getLogger()->error( |
||
139 | 'Service parameter unknown', |
||
140 | array( |
||
141 | 'service' => $this->serviceName, |
||
142 | 'resource' => $this->resourceName, |
||
143 | 'method' => $name, |
||
144 | 'parameter' => $key |
||
145 | ) |
||
146 | ); |
||
147 | throw new Google_Exception("($name) unknown parameter: '$key'"); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | foreach ($method['parameters'] as $paramName => $paramSpec) { |
||
152 | if (isset($paramSpec['required']) && |
||
153 | $paramSpec['required'] && |
||
154 | ! isset($parameters[$paramName]) |
||
155 | ) { |
||
156 | $this->client->getLogger()->error( |
||
157 | 'Service parameter missing', |
||
158 | array( |
||
159 | 'service' => $this->serviceName, |
||
160 | 'resource' => $this->resourceName, |
||
161 | 'method' => $name, |
||
162 | 'parameter' => $paramName |
||
163 | ) |
||
164 | ); |
||
165 | throw new Google_Exception("($name) missing required param: '$paramName'"); |
||
166 | } |
||
167 | if (isset($parameters[$paramName])) { |
||
168 | $value = $parameters[$paramName]; |
||
169 | $parameters[$paramName] = $paramSpec; |
||
170 | $parameters[$paramName]['value'] = $value; |
||
171 | unset($parameters[$paramName]['required']); |
||
172 | } else { |
||
173 | // Ensure we don't pass nulls. |
||
174 | unset($parameters[$paramName]); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | $this->client->getLogger()->info( |
||
179 | 'Service Call', |
||
180 | array( |
||
181 | 'service' => $this->serviceName, |
||
182 | 'resource' => $this->resourceName, |
||
183 | 'method' => $name, |
||
184 | 'arguments' => $parameters, |
||
185 | ) |
||
186 | ); |
||
187 | |||
188 | // build the service uri |
||
189 | $url = $this->createRequestUri( |
||
190 | $method['path'], |
||
191 | $parameters |
||
192 | ); |
||
193 | |||
194 | // NOTE: because we're creating the request by hand, |
||
195 | // and because the service has a rootUrl property |
||
196 | // the "base_uri" of the Http Client is not accounted for |
||
197 | $request = new Request( |
||
198 | $method['httpMethod'], |
||
199 | $url, |
||
200 | ['content-type' => 'application/json'], |
||
201 | $postBody ? json_encode($postBody) : '' |
||
202 | ); |
||
203 | |||
204 | // support uploads |
||
205 | if (isset($parameters['data'])) { |
||
206 | $mimeType = isset($parameters['mimeType']) |
||
207 | ? $parameters['mimeType']['value'] |
||
208 | : 'application/octet-stream'; |
||
209 | $data = $parameters['data']['value']; |
||
210 | $upload = new Google_Http_MediaFileUpload($this->client, $request, $mimeType, $data); |
||
211 | |||
212 | // pull down the modified request |
||
213 | $request = $upload->getRequest(); |
||
214 | } |
||
215 | |||
216 | // if this is a media type, we will return the raw response |
||
217 | // rather than using an expected class |
||
218 | if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { |
||
219 | $expectedClass = null; |
||
220 | } |
||
221 | |||
222 | // if the client is marked for deferring, rather than |
||
223 | // execute the request, return the response |
||
224 | if ($this->client->shouldDefer()) { |
||
225 | // @TODO find a better way to do this |
||
226 | $request = $request |
||
227 | ->withHeader('X-Php-Expected-Class', $expectedClass); |
||
228 | |||
229 | return $request; |
||
230 | } |
||
231 | |||
232 | return $this->client->execute($request, $expectedClass); |
||
233 | } |
||
303 |