Complex classes like ProductPresenter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductPresenter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ProductPresenter extends Presenter |
||
9 | { |
||
10 | use HasImagesPresentable; |
||
11 | |||
12 | const END_DATE = 'expire_date'; |
||
13 | const PRICE_EMPTY = '0.00'; |
||
14 | |||
15 | /** |
||
16 | * Render product's name in uppercase. |
||
17 | * |
||
18 | * @return string |
||
19 | */ |
||
20 | public function renderName() |
||
27 | |||
28 | public function renderDraftedName() |
||
35 | |||
36 | public function renderNameSimple() |
||
40 | |||
41 | public function renderCountItem() |
||
46 | |||
47 | public function renderPrice($price = null, $currency = null) |
||
51 | |||
52 | public function renderNewPrice() { |
||
60 | |||
61 | public function renderOldPrice() |
||
70 | |||
71 | public function renderCurrency($inst = 'title') |
||
81 | |||
82 | /** |
||
83 | * Calculate price amount sale. |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function renderSalePercent() |
||
94 | |||
95 | /** |
||
96 | * Render price with calc. sale.. |
||
97 | * |
||
98 | * @param $onlyPrice |
||
99 | * @return string |
||
100 | */ |
||
101 | /* public function renderPriceWithSale($onlyPrice = false) |
||
|
|||
102 | { |
||
103 | $price = $this->reformatPrice($this->model->price - $this->getPriceAmountSale()); |
||
104 | // $price = $this->getPriceAmountSale(); |
||
105 | $currency = $this->renderCurrency(); |
||
106 | if($onlyPrice) |
||
107 | return ($price != 0) ? $price : ''; |
||
108 | |||
109 | return sprintf('%s %s', $price,$currency); |
||
110 | }*/ |
||
111 | |||
112 | public function renderPriceWithSale($onlyPrice = false) |
||
122 | |||
123 | public function getSaledPrice() |
||
127 | |||
128 | /** |
||
129 | * Calculate price amount sale. |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getPriceAmountSale() |
||
137 | |||
138 | /** |
||
139 | * Reformat price. |
||
140 | * |
||
141 | * @param $price |
||
142 | * @return string |
||
143 | */ |
||
144 | public function reformatPrice($price) |
||
148 | |||
149 | /** |
||
150 | * Render economy price. |
||
151 | * - rest from sale. |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function economyPrice() |
||
162 | |||
163 | /** |
||
164 | * Render sale of product. |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function renderSale() |
||
172 | |||
173 | /** |
||
174 | * Render end date from expiration_date timestamp. |
||
175 | * |
||
176 | * @param string $format |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function endDate($format = 'm/d/Y') |
||
185 | |||
186 | /** |
||
187 | * Calc. difference from (expiration_date and today), |
||
188 | * end_date MUST be bigger than now date. |
||
189 | * |
||
190 | * @return \DateInterval |
||
191 | */ |
||
192 | public function diffEndDate() |
||
198 | |||
199 | public function getTotalSumm() |
||
208 | |||
209 | public function getSalesSumm() |
||
223 | |||
224 | /** |
||
225 | * @param bool $rotate |
||
226 | * @return float|string |
||
227 | */ |
||
228 | public function getSalesPercent($rotate = true) |
||
241 | |||
242 | public function getSale($showPercent = false) |
||
254 | |||
255 | public function renderInvolvedPriceSumm($count) |
||
261 | |||
262 | public function getInfoLabel() |
||
266 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.