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 SearchForm 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 SearchForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 28 | class SearchForm extends Form { | 
            ||
| 29 | |||
| 30 | /**  | 
            ||
| 31 | * @var int $pageLength How many results are shown per page.  | 
            ||
| 32 | * Relies on pagination being implemented in the search results template.  | 
            ||
| 33 | */  | 
            ||
| 34 | protected $pageLength = 10;  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * Classes to search  | 
            ||
| 38 | */  | 
            ||
| 39 | protected $classesToSearch = array(  | 
            ||
| 40 | "SilverStripe\\CMS\\Model\\SiteTree", "File"  | 
            ||
| 41 | );  | 
            ||
| 42 | |||
| 43 | private static $casting = array(  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 44 | 'SearchQuery' => 'Text'  | 
            ||
| 45 | );  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | *  | 
            ||
| 49 | * @param Controller $controller  | 
            ||
| 50 | * @param string $name The name of the form (used in URL addressing)  | 
            ||
| 51 | * @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized  | 
            ||
| 52 | * if fields are added to the form.  | 
            ||
| 53 | * @param FieldList $actions Optional, defaults to a single field named "Go".  | 
            ||
| 54 | */  | 
            ||
| 55 | 	public function __construct($controller, $name, $fields = null, $actions = null) { | 
            ||
| 78 | |||
| 79 | |||
| 80 | /**  | 
            ||
| 81 | * Return a rendered version of this form.  | 
            ||
| 82 | *  | 
            ||
| 83 | * This is returned when you access a form as $FormObject rather  | 
            ||
| 84 | * than <% with FormObject %>  | 
            ||
| 85 | */  | 
            ||
| 86 | 	public function forTemplate() { | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * Set the classes to search.  | 
            ||
| 100 | * Currently you can only choose from "SiteTree" and "File", but a future version might improve this.  | 
            ||
| 101 | */  | 
            ||
| 102 | 	public function classesToSearch($classes) { | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Get the classes to search  | 
            ||
| 113 | *  | 
            ||
| 114 | * @return array  | 
            ||
| 115 | */  | 
            ||
| 116 | 	public function getClassesToSearch() { | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * Return dataObjectSet of the results using $_REQUEST to get info from form.  | 
            ||
| 122 | 	 * Wraps around {@link searchEngine()}. | 
            ||
| 123 | *  | 
            ||
| 124 | * @param int $pageLength DEPRECATED 2.3 Use SearchForm->pageLength  | 
            ||
| 125 | * @param array $data Request data as an associative array. Should contain at least a key 'Search' with all searched keywords.  | 
            ||
| 126 | * @return SS_List  | 
            ||
| 127 | */  | 
            ||
| 128 | 	public function getResults($pageLength = null, $data = null){ | 
            ||
| 188 | |||
| 189 | 	protected function addStarsToKeywords($keywords) { | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * Get the search query for display in a "You searched for ..." sentence.  | 
            ||
| 209 | *  | 
            ||
| 210 | * @param array $data  | 
            ||
| 211 | * @return string  | 
            ||
| 212 | */  | 
            ||
| 213 | 	public function getSearchQuery($data = null) { | 
            ||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * Set the maximum number of records shown on each page.  | 
            ||
| 223 | *  | 
            ||
| 224 | * @param int $length  | 
            ||
| 225 | */  | 
            ||
| 226 | 	public function setPageLength($length) { | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 | * @return int  | 
            ||
| 232 | */  | 
            ||
| 233 | 	public function getPageLength() { | 
            ||
| 236 | |||
| 237 | }  | 
            ||
| 238 | |||
| 240 |