Complex classes like Title 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 Title, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Title implements TitleContract |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | protected $segments = []; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $separator; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $siteName; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var integer |
||
| 27 | */ |
||
| 28 | protected $maxSegment; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var integer |
||
| 32 | */ |
||
| 33 | protected $maxTitle; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var boolean |
||
| 37 | */ |
||
| 38 | protected $reverse; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var boolean |
||
| 42 | */ |
||
| 43 | protected $siteNameVisibility; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a new title instance. |
||
| 47 | * |
||
| 48 | * @param array $config |
||
| 49 | */ |
||
| 50 | public function __construct(array $config = []) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Init config. |
||
| 57 | * |
||
| 58 | * @param array $config |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | protected function initConfig(array $config) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set title separator. |
||
| 73 | * |
||
| 74 | * @param string $separator |
||
| 75 | * @return \LaraComponents\Seo\Title |
||
| 76 | */ |
||
| 77 | public function setSeparator($separator) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get title separator. |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getSeparator() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Set site name. |
||
| 98 | * |
||
| 99 | * @param string $siteName |
||
| 100 | * @return \LaraComponents\Seo\Title |
||
| 101 | */ |
||
| 102 | public function setSiteName($siteName) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get site name. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getSiteName() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set segment max length. |
||
| 123 | * |
||
| 124 | * @param integer $max |
||
| 125 | * @return \LaraComponents\Seo\Title |
||
| 126 | */ |
||
| 127 | public function setMaxSegemnt($max) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get segment max length. |
||
| 136 | * |
||
| 137 | * @return integer |
||
| 138 | */ |
||
| 139 | public function getMaxSegment() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set title max length. |
||
| 146 | * |
||
| 147 | * @param integer $max |
||
| 148 | * @return \LaraComponents\Seo\Title |
||
| 149 | */ |
||
| 150 | public function setMaxTitle($max) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get title max length. |
||
| 159 | * |
||
| 160 | * @return integer |
||
| 161 | */ |
||
| 162 | public function getMaxTitle() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Set reverse title position. |
||
| 169 | * |
||
| 170 | * @param boolean $reverse |
||
| 171 | */ |
||
| 172 | public function setReverse($reverse = true) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check reverse title position. |
||
| 181 | * |
||
| 182 | * @return boolean |
||
| 183 | */ |
||
| 184 | public function isReverse() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the site name visibility. |
||
| 191 | * |
||
| 192 | * @param boolean $visible |
||
| 193 | */ |
||
| 194 | public function setSiteNameVisibility($visible) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Check if site name exists and visible. |
||
| 203 | * |
||
| 204 | * @return boolean |
||
| 205 | */ |
||
| 206 | public function isSiteNameVisible() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Appent title to segments. |
||
| 213 | * |
||
| 214 | * @return \LaraComponents\Seo\Title |
||
| 215 | */ |
||
| 216 | public function append() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set title segments. |
||
| 229 | * |
||
| 230 | * @return \LaraComponents\Seo\Title |
||
| 231 | */ |
||
| 232 | public function set() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get title segments. |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function get() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Make the title string. |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function make() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Normalize the get segments. |
||
| 277 | * |
||
| 278 | * @param array $segments |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected function normalizeSegments($segments) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Limit the number of characters in a string. |
||
| 302 | * |
||
| 303 | * @param string $value |
||
| 304 | * @param int $limit |
||
| 305 | * @param string $end |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | protected function limit($value, $limit = 100, $end = '...') |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Convert object to string. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function __toString() |
||
| 326 | } |
||
| 327 |