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 |
||
| 15 | class CompaniesTableFeeder extends Command |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The name and signature of the console command. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $signature = 'companies_table:feed'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The console command description. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $description = 'Feed the table companies from DB'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create a new command instance. |
||
| 33 | * |
||
| 34 | */ |
||
| 35 | public function __construct() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Execute the console command. |
||
| 42 | * |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | public function handle() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * |
||
| 52 | */ |
||
| 53 | public function getSymbolsFormArray() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param GetMethods $interaction_methods |
||
| 91 | * @param $symbol |
||
| 92 | */ |
||
| 93 | public function httpCall(GetMethods $interaction_methods, $symbol) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param $data |
||
| 105 | * @param $symbol |
||
| 106 | */ |
||
| 107 | public function discardRepeated($data, $symbol) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $data |
||
| 124 | */ |
||
| 125 | public function storeDataInDB($data) |
||
| 137 | |||
| 138 | |||
| 139 | } |
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: