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 |
||
17 | class PriceFinder |
||
18 | { |
||
19 | private $currency; |
||
20 | private $country; |
||
21 | private $customerGroup; |
||
22 | private $channel; |
||
23 | |||
24 | 26 | public function __construct( |
|
35 | |||
36 | /** |
||
37 | * @param PriceCollection $prices |
||
38 | * @return Price |
||
39 | */ |
||
40 | 26 | public function findPrice(PriceCollection $prices) |
|
41 | { |
||
42 | 26 | $currency = $this->currency; |
|
43 | 26 | $country = $this->country; |
|
44 | 26 | $customerGroup = $this->customerGroup; |
|
45 | 26 | $channel = $this->channel; |
|
46 | |||
47 | 26 | $prices = new \CallbackFilterIterator( |
|
48 | 26 | $prices, |
|
49 | 26 | function ($price) use ($currency, $country, $customerGroup, $channel) { |
|
50 | 26 | if (!$this->priceHasCurrency($price, $currency)) { |
|
51 | 21 | return false; |
|
52 | } |
||
53 | 26 | if (!$this->priceHasNoDate($price) && !$this->priceHasValidDate($price, new \DateTime())) { |
|
54 | return false; |
||
55 | } |
||
56 | 26 | View Code Duplication | if (is_null($country)) { |
|
|||
57 | 6 | if ($this->priceHas($price, 'country')) { |
|
58 | 6 | return false; |
|
59 | } |
||
60 | 20 | } elseif (!$this->priceHasCountry($price, $country)) { |
|
61 | 18 | return false; |
|
62 | } |
||
63 | 26 | if (is_null($customerGroup)) { |
|
64 | 12 | if ($this->priceHas($price, 'customerGroup')) { |
|
65 | 12 | return false; |
|
66 | } |
||
67 | 14 | } elseif (!$this->priceHasCustomerGroup($price, $customerGroup)) { |
|
68 | 12 | return false; |
|
69 | } |
||
70 | 26 | View Code Duplication | if (is_null($channel)) { |
71 | 12 | if ($this->priceHas($price, 'channel')) { |
|
72 | 12 | return false; |
|
73 | } |
||
74 | 14 | } elseif (!$this->priceHasChannel($price, $channel)) { |
|
75 | 13 | return false; |
|
76 | } |
||
77 | 26 | return true; |
|
78 | 26 | } |
|
79 | ); |
||
80 | |||
81 | 26 | foreach ($prices as $price) { |
|
82 | 26 | return $price; |
|
83 | } |
||
84 | return null; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param $prices |
||
89 | * @param $currency |
||
90 | * @param string $country |
||
91 | * @param CustomerGroupReference $customerGroup |
||
92 | * @param ChannelReference $channel |
||
93 | * @return Price|null |
||
94 | */ |
||
95 | 26 | public static function findPriceFor( |
|
106 | |||
107 | 26 | private function priceHasCurrency(Price $price, $currency) |
|
111 | |||
112 | 20 | private function priceHasCountry(Price $price, $country) |
|
116 | |||
117 | 14 | private function priceHasCustomerGroup(Price $price, CustomerGroupReference $customerGroup) |
|
121 | |||
122 | 14 | private function priceHasChannel(Price $price, ChannelReference $channel) |
|
126 | |||
127 | private function priceHasValidDate(Price $price, \DateTime $date) |
||
133 | |||
134 | 26 | private function priceHasNoDate(Price $price) |
|
138 | |||
139 | 18 | private function priceHas(Price $price, $field) |
|
143 | } |
||
144 |
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.