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 |
||
| 7 | class OpenGraph |
||
| 8 | { |
||
| 9 | const DETERMINER_SUPPORTED = [ |
||
| 10 | 'a', 'an', 'auto', 'the', |
||
| 11 | ]; |
||
| 12 | |||
| 13 | const LOCALE_SUPPORTED = [ |
||
| 14 | 'af_ZA', 'ak_GH', 'am_ET', 'ar_AR', 'as_IN', 'ay_BO', 'az_AZ', 'be_BY', 'bg_BG', 'bn_IN', 'br_FR', |
||
| 15 | 'bs_BA', 'ca_ES', 'cb_IQ', 'ck_US', 'co_FR', 'cs_CZ', 'cx_PH', 'cy_GB', 'da_DK', 'de_DE', 'el_GR', |
||
| 16 | 'en_GB', 'en_IN', 'en_US', 'eo_EO', 'es_CO', 'es_ES', 'es_LA', 'et_EE', 'eu_ES', 'fa_IR', 'ff_NG', |
||
| 17 | 'fi_FI', 'fo_FO', 'fr_CA', 'fr_FR', 'fy_NL', 'ga_IE', 'gl_ES', 'gn_PY', 'gu_IN', 'gx_GR', 'ha_NG', |
||
| 18 | 'he_IL', 'hi_IN', 'hr_HR', 'hu_HU', 'hy_AM', 'id_ID', 'ig_NG', 'is_IS', 'it_IT', 'ja_JP', 'ja_KS', |
||
| 19 | 'jv_ID', 'ka_GE', 'kk_KZ', 'km_KH', 'kn_IN', 'ko_KR', 'ku_TR', 'la_VA', 'lg_UG', 'li_NL', 'ln_CD', |
||
| 20 | 'lo_LA', 'lt_LT', 'lv_LV', 'mg_MG', 'mk_MK', 'ml_IN', 'mn_MN', 'mr_IN', 'ms_MY', 'mt_MT', 'my_MM', |
||
| 21 | 'nb_NO', 'nd_ZW', 'ne_NP', 'nl_BE', 'nl_NL', 'nn_NO', 'ny_MW', 'or_IN', 'pa_IN', 'pl_PL', 'ps_AF', |
||
| 22 | 'pt_BR', 'pt_PT', 'qu_PE', 'rm_CH', 'ro_RO', 'ru_RU', 'rw_RW', 'sa_IN', 'sc_IT', 'se_NO', 'si_LK', |
||
| 23 | 'sk_SK', 'sl_SI', 'sn_ZW', 'so_SO', 'sq_AL', 'sr_RS', 'sv_SE', 'sw_KE', 'sy_SY', 'sz_PL', 'ta_IN', |
||
| 24 | 'te_IN', 'tg_TJ', 'th_TH', 'tk_TM', 'tl_PH', 'tr_TR', 'tt_RU', 'tz_MA', 'uk_UA', 'ur_PK', 'uz_UZ', |
||
| 25 | 'vi_VN', 'wo_SN', 'xh_ZA', 'yi_DE', 'yo_NG', 'zh_CN', 'zh_HK', 'zh_TW', 'zu_ZA', 'zz_TR', |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $type; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var LaraComponents\Seo\OpenGraph\Objects\TypeObject |
||
| 35 | */ |
||
| 36 | protected $typeObject; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $title; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $siteName; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $description; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $url; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $determiner; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $locale; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $alternateLocales = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $images = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $audios = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $videos = []; |
||
| 87 | |||
| 88 | public function setType($type) |
||
| 94 | |||
| 95 | public function getType() |
||
| 99 | |||
| 100 | public function setTypeObject(TypeObject $typeObject) |
||
| 104 | |||
| 105 | public function getTypeObject() |
||
| 109 | |||
| 110 | public function setTitle($title) |
||
| 116 | |||
| 117 | public function getTitle() |
||
| 121 | |||
| 122 | public function setSiteName($siteName) |
||
| 128 | |||
| 129 | public function getSiteName() |
||
| 133 | |||
| 134 | public function setDescription($description) |
||
| 140 | |||
| 141 | public function getDescription() |
||
| 145 | |||
| 146 | public function setUrl($url) |
||
| 152 | |||
| 153 | public function getUrl() |
||
| 157 | |||
| 158 | public function setDeterminer($determiner) |
||
| 166 | |||
| 167 | public function getDeterminer() |
||
| 171 | |||
| 172 | public function setLocale($locale) |
||
| 180 | |||
| 181 | public function getLocale() |
||
| 185 | |||
| 186 | public function addAlternateLocale($locale) |
||
| 194 | |||
| 195 | public function setAlternateLocales(array $locales) |
||
| 205 | |||
| 206 | public function getAlternateLocales() |
||
| 210 | |||
| 211 | public function addImage(Image $image) |
||
| 217 | |||
| 218 | public function setImages(array $images) |
||
| 228 | |||
| 229 | public function getImages() |
||
| 233 | |||
| 234 | public function addAudio(Audio $audio) |
||
| 240 | |||
| 241 | public function setAudios(array $audios) |
||
| 251 | |||
| 252 | public function getAudios() |
||
| 256 | |||
| 257 | public function addVideo(Video $video) |
||
| 263 | |||
| 264 | public function setVideos(array $videos) |
||
| 274 | |||
| 275 | public function getVideos() |
||
| 279 | |||
| 280 | protected function trim($string) |
||
| 286 | |||
| 287 | public function toArray() |
||
| 317 | } |
||
| 318 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..