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 |
||
| 9 | abstract class AbstractPrestashopSuppliersImportCommand extends Command |
||
| 10 | { |
||
| 11 | const PRESTASHOP_RESOURCE_NAME = 'suppliers'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The name and signature of the console command. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $signature = 'prestashop:import-suppliers {--id=* : The Prestashop ID of the supplier}'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The console command description. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $description = 'Import and sync vendors with Prestashop suppliers database'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The prestashop singleton from the service provider |
||
| 29 | * @var PrestashopWebService |
||
| 30 | */ |
||
| 31 | protected $prestashop; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new command instance. |
||
| 35 | * |
||
| 36 | * @param PrestashopWebService $prestashop |
||
| 37 | */ |
||
| 38 | public function __construct(PrestashopWebService $prestashop) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Execute the console command. |
||
| 47 | * |
||
| 48 | * @return mixed |
||
| 49 | */ |
||
| 50 | public function handle() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | View Code Duplication | private function getSuppliers() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $id |
||
| 109 | * @return \SimpleXMLElement |
||
| 110 | * @throws \DansMaCulotte\PrestashopWebService\Exceptions\PrestashopWebServiceException |
||
| 111 | */ |
||
| 112 | public function getSupplier(string $id) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param \SimpleXMLElement $supplier |
||
| 124 | */ |
||
| 125 | abstract public function importSupplier(\SimpleXMLElement $supplier); |
||
| 126 | } |
||
| 127 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.