Conditions | 14 |
Paths | 126 |
Total Lines | 98 |
Lines | 25 |
Ratio | 25.51 % |
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 |
||
183 | private function enviarRTC($email, $empresa, $dte, $token, $retry = null) |
||
184 | { |
||
185 | // definir datos que se usarán en el envío |
||
186 | list($rutCompany, $dvCompany) = explode('-', str_replace('.', '', $empresa)); |
||
187 | if (strpos($dte, '<?xml') === false) { |
||
188 | $dte = '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . $dte; |
||
189 | } |
||
190 | View Code Duplication | do { |
|
191 | $file = sys_get_temp_dir() . '/aec_' . md5(microtime() . $token . $dte) . '.xml'; |
||
192 | } while (file_exists($file)); |
||
193 | file_put_contents($file, $dte); |
||
194 | $data = [ |
||
195 | 'emailNotif' => $email, |
||
196 | 'rutCompany' => $rutCompany, |
||
197 | 'dvCompany' => $dvCompany, |
||
198 | 'archivo' => curl_file_create( |
||
199 | $file, |
||
200 | 'application/xml', |
||
201 | basename($file) |
||
202 | ), |
||
203 | ]; |
||
204 | // crear sesión curl con sus opciones |
||
205 | $curl = curl_init(); |
||
206 | $header = [ |
||
207 | 'User-Agent: Mozilla/4.0 (compatible; PROG 1.0; LibreDTE)', |
||
208 | 'Referer: https://libredte.cl', |
||
209 | 'Cookie: TOKEN='.$token, |
||
210 | ]; |
||
211 | $url = 'https://'.\sasco\LibreDTE\Sii::getServidor().'.sii.cl/cgi_rtc/RTC/RTCAnotEnvio.cgi'; |
||
212 | curl_setopt($curl, CURLOPT_POST, true); |
||
213 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
214 | curl_setopt($curl, CURLOPT_HTTPHEADER, $header); |
||
215 | curl_setopt($curl, CURLOPT_URL, $url); |
||
216 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
217 | // si no se debe verificar el SSL se asigna opción a curl, además si |
||
218 | // se está en el ambiente de producción y no se verifica SSL se |
||
219 | // generará una entrada en el log |
||
220 | View Code Duplication | if (!\sasco\LibreDTE\Sii::getVerificarSSL()) { |
|
221 | if (\sasco\LibreDTE\Sii::getAmbiente()==\sasco\LibreDTE\Sii::PRODUCCION) { |
||
222 | \sasco\LibreDTE\Log::write( |
||
223 | \sasco\LibreDTE\Estado::ENVIO_SSL_SIN_VERIFICAR, |
||
224 | \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_SSL_SIN_VERIFICAR), |
||
225 | LOG_WARNING |
||
226 | ); |
||
227 | } |
||
228 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
||
229 | } |
||
230 | // enviar XML al SII |
||
231 | View Code Duplication | for ($i = 0; $i < $retry; $i++) { |
|
232 | $response = curl_exec($curl); |
||
233 | if ($response and $response != 'Error 500') |
||
234 | break; |
||
235 | } |
||
236 | unlink($file); |
||
237 | // verificar respuesta del envío y entregar error en caso que haya uno |
||
238 | View Code Duplication | if (!$response or $response == 'Error 500') { |
|
239 | if (!$response) |
||
240 | \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::ENVIO_ERROR_CURL, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_ERROR_CURL, curl_error($curl))); |
||
241 | if ($response == 'Error 500') |
||
242 | \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::ENVIO_ERROR_500, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_ERROR_500)); |
||
243 | return false; |
||
244 | } |
||
245 | // cerrar sesión curl |
||
246 | curl_close($curl); |
||
247 | // crear XML con la respuesta y retornar |
||
248 | try { |
||
249 | $xml = new \SimpleXMLElement($response, LIBXML_COMPACT); |
||
250 | } catch (Exception $e) { |
||
251 | \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::ENVIO_ERROR_XML, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_ERROR_XML, $e->getMessage())); |
||
252 | return false; |
||
253 | } |
||
254 | /* |
||
255 | * 0 Envío recibido OK. |
||
256 | * 1 Rut usuario autenticado no tiene permiso para enviar en empresa Cedente. |
||
257 | * 2 Error en tamaño del archivo enviado. |
||
258 | * 4 Faltan parámetros de entrada. |
||
259 | * 5 Error de autenticación, TOKEN inválido, no existe o está expirado. |
||
260 | * 6 Empresa no es DTE. |
||
261 | * 9 Error Interno. |
||
262 | * 10 Error Interno |
||
263 | */ |
||
264 | $error = [ |
||
265 | 1 => 'Rut usuario autenticado no tiene permiso para enviar en empresa Cedente', |
||
266 | 2 => 'Error en tamaño del archivo enviado', |
||
267 | 4 => 'Faltan parámetros de entrada', |
||
268 | 5 => 'Error de autenticación, TOKEN inválido, no existe o está expirado', |
||
269 | 6 => 'Empresa no es DTE', |
||
270 | 9 => 'Error Interno', |
||
271 | 10 => 'Error Interno' |
||
272 | ]; |
||
273 | if ($xml->STATUS != 0) { |
||
274 | \sasco\LibreDTE\Log::write( |
||
275 | $xml->STATUS, |
||
276 | $error[(int)$xml->STATUS] |
||
277 | ); |
||
278 | } |
||
279 | return $xml; |
||
280 | } |
||
281 | |||
301 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.