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 |
||
16 | class ExchangeHistoryTableFeeder extends Command |
||
17 | { |
||
18 | /** |
||
19 | * The name and signature of the console command. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $signature = 'history_table:feed'; |
||
24 | |||
25 | /** |
||
26 | * The console command description. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $description = 'Feed the table exchange_history from DB'; |
||
31 | |||
32 | /** |
||
33 | * Create a new command instance. |
||
34 | * |
||
35 | */ |
||
36 | public function __construct() |
||
40 | |||
41 | /** |
||
42 | * Execute the console command. |
||
43 | * |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public function handle() |
||
50 | |||
51 | /** |
||
52 | *Elimina les dades antigues i n'insereix unes de noves |
||
53 | */ |
||
54 | public function getSymbolsFormDB() |
||
89 | |||
90 | /** |
||
91 | * Aconegueix les dades de la api i fa la crida de la funció que les guardara a la BD |
||
92 | * |
||
93 | * @param GetMethods $interaction_methods |
||
94 | * @param $symbol |
||
95 | */ |
||
96 | public function httpCall(GetMethods $interaction_methods, $symbol) |
||
104 | |||
105 | /** |
||
106 | * |
||
107 | * Persisteix les dades al MYSQL |
||
108 | * |
||
109 | * @param $data |
||
110 | */ |
||
111 | public function storeDataInDB($data) |
||
128 | |||
129 | } |
||
130 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: