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 = '') |
|
| 55 | { |
||
| 56 | 6 | parent::__construct($configJson); |
|
| 57 | } |
||
| 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 | public function setCompetencia($aaaabb) |
||
| 65 | { |
||
| 66 | if (!is_numeric($aaaabb)) { |
||
| 67 | throw new InvalidArgumentException('O periodo de competência é uma informação APENAS numérica.'); |
||
| 68 | } |
||
| 69 | $bm = intval(substr($aaaabb, -2)); |
||
| 70 | if ($bm > 6 || $bm <= 0) { |
||
| 71 | throw new InvalidArgumentException('O bimestre pode ser de 01 até 06 APENAS.'); |
||
| 72 | } |
||
| 73 | $this->competencia = $aaaabb; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Retorna o período de competência informado |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getCompetencia() |
||
| 81 | { |
||
| 82 | return $this->competencia; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Retorna no token ativo |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function getToken() |
||
| 90 | { |
||
| 91 | return $this->tokenid; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Retorna o status de inicio de transferencia |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function getTransferencia() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Operações com token |
||
| 105 | * O método pode ser: |
||
| 106 | * C - cancela a transferencia |
||
| 107 | * F - finaliza a transferencia |
||
| 108 | * I - inica a transferencia |
||
| 109 | * O - Obtem o token para realizar as operações |
||
| 110 | * S - Verifica a situação do token |
||
| 111 | * @param string $method |
||
| 112 | */ |
||
| 113 | public function token($method = self::TK_OBTEM) |
||
| 114 | { |
||
| 115 | $uri = $this->url[$this->tpAmb].'/esfinge/services/tokenWS'; |
||
| 116 | $namespace = 'http://token.ws.tce.sc.gov.br/'; |
||
| 117 | |||
| 118 | switch ($method) { |
||
| 119 | View Code Duplication | case self::TK_CANCELA: |
|
| 120 | //cancela as operações realizadas com um determinado token |
||
| 121 | //se OK o token é removido e todas as operações com ele |
||
| 122 | //realizadas são descartadas |
||
| 123 | if ($this->flagIniciar === false) { |
||
| 124 | //não está iniciada a tranferencia então não dá para cancelar |
||
| 125 | throw new RuntimeException('A transferência não foi iniciada, então não pode ser cancelada'); |
||
| 126 | } |
||
| 127 | $met = 'cancelarTransferencia'; |
||
| 128 | $body = "<svc:cancelarTransferencia>" |
||
| 129 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 130 | . "</svc:cancelarTransferencia>"; |
||
| 131 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 132 | if ($resp['bStat'] && $resp['status'] == 'OK') { |
||
| 133 | //cancelamento aceito |
||
| 134 | $this->tokenid = ''; |
||
| 135 | $this->flagIniciar = false; |
||
| 136 | } |
||
| 137 | break; |
||
| 138 | View Code Duplication | case self::TK_FINALIZA: |
|
| 139 | //Ao final da transferência caso queria confirmar todos os elementos inseridos |
||
| 140 | //(que não retornaram erro) nesta sessão, ou seja todos os elementos ligados a |
||
| 141 | //determinado token passado para o serviço. Uma vez executado este serviço |
||
| 142 | //o token atual será descartado. |
||
| 143 | if ($this->flagIniciar === false) { |
||
| 144 | //não está iniciada a tranferencia então não dá para finalizar |
||
| 145 | throw new RuntimeException('A transferência não foi iniciada, então não pode ser finalizada'); |
||
| 146 | } |
||
| 147 | $met = 'finalizarTransferencia'; |
||
| 148 | $body = "<svc:finalizarTransferencia>" |
||
| 149 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 150 | . "</svc:finalizarTransferencia>"; |
||
| 151 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 152 | if ($resp['bStat'] && $resp['status'] == 'OK') { |
||
| 153 | //finalização aceita |
||
| 154 | $this->tokenid = ''; |
||
| 155 | $this->flagIniciar = false; |
||
| 156 | } |
||
| 157 | break; |
||
| 158 | case self::TK_INICIA: |
||
| 159 | //Antes de iniciar a transferência dos dados propriamente dita, será necessário executar |
||
| 160 | //o serviço iniciarTransferencia |
||
| 161 | if ($this->tokenid == '') { |
||
| 162 | //não é possivel iniciar sem um token valido |
||
| 163 | throw new RuntimeException('Não é possivel iniciar a transferência sem um token valido'); |
||
| 164 | //$this->token(self::TK_O); |
||
| 165 | } |
||
| 166 | if ($this->flagIniciar === true) { |
||
| 167 | $resp = [ |
||
| 168 | 'bStat' => true, |
||
| 169 | 'message' => 'Início de transferência liberado', |
||
| 170 | 'status' => 'OK' |
||
| 171 | ]; |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | $met = 'iniciarTransferencia'; |
||
| 175 | $body = "<svc:iniciarTransferencia>" |
||
| 176 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 177 | . "</svc:iniciarTransferencia>"; |
||
| 178 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 179 | if ($resp['bStat'] && $resp['status'] == 'OK') { |
||
| 180 | $this->flagIniciar = true; |
||
| 181 | } |
||
| 182 | break; |
||
| 183 | case self::TK_OBTEM: |
||
| 184 | //Retorna um token para a unidade gestora poder usar o serviço do TCE. |
||
| 185 | //Permite somente um token por unidade gestora. |
||
| 186 | View Code Duplication | if ($this->tokenid != '') { |
|
| 187 | $resp = [ |
||
| 188 | 'bStat' => true, |
||
| 189 | 'message' => 'Token criado com sucesso', |
||
| 190 | 'status' => 'OK', |
||
| 191 | 'chaveToken' => $this->tokenid, |
||
| 192 | 'posicao' => 2, |
||
| 193 | 'situacao' => 'Pronto para envio ou consulta' |
||
| 194 | ]; |
||
| 195 | break; |
||
| 196 | } |
||
| 197 | $met = 'obterToken'; |
||
| 198 | $body = "<svc:obterToken>" |
||
| 199 | . "<codigoUg>$this->codigoUnidadeGestora</codigoUg>" |
||
| 200 | . "</svc:obterToken>"; |
||
| 201 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 202 | if ($resp['bStat'] |
||
| 203 | && $resp['chaveToken'] != '' |
||
| 204 | && $resp['status'] == 'OK' |
||
| 205 | ) { |
||
| 206 | $this->tokenid = $resp['chaveToken']; |
||
| 207 | } |
||
| 208 | break; |
||
| 209 | case self::TK_STATUS: |
||
| 210 | //Retorna a situação do token passado como parâmetro. Para evitar solicitações |
||
| 211 | //indefinidas a este serviço o sistema punirá com a remoção do token da fila |
||
| 212 | //sempre que for feita duas chamadas seguidas do serviço obterSituacaoToken |
||
| 213 | //em menos de cinco segundos. |
||
| 214 | if ($this->tokenid == '') { |
||
| 215 | //não é possivel verificar o token |
||
| 216 | throw new RuntimeException('Não existe um token aberto.'); |
||
| 217 | } |
||
| 218 | //se tentativa de verificação ocorrer em menos de 2 seg |
||
| 219 | //retorna como OK |
||
| 220 | View Code Duplication | if ((time()-$this->tsLastSitToken) <= 2) { |
|
| 221 | $resp = [ |
||
| 222 | 'bStat' => true, |
||
| 223 | 'message' => 'Situação token obtida com sucesso', |
||
| 224 | 'status' => 'OK', |
||
| 225 | 'posicao' => 1, |
||
| 226 | 'situacao' => 'Pronto para envio ou consulta' |
||
| 227 | ]; |
||
| 228 | break; |
||
| 229 | } |
||
| 230 | $met = 'obterSituacaoToken'; |
||
| 231 | $body = "<svc:obterSituacaoToken>" |
||
| 232 | . "<chaveToken>$this->tokenid</chaveToken>" |
||
| 233 | . "</svc:obterSituacaoToken>"; |
||
| 234 | $resp = $this->envia($uri, $namespace, $body, '', $met); |
||
| 235 | $this->tsLastSitToken = time(); |
||
| 236 | break; |
||
| 237 | } |
||
| 238 | return $resp; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Inicia o processo de tranferência de dados |
||
| 243 | * @param array $data |
||
| 244 | * @throws InvalidArgumentException |
||
| 245 | * @throws RuntimeException |
||
| 246 | */ |
||
| 247 | protected function obterTokenIniciarTransferencia($data = array(), $method = 'L') |
||
| 248 | { |
||
| 249 | if (empty($data)) { |
||
| 250 | throw new InvalidArgumentException('Não foram passados dados para o método'); |
||
| 251 | } |
||
| 252 | $this->token(self::TK_OBTEM); |
||
| 253 | if ($method == 'E') { |
||
| 254 | $this->token(self::TK_INICIA); |
||
| 255 | } |
||
| 256 | if ($this->tokenid == '') { |
||
| 257 | throw new RuntimeException("Falha token:$this->tokenid , Iniciar: $this->flagIniciar"); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Servidor |
||
| 263 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 264 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 265 | * @param array $data |
||
| 266 | * @param string $method |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function servidor($data = array(), $method = 'L') |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Situação Servidor Folha Pagamento |
||
| 282 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 283 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 284 | * @param array $data |
||
| 285 | * @param string $method |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | View Code Duplication | public function situacaoServidorFolhaPagamento($data = array(), $method = 'L') |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Componentes Folha Pagamento |
||
| 300 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 301 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 302 | * @param array $data |
||
| 303 | * @param string $method |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | View Code Duplication | public function componentesFolhaPagamento($data = array(), $method = 'L') |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Folha Pagamento |
||
| 318 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
| 319 | * se ainda não tiver iniciado -> inicia (automático) |
||
| 320 | * @param array $data |
||
| 321 | * @param string $method |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function folhaPagamento($data = array(), $method = 'L') |
|
| 333 | } |
||
| 334 |
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.