Conditions | 16 |
Paths | 1824 |
Total Lines | 111 |
Code Lines | 73 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
95 | public function formSucceeded($form, $values) |
||
96 | { |
||
97 | $url = $values['api_url']; |
||
98 | |||
99 | $token = false; |
||
100 | if (isset($values['token'])) { |
||
101 | $token = $values['token']; |
||
102 | unset($values['token']); |
||
103 | } |
||
104 | |||
105 | $postFields = []; |
||
106 | $getFields = []; |
||
107 | |||
108 | if (isset($values['token_csfr'])) { |
||
109 | $postFields[] = "token=" . urlencode($values['token_csfr']); |
||
110 | unset($values['token_csfr']); |
||
111 | } |
||
112 | |||
113 | $params = $this->handler->params(); |
||
114 | |||
115 | foreach ($values as $key => $value) { |
||
116 | if (strstr($key, '___') !== false) { |
||
117 | $parts = explode('___', $key); |
||
118 | $key = $parts[0]; |
||
119 | } |
||
120 | foreach ($params as $param) { |
||
121 | if ($param->getKey() == $key) { |
||
122 | if (!$value) { |
||
123 | continue; |
||
124 | } |
||
125 | if ($param->isMulti()) { |
||
126 | $valueKey = ''; |
||
127 | if (strstr($value, '=') !== false) { |
||
128 | $parts = explode('=', $value); |
||
129 | $valueKey = $parts[0]; |
||
130 | $value = $parts[1]; |
||
131 | } |
||
132 | $valueData = $key . "[$valueKey]=$value"; |
||
133 | } else { |
||
134 | $valueData = "$key=$value"; |
||
135 | } |
||
136 | |||
137 | if ($param->getType() == InputParam::TYPE_POST) { |
||
138 | $postFields[] = $valueData; |
||
139 | } else { |
||
140 | $getFields[] = $valueData; |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | if (count($getFields)) { |
||
147 | $url = $url . '?' . implode('&', $getFields); |
||
148 | } |
||
149 | |||
150 | |||
151 | Debugger::timer(); |
||
152 | |||
153 | $result = 'Requesting url: ' . $url . "\n"; |
||
154 | $result .= "POST Params:\n\t"; |
||
155 | $result .= implode("\n\t", $postFields); |
||
156 | $result .= "\n"; |
||
157 | $curl = curl_init(); |
||
158 | curl_setopt($curl, CURLOPT_URL, $url); |
||
159 | curl_setopt($curl, CURLOPT_NOBODY, false); |
||
160 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
||
161 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
162 | curl_setopt($curl, CURLOPT_VERBOSE, false); |
||
163 | curl_setopt($curl, CURLOPT_TIMEOUT, 10); |
||
164 | if (count($postFields)) { |
||
165 | curl_setopt($curl, CURLOPT_POST, 1); |
||
166 | curl_setopt($curl, CURLOPT_POSTFIELDS, implode('&', $postFields)); |
||
167 | } |
||
168 | |||
169 | curl_setopt($curl, CURLOPT_TIMEOUT, 30); |
||
170 | if ($token) { |
||
171 | $headers = array('Authorization: Bearer ' . $token); |
||
172 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
173 | $result .= implode("\n", $headers); |
||
174 | } |
||
175 | |||
176 | |||
177 | $result .= "\n\n--------------------------------------------------------\n\n"; |
||
178 | |||
179 | $responseBody = curl_exec($curl); |
||
180 | |||
181 | $result .= "\n"; |
||
182 | |||
183 | $elapsed = intval(Debugger::timer() * 1000); |
||
184 | $result .= "Took: {$elapsed}ms\n"; |
||
185 | |||
186 | $curlErrorNumber = curl_errno($curl); |
||
187 | $curlError = curl_error($curl); |
||
188 | if ($curlErrorNumber > 0) { |
||
189 | $result .= "HTTP Error: " . $curlError . "\n"; |
||
190 | } else { |
||
191 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
192 | |||
193 | $result .= "HTTP code: $httpcode\n\n"; |
||
194 | |||
195 | $body = $responseBody; |
||
196 | $decoded = json_decode($body); |
||
197 | if ($decoded) { |
||
198 | $body = json_encode($decoded, JSON_PRETTY_PRINT); |
||
199 | } |
||
200 | |||
201 | $result .= "Result:\n\n{$body}\n\n"; |
||
202 | } |
||
203 | |||
204 | $this->template->response = $result; |
||
205 | } |
||
206 | } |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.