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 |
||
18 | class Similarity implements EngineInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var Factory |
||
22 | */ |
||
23 | private $_factory; |
||
24 | |||
25 | /** |
||
26 | * @var PersonalisedProductsLogger |
||
27 | */ |
||
28 | private $_logger; |
||
29 | |||
30 | /** |
||
31 | * @var Config |
||
32 | */ |
||
33 | private $_config; |
||
34 | |||
35 | /** |
||
36 | * @var Urls |
||
37 | */ |
||
38 | private $_urls; |
||
39 | |||
40 | /** |
||
41 | * @var \predictionio\EngineClient |
||
42 | */ |
||
43 | private $_engineClient; |
||
44 | |||
45 | /** |
||
46 | * Similarity constructor. |
||
47 | * @param Factory $factory |
||
48 | * @param PersonalisedProductsLogger $logger |
||
49 | * @param Config $config |
||
50 | * @param Urls $urls |
||
51 | */ |
||
52 | View Code Duplication | public function __construct( |
|
72 | |||
73 | /** |
||
74 | * Send the query to PredictionIO engine for product data set |
||
75 | * |
||
76 | * @param array $productIds |
||
77 | * @param array $categories |
||
78 | * @param array $whitelist |
||
79 | * @param array $blacklist |
||
80 | * @return array|bool |
||
81 | */ |
||
82 | public function sendQuery(array $productIds, array $categories = [], array $whitelist = [], array $blacklist = []) |
||
103 | |||
104 | /** |
||
105 | * Add query properties to the data in the query for PredictionIO engine |
||
106 | * |
||
107 | * @param $property |
||
108 | * @param $data |
||
109 | * @param $propertyData |
||
110 | * @return $this |
||
111 | */ |
||
112 | private function _addProperties($property, &$data, $propertyData) |
||
120 | } |
||
121 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.