Complex classes like AutomatedLink 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 AutomatedLink, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class AutomatedLink extends DataObject implements PermissionProvider { |
||
| 23 | |||
| 24 | private static $db = array( |
||
| 25 | 'Phrase' => 'VARCHAR(255)', |
||
| 26 | 'TitleTag' => 'VARCHAR(255)', |
||
| 27 | 'AnchorTag' => 'VARCHAR(255)', |
||
| 28 | 'NewWindow' => 'Boolean', |
||
| 29 | 'NoFollow' => 'Boolean', |
||
| 30 | 'SelfLinking' => 'Boolean', |
||
| 31 | 'CaseSensitive' => 'Boolean', |
||
| 32 | 'MaxLinksPerPage' => 'INT', |
||
| 33 | 'Priority' => 'Int' |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $defaults = array( |
||
| 37 | 'MaxLinksPerPage' => 10 |
||
| 38 | ); |
||
| 39 | |||
| 40 | private static $default_sort = 'Priority'; |
||
| 41 | |||
| 42 | private static $has_one = array( |
||
| 43 | 'Page' => 'SiteTree' |
||
| 44 | ); |
||
| 45 | |||
| 46 | private static $summary_fields = array( 'Phrase', 'PointsTo' ); |
||
| 47 | private static $searchable_fields = array( 'Phrase' ); |
||
| 48 | private static $singular_name = 'Automated Link'; |
||
| 49 | private static $plural_name = 'Automated Links'; |
||
| 50 | private static $parsableFields = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the url of the page linked to this object |
||
| 54 | * |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public function PointsTo(){ |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Return the phrase set for this object |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function Title(){ |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return the rendered version of this object |
||
| 72 | * |
||
| 73 | * @return String |
||
| 74 | */ |
||
| 75 | public function forTemplate(){ |
||
| 78 | |||
| 79 | public function canView( $member = false ){ |
||
| 82 | |||
| 83 | public function canEdit( $member = false ){ |
||
| 86 | |||
| 87 | public function canDelete( $member = false ){ |
||
| 90 | |||
| 91 | public function canCreate( $member = false ){ |
||
| 94 | |||
| 95 | public function providePermissions() { |
||
| 103 | |||
| 104 | public function requireDefaultRecords(){ |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Returns the HTML Representation of this object |
||
| 115 | * |
||
| 116 | * @param String $originalPhrase |
||
| 117 | * @return String |
||
| 118 | */ |
||
| 119 | public function getHTML($originalPhrase = NULL) { |
||
| 128 | |||
| 129 | public function getCMSFields() { |
||
| 173 | |||
| 174 | public function getCMSValidator() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Save the Global Settings into the |
||
| 180 | * Global Auto Link Settings Object |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function onBeforeWrite() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Checks if the destination is a redirector page if so |
||
| 205 | * it updates it to the destination of the redirector page |
||
| 206 | * |
||
| 207 | * @Boolean $write - Write the changes if any |
||
| 208 | * @return void |
||
| 209 | */ |
||
| 210 | public function CheckAndUpdateDestination( $write = false ){ |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Checks if the field is parable |
||
| 226 | * |
||
| 227 | * @param SiteTree $page |
||
| 228 | * @param String $field |
||
| 229 | * @return Boolean |
||
| 230 | */ |
||
| 231 | public static function isFieldParsable(SiteTree $page, $field) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Checks if this link can be added to the provided |
||
| 243 | * page and field |
||
| 244 | * |
||
| 245 | * @param ContentController $controller |
||
| 246 | * @return Boolean |
||
| 247 | */ |
||
| 248 | public function canBeAdded( ContentController $controller ){ |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Turn the string passed into a DOMDocument object |
||
| 254 | * |
||
| 255 | * @param string $html |
||
| 256 | * @return DOMDocument |
||
| 257 | */ |
||
| 258 | public static function constructDOMDocument($html){ |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns an array with all the database fields $class has |
||
| 279 | * |
||
| 280 | * @param string $class |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | public static function getAllDatabaseFields($class){ |
||
| 290 | } |
||
| 291 |