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:
Complex classes like EccubeExtension 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 EccubeExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class EccubeExtension extends AbstractExtension |
||
|
|
|||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var EccubeConfig |
||
| 29 | */ |
||
| 30 | protected $eccubeConfig; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var TaxRuleService |
||
| 34 | */ |
||
| 35 | protected $TaxRuleService; |
||
| 36 | |||
| 37 | 463 | public function __construct(TaxRuleService $TaxRuleService, EccubeConfig $eccubeConfig) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Returns a list of functions to add to the existing list. |
||
| 45 | * |
||
| 46 | * @return array An array of functions |
||
| 47 | */ |
||
| 48 | 243 | public function getFunctions() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Returns a list of filters. |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | 243 | public function getFilters() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Name of this extension |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function getName() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Name of this extension |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | 11 | public function getCalcIncTax($price, $tax_rate, $tax_rule) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Name of this extension |
||
| 106 | * |
||
| 107 | * @param array $menus |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | 147 | public function getActiveMenus($menus = []) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * return No Image filename |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 61 | public function getNoImageProduct($image) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Name of this extension |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 142 | public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d') |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Name of this extension |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | 95 | public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Name of this extension |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function getEllipsis($value, $length = 100, $end = '...') |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Name of this extension |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getTimeAgo($date) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if the value is object |
||
| 181 | * |
||
| 182 | * @param object $value |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function isObject($value) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * FormView にエラーが含まれるかを返す. |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 13 | public function hasErrors() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * product_idで指定したProductを取得 |
||
| 216 | * Productが取得できない場合、または非公開の場合、商品情報は表示させない。 |
||
| 217 | * デバッグ環境以外ではProductが取得できなくでもエラー画面は表示させず無視される。 |
||
| 218 | * |
||
| 219 | * @param $id |
||
| 220 | * |
||
| 221 | * @return Product|null |
||
| 222 | */ |
||
| 223 | public function getProduct($id) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Twigでphp関数を使用できるようにする。 |
||
| 240 | * |
||
| 241 | * @return mixed|null |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function getPhpFunctions() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get the ClassCategories as JSON. |
||
| 259 | * |
||
| 260 | * @param Product $Product |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 15 | public function getClassCategoriesAsJson(Product $Product) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Display file extension icon |
||
| 310 | * |
||
| 311 | * @param $ext |
||
| 312 | * @param $attr |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | 3 | public function getExtensionIcon($ext, $attr = []) |
|
| 360 | } |
||
| 361 |