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 |
||
| 24 | class EccubeExtension extends AbstractExtension |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var EccubeConfig |
||
| 28 | */ |
||
| 29 | protected $eccubeConfig; |
||
| 30 | |||
| 31 | 470 | public function __construct(EccubeConfig $eccubeConfig) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Returns a list of functions to add to the existing list. |
||
| 38 | * |
||
| 39 | * @return TwigFunction[] An array of functions |
||
| 40 | */ |
||
| 41 | 241 | public function getFunctions() |
|
| 42 | { |
||
| 43 | return [ |
||
| 44 | 241 | new TwigFunction('has_errors', [$this, 'hasErrors']), |
|
| 45 | 241 | new TwigFunction('active_menus', [$this, 'getActiveMenus']), |
|
| 46 | 241 | new TwigFunction('class_categories_as_json', [$this, 'getClassCategoriesAsJson']), |
|
| 47 | 241 | new TwigFunction('php_*', function () { |
|
| 48 | 10 | $arg_list = func_get_args(); |
|
| 49 | 10 | $function = array_shift($arg_list); |
|
| 50 | 10 | if (is_callable($function)) { |
|
| 51 | 10 | return call_user_func_array($function, $arg_list); |
|
| 52 | } |
||
| 53 | trigger_error('Called to an undefined function : php_'.$function, E_USER_WARNING); |
||
| 54 | 241 | }, ['pre_escape' => 'html', 'is_safe' => ['html']]), |
|
| 55 | ]; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns a list of filters. |
||
| 60 | * |
||
| 61 | * @return TwigFilter[] |
||
| 62 | */ |
||
| 63 | 241 | public function getFilters() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Name of this extension |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getName() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Name of this extension |
||
| 87 | * |
||
| 88 | * @param array $menus |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | 152 | public function getActiveMenus($menus = []) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * return No Image filename |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 63 | public function getNoImageProduct($image) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Name of this extension |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 1 | public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d') |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Name of this extension |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | 141 | public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Name of this extension |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getEllipsis($value, $length = 100, $end = '...') |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Name of this extension |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getTimeAgo($date) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * FormView にエラーが含まれるかを返す. |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 13 | public function hasErrors() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * product_idで指定したProductを取得 |
||
| 185 | * Productが取得できない場合、または非公開の場合、商品情報は表示させない。 |
||
| 186 | * デバッグ環境以外ではProductが取得できなくでもエラー画面は表示させず無視される。 |
||
| 187 | * |
||
| 188 | * @param $id |
||
| 189 | * |
||
| 190 | * @return Product|null |
||
| 191 | */ |
||
| 192 | public function getProduct($id) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Twigでphp関数を使用できるようにする。 |
||
| 209 | * |
||
| 210 | * @return mixed|null |
||
| 211 | */ |
||
| 212 | public function getPhpFunctions() |
||
| 213 | { |
||
| 214 | $arg_list = func_get_args(); |
||
| 215 | $function = array_shift($arg_list); |
||
| 216 | |||
| 217 | if (is_callable($function)) { |
||
| 218 | return call_user_func_array($function, $arg_list); |
||
| 219 | } |
||
| 220 | |||
| 221 | trigger_error('Called to an undefined function : php_'.$function, E_USER_WARNING); |
||
| 222 | |||
| 223 | return null; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get the ClassCategories as JSON. |
||
| 228 | * |
||
| 229 | * @param Product $Product |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 16 | public function getClassCategoriesAsJson(Product $Product) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Display file extension icon |
||
| 279 | * |
||
| 280 | * @param $ext |
||
| 281 | * @param $attr |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | 3 | public function getExtensionIcon($ext, $attr = []) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * URLに対する権限有無チェック |
||
| 332 | * |
||
| 333 | * @param $target |
||
| 334 | * @param $AuthorityRoles |
||
| 335 | * |
||
| 336 | * @return boolean |
||
| 337 | */ |
||
| 338 | public function isAuthorizedUrl($target, $AuthorityRoles) |
||
| 349 | } |
||
| 350 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.