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 |
||
| 11 | class ExchangeHistoryTableFeeder extends Command |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The name and signature of the console command. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $signature = 'history_table:feed'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The console command description. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $description = 'Command description'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Create a new command instance. |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | public function __construct() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Execute the console command. |
||
| 38 | * |
||
| 39 | * @return mixed |
||
| 40 | */ |
||
| 41 | public function handle() |
||
| 45 | |||
| 46 | /** |
||
| 47 | *Elimina les dades antigues i n'insereix unes de noves |
||
| 48 | */ |
||
| 49 | public function getSymbolsFormDB() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Aconegueix les dades de la api i fa la crida de la funció que les guardara a la BD |
||
| 67 | * |
||
| 68 | * @param GetMethods $interaction_methods |
||
| 69 | * @param $symbol |
||
| 70 | */ |
||
| 71 | public function httpCall(GetMethods $interaction_methods, $symbol) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * |
||
| 82 | * Persisteix les dades al MYSQL |
||
| 83 | * |
||
| 84 | * @param $data |
||
| 85 | */ |
||
| 86 | public function storeDataInDB($data) |
||
| 103 | |||
| 104 | } |
||
| 105 |
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: