Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 10 | class Tools extends Base |
||
| 11 | { |
||
| 12 | const TK_OBTEM = 'O'; |
||
| 13 | const TK_INICIA = 'I'; |
||
| 14 | const TK_FINALIZA = 'F'; |
||
| 15 | const TK_CANCELA = 'C'; |
||
| 16 | const TK_STATUS = 'S'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Endereços principais dos webservices |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $url = [ |
||
| 23 | '1' => 'https://esfingews.tce.sc.gov.br', |
||
| 24 | '2' => 'https://desenv2.tce.sc.gov.br:7443', |
||
| 25 | ]; |
||
| 26 | /** |
||
| 27 | * Competência bimestral no formato: AAAABB, onde: |
||
| 28 | * AAAA = ano a ser enviado os dados |
||
| 29 | * BB = bimestre de 01 até 06 |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $competencia; |
||
| 33 | /** |
||
| 34 | * Token de segurança e queue |
||
| 35 | * hash com 36 caracteres aleatórios |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $tokenid; |
||
| 39 | /** |
||
| 40 | * Flag iniciar tranferencia |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $flagIniciar = false; |
||
| 44 | /** |
||
| 45 | * Datahora da ultima solicitação da situação do token |
||
| 46 | * @var timestramp |
||
| 47 | */ |
||
| 48 | protected $tsLastSitToken; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Construtor |
||
| 52 | * @param string $configJson |
||
| 53 | */ |
||
| 54 | 6 | public function __construct($configJson = '') |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Define o período de competência das informações |
||
| 61 | * formado AAAABB sendo BB o bimestre de 01 até 06 |
||
| 62 | * @param string $valor |
||
|
|
|||
| 63 | */ |
||
| 64 | 9 | public function setCompetencia($aaaabb) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Retorna o período de competência informado |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 3 | public function getCompetencia() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Operações com token |
||
| 87 | * O método pode ser: |
||
| 88 | * C - cancela a transferencia |
||
| 89 | * F - finaliza a transferencia |
||
| 90 | * I - inica a transferencia |
||
| 91 | * O - Obtem o token para realizar as operações |
||
| 92 | * S - Verifica a situação do token |
||
| 93 | * @param string $method |
||
| 94 | */ |
||
| 95 | public function token($method = self::TK_OBTEM) |
||
| 96 | { |
||
| 97 | $uri = $this->url[$this->tpAmb].'/esfinge/services/tokenWS'; |
||
| 98 | $namespace = 'http://token.ws.tce.sc.gov.br/'; |
||
| 99 | |||
| 100 | switch ($method) { |
||
| 101 | View Code Duplication | case self::TK_CANCELA: |
|
| 102 | //cancela as operações realizadas com um determinado token |
||
| 103 | //se OK o token é removido e todas as operações com ele |
||
| 104 | //realizadas são descartadas |
||
| 105 | if ($this->flagIniciar === false) { |
||
| 106 | //não está iniciada a tranferencia então não dá para cancelar |
||
| 107 | throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser cancelada'); |
||
| 108 | } |
||
| 109 | $met = 'cancelarTransferencia'; |
||
| 110 | $body = "<svc:cancelarTransferencia>" |
||
| 111 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 112 | . "</svc:cancelarTransferencia>"; |
||
| 113 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 114 | if ($resp['bStat'] && $resp['status'] == 'OK') { |
||
| 115 | //cancelamento aceito |
||
| 116 | $this->tokenid = ''; |
||
| 117 | $this->flagIniciar = false; |
||
| 118 | } |
||
| 119 | break; |
||
| 120 | View Code Duplication | case self::TK_FINALIZA: |
|
| 121 | //Ao final da transferência caso queria confirmar todos os elementos inseridos |
||
| 122 | //(que não retornaram erro) nesta sessão, ou seja todos os elementos ligados a |
||
| 123 | //determinado token passado para o serviço. Uma vez executado este serviço |
||
| 124 | //o token atual será descartado. |
||
| 125 | if ($this->flagIniciar === false) { |
||
| 126 | //não está iniciada a tranferencia então não dá para finalizar |
||
| 127 | throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser finalizada'); |
||
| 128 | } |
||
| 129 | $met = 'finalizarTransferencia'; |
||
| 130 | $body = "<svc:finalizarTransferencia>" |
||
| 131 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 132 | . "</svc:finalizarTransferencia>"; |
||
| 133 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 134 | if ($resp['bStat'] && $resp['status'] == 'OK') { |
||
| 135 | //finalização aceita |
||
| 136 | $this->tokenid = ''; |
||
| 137 | $this->flagIniciar = false; |
||
| 138 | } |
||
| 139 | break; |
||
| 140 | case self::TK_INICIA: |
||
| 141 | //Antes de iniciar a transferência dos dados propriamente dita, será necessário executar |
||
| 142 | //o serviço iniciarTransferencia |
||
| 143 | if ($this->tokenid == '') { |
||
| 144 | //não é possivel iniciar sem um token valido |
||
| 145 | throw new RuntimeException('Não é possivel iniciar a tranferência sem um token valido'); |
||
| 146 | //$this->token(self::TK_O); |
||
| 147 | } |
||
| 148 | if ($this->flagIniciar === true) { |
||
| 149 | $resp = [ |
||
| 150 | 'bStat' => true, |
||
| 151 | 'message' => 'Início de transferência liberado', |
||
| 152 | 'status' => 'OK' |
||
| 153 | ]; |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | $met = 'iniciarTransferencia'; |
||
| 157 | $body = "<svc:iniciarTransferencia>" |
||
| 158 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 159 | . "</svc:iniciarTransferencia>"; |
||
| 160 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 161 | if ($resp['bStat'] && $resp['status'] == 'OK') { |
||
| 162 | $this->flagIniciar = true; |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | case self::TK_OBTEM: |
||
| 166 | //Retorna um token para a unidade gestora poder usar o serviço do TCE. |
||
| 167 | //Permite somente um token por unidade gestora. |
||
| 168 | View Code Duplication | if ($this->tokenid != '') { |
|
| 169 | $resp = [ |
||
| 170 | 'bStat' => true, |
||
| 171 | 'message' => 'Token criado com sucesso', |
||
| 172 | 'status' => 'OK', |
||
| 173 | 'chaveToken' => $this->tokenid, |
||
| 174 | 'posicao' => 2, |
||
| 175 | 'situacao' => 'Pronto para envio ou consulta' |
||
| 176 | ]; |
||
| 177 | break; |
||
| 178 | } |
||
| 179 | $met = 'obterToken'; |
||
| 180 | $body = "<svc:obterToken>" |
||
| 181 | . "<codigoUg>$this->codigoUnidadeGestora</codigoUg>" |
||
| 182 | . "</svc:obterToken>"; |
||
| 183 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 184 | if ($resp['bStat'] |
||
| 185 | && $resp['chaveToken'] != '' |
||
| 186 | && $resp['status'] == 'OK' |
||
| 187 | ) { |
||
| 188 | $this->tokenid = $resp['chaveToken']; |
||
| 189 | } |
||
| 190 | break; |
||
| 191 | case self::TK_STATUS: |
||
| 192 | //Retorna a situação do token passado como parâmetro. Para evitar solicitações |
||
| 193 | //indefinidas a este serviço o sistema punirá com a remoção do token da fila |
||
| 194 | //sempre que for feita duas chamadas seguidas do serviço obterSituacaoToken |
||
| 195 | //em menos de cinco segundos. |
||
| 196 | if ($this->tokenid == '') { |
||
| 197 | //não é possivel verificar o token |
||
| 198 | throw new RuntimeException('Não existe um token aberto.'); |
||
| 199 | } |
||
| 200 | //se tentativa de verificação ocorrer em menos de 2 seg |
||
| 201 | //retorna como OK |
||
| 202 | View Code Duplication | if ((time()-$this->tsLastSitToken) <= 2) { |
|
| 203 | $resp = [ |
||
| 204 | 'bStat' => true, |
||
| 205 | 'message' => 'Situação token obtida com sucesso', |
||
| 206 | 'status' => 'OK', |
||
| 207 | 'posicao' => 1, |
||
| 208 | 'situacao' => 'Pronto para envio ou consulta' |
||
| 209 | ]; |
||
| 210 | break; |
||
| 211 | } |
||
| 212 | $met = 'obterSituacaoToken'; |
||
| 213 | $body = "<svc:obterSituacaoToken>" |
||
| 214 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 215 | . "</svc:obterSituacaoToken>"; |
||
| 216 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 217 | $this->tsLastSitToken = time(); |
||
| 218 | break; |
||
| 219 | } |
||
| 220 | return $resp; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Inicia o processo de tranferência de dados |
||
| 225 | * @param array $data |
||
| 226 | * @throws InvalidArgumentException |
||
| 227 | * @throws RuntimeException |
||
| 228 | */ |
||
| 229 | protected function obterTokenIniciarTransferencia($data = array()) |
||
| 230 | { |
||
| 231 | if (empty($data)) { |
||
| 232 | throw new InvalidArgumentException('Não foram passados dados para o método'); |
||
| 233 | } |
||
| 234 | $this->token(self::TK_OBTEM); |
||
| 235 | $this->token(self::TK_INICIA); |
||
| 236 | if ($this->tokenid == '' || $this->flagIniciar === false) { |
||
| 237 | throw new RuntimeException("Falha token:$this->tokenid , Iniciar: $this->flagIniciar"); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Servidor |
||
| 243 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 244 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 245 | * @param array $data |
||
| 246 | * @param string $method |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | View Code Duplication | public function servidor($data = array(), $method = 'L') |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Situação Servidor Folha Pagamento |
||
| 262 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 263 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 264 | * @param array $data |
||
| 265 | * @param string $method |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function situacaoServidorFolhaPagamento($data = array(), $method = 'L') |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Componentes Folha Pagamento |
||
| 280 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 281 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 282 | * @param array $data |
||
| 283 | * @param string $method |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function componentesFolhaPagamento($data = array(), $method = 'L') |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Folha Pagamento |
||
| 298 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 299 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 300 | * @param array $data |
||
| 301 | * @param string $method |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function folhaPagamento($data = array(), $method = 'L') |
|
| 313 | } |
||
| 314 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.