Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 12 | class AutomatedLink extends DataObject implements PermissionProvider { |
||
|
1 ignored issue
–
show
|
|||
| 13 | |||
| 14 | private static $db = array( |
||
|
2 ignored issues
–
show
|
|||
| 15 | 'Phrase' => 'VARCHAR(255)', |
||
| 16 | 'TitleTag' => 'VARCHAR(255)', |
||
| 17 | 'AnchorTag' => 'VARCHAR(255)', |
||
| 18 | 'NewWindow' => 'Boolean', |
||
| 19 | 'NoFollow' => 'Boolean', |
||
| 20 | 'SelfLinking' => 'Boolean', |
||
| 21 | 'CaseSensitive' => 'Boolean', |
||
| 22 | 'MaxLinksPerPage' => 'INT', |
||
| 23 | 'Priority' => 'Int' |
||
| 24 | ); |
||
| 25 | |||
| 26 | private static $defaults = array( |
||
|
2 ignored issues
–
show
|
|||
| 27 | 'MaxLinksPerPage' => 10 |
||
| 28 | ); |
||
| 29 | |||
| 30 | private static $default_sort = 'Priority'; |
||
|
2 ignored issues
–
show
|
|||
| 31 | |||
| 32 | private static $has_one = array( |
||
|
2 ignored issues
–
show
|
|||
| 33 | 'Page' => 'SiteTree' |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $summary_fields = array( 'Phrase', 'PointsTo' ); |
||
|
2 ignored issues
–
show
|
|||
| 37 | private static $searchable_fields = array( 'Phrase' ); |
||
|
2 ignored issues
–
show
|
|||
| 38 | private static $singular_name = 'Automated Link'; |
||
|
2 ignored issues
–
show
|
|||
| 39 | private static $plural_name = 'Automated Links'; |
||
|
2 ignored issues
–
show
|
|||
| 40 | private static $parsableFields = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the url of the page linked to this object |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public function PointsTo(){ |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return the phrase set for this object |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function Title(){ |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Return the rendered version of this object |
||
| 62 | * |
||
| 63 | * @return String |
||
| 64 | */ |
||
| 65 | public function forTemplate(){ |
||
| 68 | |||
| 69 | function canView( $member = false ){ |
||
| 72 | |||
| 73 | function canEdit( $member = false ){ |
||
| 76 | |||
| 77 | function canDelete( $member = false ){ |
||
| 80 | |||
| 81 | function canCreate( $member = false ){ |
||
| 84 | |||
| 85 | function providePermissions() { |
||
| 93 | |||
| 94 | public function requireDefaultRecords(){ |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns the HTML Representation of this object |
||
| 105 | * |
||
| 106 | * @param String $originalPhrase |
||
| 107 | * @return String |
||
| 108 | */ |
||
| 109 | public function getHTML($originalPhrase = NULL) { |
||
| 118 | |||
| 119 | public function getCMSFields() { |
||
| 163 | |||
| 164 | public function getCMSValidator() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Save the Global Settings into the |
||
| 170 | * Global Auto Link Settings Object |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function onBeforeWrite() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Checks if the destination is a redirector page if so |
||
| 195 | * it updates it to the destination of the redirector page |
||
| 196 | * |
||
| 197 | * @Boolean $write - Write the changes if any |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | public function CheckAndUpdateDestination( $write = false ){ |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Checks if the field is parable |
||
| 216 | * |
||
| 217 | * @param SiteTree $page |
||
| 218 | * @param String $field |
||
| 219 | * @return Boolean |
||
| 220 | */ |
||
| 221 | public static function isFieldParsable(SiteTree $page, $field) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Checks if this link can be added to the provided |
||
| 237 | * page and field |
||
| 238 | * |
||
| 239 | * @param ContentController $controller |
||
| 240 | * @return Boolean |
||
| 241 | */ |
||
| 242 | public function canBeAdded( ContentController $controller ){ |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Turn the string passed into a DOMDocument object |
||
| 248 | * |
||
| 249 | * @param string $html |
||
| 250 | * @return DOMDocument |
||
| 251 | */ |
||
| 252 | public static function constructDOMDocument($html){ |
||
| 270 | } |
||
| 271 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.