Complex classes like OpenGraph 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 OpenGraph, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class OpenGraph |
||
6 | { |
||
7 | const DETERMINER_SUPPORTED = [ |
||
8 | 'a', 'an', 'auto', 'the', |
||
9 | ]; |
||
10 | |||
11 | const LOCALE_SUPPORTED = [ |
||
12 | 'af_ZA', 'ak_GH', 'am_ET', 'ar_AR', 'as_IN', 'ay_BO', 'az_AZ', 'be_BY', 'bg_BG', 'bn_IN', 'br_FR', |
||
13 | 'bs_BA', 'ca_ES', 'cb_IQ', 'ck_US', 'co_FR', 'cs_CZ', 'cx_PH', 'cy_GB', 'da_DK', 'de_DE', 'el_GR', |
||
14 | 'en_GB', 'en_IN', 'en_US', 'eo_EO', 'es_CO', 'es_ES', 'es_LA', 'et_EE', 'eu_ES', 'fa_IR', 'ff_NG', |
||
15 | 'fi_FI', 'fo_FO', 'fr_CA', 'fr_FR', 'fy_NL', 'ga_IE', 'gl_ES', 'gn_PY', 'gu_IN', 'gx_GR', 'ha_NG', |
||
16 | 'he_IL', 'hi_IN', 'hr_HR', 'hu_HU', 'hy_AM', 'id_ID', 'ig_NG', 'is_IS', 'it_IT', 'ja_JP', 'ja_KS', |
||
17 | 'jv_ID', 'ka_GE', 'kk_KZ', 'km_KH', 'kn_IN', 'ko_KR', 'ku_TR', 'la_VA', 'lg_UG', 'li_NL', 'ln_CD', |
||
18 | 'lo_LA', 'lt_LT', 'lv_LV', 'mg_MG', 'mk_MK', 'ml_IN', 'mn_MN', 'mr_IN', 'ms_MY', 'mt_MT', 'my_MM', |
||
19 | 'nb_NO', 'nd_ZW', 'ne_NP', 'nl_BE', 'nl_NL', 'nn_NO', 'ny_MW', 'or_IN', 'pa_IN', 'pl_PL', 'ps_AF', |
||
20 | 'pt_BR', 'pt_PT', 'qu_PE', 'rm_CH', 'ro_RO', 'ru_RU', 'rw_RW', 'sa_IN', 'sc_IT', 'se_NO', 'si_LK', |
||
21 | 'sk_SK', 'sl_SI', 'sn_ZW', 'so_SO', 'sq_AL', 'sr_RS', 'sv_SE', 'sw_KE', 'sy_SY', 'sz_PL', 'ta_IN', |
||
22 | 'te_IN', 'tg_TJ', 'th_TH', 'tk_TM', 'tl_PH', 'tr_TR', 'tt_RU', 'tz_MA', 'uk_UA', 'ur_PK', 'uz_UZ', |
||
23 | 'vi_VN', 'wo_SN', 'xh_ZA', 'yi_DE', 'yo_NG', 'zh_CN', 'zh_HK', 'zh_TW', 'zu_ZA', 'zz_TR', |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $type; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $title; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $siteName; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $description; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $url; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $determiner; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $locale; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $alternateLocales = []; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $images = []; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $audios = []; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $videos = []; |
||
80 | |||
81 | public function setType($type) |
||
87 | |||
88 | public function getType() |
||
92 | |||
93 | public function setTitle($title) |
||
99 | |||
100 | public function getTitle() |
||
104 | |||
105 | public function setSiteName($siteName) |
||
111 | |||
112 | public function getSiteName() |
||
116 | |||
117 | public function setDescription($description) |
||
123 | |||
124 | public function getDescription() |
||
128 | |||
129 | public function setUrl($url) |
||
135 | |||
136 | public function getUrl() |
||
140 | |||
141 | public function setDeterminer($determiner) |
||
149 | |||
150 | public function getDeterminer() |
||
154 | |||
155 | public function setLocale($locale) |
||
163 | |||
164 | public function getLocale() |
||
168 | |||
169 | public function addAlternateLocale($locale) |
||
177 | |||
178 | public function setAlternateLocales(array $locales) |
||
188 | |||
189 | public function getAlternateLocales() |
||
193 | |||
194 | public function addImage(Image $image) |
||
200 | |||
201 | public function setImages(array $images) |
||
211 | |||
212 | public function getImages() |
||
216 | |||
217 | public function addAudio(Audio $audio) |
||
223 | |||
224 | public function setAudios(array $audios) |
||
234 | |||
235 | public function getAudios() |
||
239 | |||
240 | public function addVideo(Video $video) |
||
246 | |||
247 | public function setVideos(array $videos) |
||
257 | |||
258 | public function getVideos() |
||
262 | |||
263 | protected function trim($string) |
||
269 | |||
270 | public function toArray() |
||
300 | } |
||
301 |