| Conditions | 13 |
| Paths | 43 |
| Total Lines | 66 |
| 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 |
||
| 39 | protected function standarize(\stdClass $std) |
||
| 40 | { |
||
| 41 | if (empty($this->parameters)) { |
||
| 42 | throw new Exception('Parametros não estabelecidos na classe'); |
||
| 43 | } |
||
| 44 | $errors = []; |
||
| 45 | //passa todos as variáveis do stdClass para minusculo |
||
| 46 | $arr = array_change_key_case(get_object_vars($std), CASE_LOWER); |
||
| 47 | $std = json_decode(json_encode($arr)); |
||
| 48 | //paga as chaves dos parametros e passa para minusculo |
||
| 49 | $stdParam = json_decode(json_encode($this->parameters)); |
||
| 50 | $this->parameters = array_change_key_case(get_object_vars($stdParam), CASE_LOWER); |
||
| 51 | $paramKeys = array_keys($this->parameters); |
||
| 52 | //passa os paramatros com as chaves modificadas para um stdClass |
||
| 53 | if (!$json = json_encode($this->parameters)) { |
||
| 54 | throw new \RuntimeException("Falta definir os parametros ou existe erro no array"); |
||
| 55 | } |
||
| 56 | $stdParam = json_decode($json); |
||
| 57 | if ($stdParam === null) { |
||
| 58 | throw new \RuntimeException("Houve uma falha na converção para stdClass"); |
||
| 59 | } |
||
| 60 | //verifica se foram passados os dados obrigatórios |
||
| 61 | foreach ($std as $key => $value) { |
||
| 62 | if (!isset($stdParam->$key)) { |
||
| 63 | //ignore non defined params |
||
| 64 | continue; |
||
| 65 | } |
||
| 66 | if ($stdParam->$key->required && $std->$key === null) { |
||
| 67 | $errors[] = "$key é requerido."; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | $newstd = new \stdClass(); |
||
| 71 | foreach ($paramKeys as $key) { |
||
| 72 | if (!key_exists($key, $arr)) { |
||
| 73 | $newstd->$key = null; |
||
| 74 | } else { |
||
| 75 | if ($std->$key === null) { |
||
| 76 | $newstd->$key = null; |
||
| 77 | continue; |
||
| 78 | } |
||
| 79 | //se o valor para o parametro foi passado, então validar |
||
| 80 | $resp = $this->isFieldInError( |
||
| 81 | $std->$key, |
||
| 82 | $stdParam->$key, |
||
| 83 | strtoupper($key), |
||
| 84 | $this->reg, |
||
| 85 | $stdParam->$key->required |
||
| 86 | ); |
||
| 87 | if ($resp) { |
||
| 88 | $errors[] = $resp; |
||
| 89 | } |
||
| 90 | //e formatar o dado passado |
||
| 91 | $formated = $this->formater( |
||
| 92 | $std->$key, |
||
| 93 | $stdParam->$key->format, |
||
| 94 | strtoupper($key) |
||
| 95 | ); |
||
| 96 | $newstd->$key = $formated; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | //se algum erro for detectado disparar um Exception |
||
| 100 | if (!empty($errors)) { |
||
| 101 | throw new \InvalidArgumentException(implode("\n", $errors)); |
||
| 102 | } |
||
| 103 | return $newstd; |
||
| 104 | } |
||
| 105 | |||
| 247 |