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 |
||
| 10 | abstract class AbstractPrestashopProductsImportCommand extends Command |
||
| 11 | { |
||
| 12 | const PRESTASHOP_RESOURCE_NAME = 'products'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The name and signature of the console command. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $signature = 'prestashop:import-products {--id=* : The Prestashop ID of the product}'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The console command description. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $description = 'Import or sync products with Prestashop products database'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The prestashop singleton from the service provider |
||
| 30 | * @var PrestashopWebService |
||
| 31 | */ |
||
| 32 | protected $prestashop; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a new command instance. |
||
| 36 | * |
||
| 37 | * @param PrestashopWebService $prestashop |
||
| 38 | */ |
||
| 39 | public function __construct(PrestashopWebService $prestashop) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Execute the console command. |
||
| 48 | * |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public function handle() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function getProducts() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $id |
||
| 132 | * @return \SimpleXMLElement |
||
| 133 | * @throws \DansMaCulotte\PrestashopWebService\Exceptions\PrestashopWebServiceException |
||
| 134 | */ |
||
| 135 | public function getProduct(string $id) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param \SimpleXMLElement $product |
||
| 147 | */ |
||
| 148 | abstract public function importProduct(\SimpleXMLElement $product); |
||
| 149 | } |
||
| 150 |
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.