Complex classes like Jetpack_SEO_Titles 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 Jetpack_SEO_Titles, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Jetpack_SEO_Titles { |
||
38 | /** |
||
39 | * Site option name used to store custom title formats. |
||
40 | */ |
||
41 | const TITLE_FORMATS_OPTION = 'advanced_seo_title_formats'; |
||
42 | |||
43 | /** |
||
44 | * Retrieves custom title formats from site option. |
||
45 | * |
||
46 | * @return array Array of custom title formats, or empty array. |
||
47 | */ |
||
48 | public static function get_custom_title_formats() { |
||
55 | |||
56 | /** |
||
57 | * Returns tokens that are currently supported for each page type. |
||
58 | * |
||
59 | * @return array Array of allowed token strings. |
||
60 | */ |
||
61 | public static function get_allowed_tokens() { |
||
70 | |||
71 | /** |
||
72 | * Used to modify the default title with custom SEO title. |
||
73 | * |
||
74 | * @param string $default_title Default title for current page. |
||
75 | * |
||
76 | * @return string Custom title with replaced tokens or default title. |
||
77 | */ |
||
78 | public static function get_custom_title( $default_title = '' ) { |
||
115 | |||
116 | /** |
||
117 | * Returns string value for given token. |
||
118 | * |
||
119 | * @param string $token_name The token name value that should be replaced. |
||
120 | * |
||
121 | * @return string Token replacement for current site, or empty string for unknown token name. |
||
122 | */ |
||
123 | public static function get_token_value( $token_name ) { |
||
146 | |||
147 | /** |
||
148 | * Returns page type for current page. We need this helper in order to determine what |
||
149 | * user defined title format should be used for custom title. |
||
150 | * |
||
151 | * @return string|bool Type of current page or false if unsupported. |
||
152 | */ |
||
153 | public static function get_page_type() { |
||
177 | |||
178 | /** |
||
179 | * Returns the value that should be used as a replacement for the date token, |
||
180 | * depending on the archive path specified. |
||
181 | * |
||
182 | * @return string Token replacement for a given date, or empty string if no date is specified. |
||
183 | */ |
||
184 | public static function get_date_for_title() { |
||
202 | |||
203 | /** |
||
204 | * Checks if current theme is defining custom title that won't work nicely |
||
205 | * with our custom SEO title override. |
||
206 | * |
||
207 | * @return bool True if current theme sets custom title, false otherwise. |
||
208 | */ |
||
209 | public static function is_conflicted_theme() { |
||
226 | |||
227 | /** |
||
228 | * Checks if a given format conforms to predefined SEO title templates. |
||
229 | * |
||
230 | * Every format type and token must be whitelisted. |
||
231 | * @see get_allowed_tokens() |
||
232 | * |
||
233 | * @param array $title_formats Template of SEO title to check. |
||
234 | * |
||
235 | * @return bool True if the formats are valid, false otherwise. |
||
236 | */ |
||
237 | public static function are_valid_title_formats( $title_formats ) { |
||
268 | |||
269 | /** |
||
270 | * Combines the previous values of title formats, stored as array in site options, |
||
271 | * with the new values that are provided. |
||
272 | * |
||
273 | * @param array $new_formats Array containing new title formats. |
||
274 | * |
||
275 | * @return array $result Array of updated title formats, or empty array if no update was performed. |
||
276 | */ |
||
277 | public static function update_title_formats( $new_formats ) { |
||
297 | } |
||
298 |