Complex classes like StockistPage 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 StockistPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class StockistPage extends Page |
||
| 4 | { |
||
| 5 | |||
| 6 | |||
| 7 | /** |
||
| 8 | * @inherited |
||
| 9 | */ |
||
| 10 | private static $icon = 'mysite/images/treeicons/StockistPage'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @inherited |
||
| 14 | */ |
||
| 15 | private static $allowed_children = 'none'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * remove from Site Tree as were using lumberjack |
||
| 19 | * @var boolean |
||
| 20 | */ |
||
| 21 | private static $show_in_sitetree = false; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @inherited |
||
| 25 | */ |
||
| 26 | private static $can_be_root = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @inherited |
||
| 30 | */ |
||
| 31 | private static $default_parent = 'StockistCountryPage'; |
||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Standard SS variable. |
||
| 37 | */ |
||
| 38 | private static $singular_name = "Stockist Page"; |
||
| 39 | public function i18n_singular_name() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Standard SS variable. |
||
| 46 | */ |
||
| 47 | private static $plural_name = "Stockist Pages"; |
||
| 48 | public function i18n_plural_name() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @inherited |
||
| 55 | */ |
||
| 56 | private static $description = 'Individual Stockist Page'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @inherited |
||
| 60 | */ |
||
| 61 | private static $db = array( |
||
| 62 | 'Address' => 'Varchar(255)', |
||
| 63 | 'StreetAddress' => 'Varchar(255)', |
||
| 64 | 'City' => 'Varchar(255)', |
||
| 65 | 'WebAddress' => 'Varchar(255)', |
||
| 66 | 'Email' => 'Varchar(255)', |
||
| 67 | 'Phone' => 'Varchar(255)', |
||
| 68 | 'Fax' => 'Varchar(255)', |
||
| 69 | 'HasPhysicalStore' => 'Boolean', |
||
| 70 | 'HasWebStore' => 'Boolean', |
||
| 71 | 'DefaultZoom' => 'Int' |
||
| 72 | ); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inherited |
||
| 76 | */ |
||
| 77 | private static $has_one = array( |
||
| 78 | "Image" => "Image", |
||
| 79 | "Logo" => "Image" |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @inherited |
||
| 84 | */ |
||
| 85 | private static $many_many = array( |
||
| 86 | "Types" => "StockistPage_Type" |
||
| 87 | ); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @inherited |
||
| 91 | */ |
||
| 92 | private static $defaults = array( |
||
| 93 | 'HasGeoInfo' => true, |
||
| 94 | 'HasPhysicalStore' => true, |
||
| 95 | 'HasLighting' => true, |
||
| 96 | 'HasJewellery' => true, |
||
| 97 | 'DefaultZoom' => 15 |
||
| 98 | ); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @inherited |
||
| 102 | */ |
||
| 103 | private static $casting = array( |
||
| 104 | 'CountryName' => "Varchar", |
||
| 105 | 'CountryCode' => "Varchar", |
||
| 106 | 'DistributorName' => "Varchar", |
||
| 107 | 'PhoneWithoutSpaces' => 'Varchar' |
||
| 108 | ); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @inherited |
||
| 112 | */ |
||
| 113 | public function getCMSFields() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * checks the map point |
||
| 172 | * @inherited |
||
| 173 | */ |
||
| 174 | public function onBeforeWrite() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * checks the map details if it has a map... |
||
| 182 | */ |
||
| 183 | public function createMapPoint() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * |
||
| 209 | * @return String |
||
| 210 | */ |
||
| 211 | public function CustomAjaxInfoWindow() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * provides a links to Google Maps to search for directions |
||
| 218 | * @return String |
||
| 219 | */ |
||
| 220 | public function DirectionsLink() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Obscure all email links in StringField. |
||
| 229 | * Matches mailto:[email protected] as well as [email protected] |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function EncodedEmailLink() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Obscure all email links in StringField. |
||
| 241 | * Matches mailto:[email protected] as well as [email protected] |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function EncodedEmailText() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return Distributor |
||
| 253 | */ |
||
| 254 | public function Distributor() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return Distributor |
||
| 265 | */ |
||
| 266 | public function DistributorName() |
||
| 276 | |||
| 277 | public function CountryName() |
||
| 285 | /** |
||
| 286 | * alias for getPointValues |
||
| 287 | * @return String |
||
| 288 | */ |
||
| 289 | public function PointValues($fieldNameArray = 'LocalityName') |
||
| 293 | |||
| 294 | /** |
||
| 295 | * returns, for example, an array for all the cities |
||
| 296 | * for a stockist (based on their Geo Locations) |
||
| 297 | * NB... values are cached... |
||
| 298 | * |
||
| 299 | * @param string $fieldName |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function getPointValues($fieldNameArray) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return String |
||
| 345 | */ |
||
| 346 | public function CountryCode() |
||
| 363 | |||
| 364 | public function getPhoneWithoutSpaces() |
||
| 368 | |||
| 369 | public function types() |
||
| 373 | } |
||
| 374 | |||
| 391 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.