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:
Complex classes like Tools often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Tools, and based on these observations, apply Extract Interface, too.
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 = '', $debug = false) |
|
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 | * Retorna no token ativo |
||
87 | * @return string |
||
88 | */ |
||
89 | 3 | public function getToken() |
|
93 | |||
94 | /** |
||
95 | * Retorna o status de inicio de transferencia |
||
96 | * @return bool |
||
97 | */ |
||
98 | 3 | 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) |
||
258 | |||
259 | /** |
||
260 | * Extrai os dados entre dois marcadores |
||
261 | * @param string $string |
||
262 | * @param string $start |
||
263 | * @param string $end |
||
264 | * @return string |
||
265 | */ |
||
266 | private function getStringBetween($string, $start, $end) |
||
277 | |||
278 | /** |
||
279 | * Inicia o processo de tranferência de dados |
||
280 | * @param array $data |
||
281 | * @throws InvalidArgumentException |
||
282 | * @throws RuntimeException |
||
283 | */ |
||
284 | protected function obterTokenIniciarTransferencia($data = [], $method = 'L') |
||
297 | |||
298 | /** |
||
299 | * Servidor |
||
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 servidor($data = [], $method = 'L') |
|
316 | |||
317 | /** |
||
318 | * Situação Servidor Folha Pagamento |
||
319 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
320 | * se ainda não tiver iniciado -> inicia (automático) |
||
321 | * @param array $data |
||
322 | * @param string $method |
||
323 | * @return array |
||
324 | */ |
||
325 | View Code Duplication | public function situacaoServidorFolhaPagamento($data = [], $method = 'L') |
|
334 | |||
335 | /** |
||
336 | * Componentes Folha Pagamento |
||
337 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
338 | * se ainda não tiver iniciado -> inicia (automático) |
||
339 | * @param array $data |
||
340 | * @param string $method |
||
341 | * @return array |
||
342 | */ |
||
343 | View Code Duplication | public function componentesFolhaPagamento($data = [], $method = 'L') |
|
352 | |||
353 | /** |
||
354 | * Folha Pagamento |
||
355 | * se ainda não tiver o TOKEN -> Obtem (automático) |
||
356 | * se ainda não tiver iniciado -> inicia (automático) |
||
357 | * @param array $data |
||
358 | * @param string $method |
||
359 | * @return array |
||
360 | */ |
||
361 | View Code Duplication | public function folhaPagamento($data = [], $method = 'L') |
|
370 | } |
||
371 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.