Complex classes like Advertisement 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 Advertisement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Advertisement extends DataObject |
||
|
|||
9 | { |
||
10 | private static $thumbnail_size = 140; |
||
11 | |||
12 | private static $width = 0; |
||
13 | |||
14 | private static $height = 0; |
||
15 | |||
16 | /** |
||
17 | * must be a string as booleans dont work well on configs |
||
18 | * @varchar yes / no |
||
19 | */ |
||
20 | private static $resize_images = "yes"; |
||
21 | |||
22 | public static function recommended_image_size_statement() |
||
23 | { |
||
24 | $array = array(); |
||
25 | if (Config::inst()->get("Advertisement", "width")) { |
||
26 | $array[] = "width = ".Config::inst()->get("Advertisement", "width")."px"; |
||
27 | } |
||
28 | if (Config::inst()->get("Advertisement", "height")) { |
||
29 | $array[] = "height = ".Config::inst()->get("Advertisement", "height")."px"; |
||
30 | } |
||
31 | $count = count($array); |
||
32 | if ($count == 0) { |
||
33 | return _t("Advertisement.NO_RECOMMENDED_SIZE_HAS_BEEN_SET", "No recommeded image size has been set."); |
||
34 | } else { |
||
35 | return _t("Advertisement.RECOMMENDED_SIZE", "Recommended Size").": ".implode(" "._t("Advertisement.AND", "and")." ", $array)."."; |
||
36 | } |
||
37 | } |
||
38 | |||
39 | private static $db = array( |
||
40 | "Title" => "Varchar(255)", |
||
41 | "ExternalLink" => "Varchar(150)", |
||
42 | "Description" => "Text", |
||
43 | "Sort" => "Int" |
||
44 | ); |
||
45 | |||
46 | private static $has_one = array( |
||
47 | "AdvertisementImage" => "Image", |
||
48 | "LinkedPage" => "SiteTree", |
||
49 | "AdditionalImage" => "Image" |
||
50 | ); |
||
51 | |||
52 | private static $belongs_many_many = array( |
||
53 | "Parents" => "SiteTree", |
||
54 | ); |
||
55 | |||
56 | private static $casting = array( |
||
57 | "FullTitle" => "HTMLText", |
||
58 | "Link" => "Varchar", |
||
59 | "GroupID" => "Int" |
||
60 | ); |
||
61 | |||
62 | private static $defaults = array( |
||
63 | "Sort" => 1000 |
||
64 | ); |
||
65 | |||
66 | private static $default_sort = "\"Sort\" ASC, \"Title\" ASC"; |
||
67 | |||
68 | private static $searchable_fields = array( |
||
69 | "Title" => "PartialMatchFilter" |
||
70 | ); |
||
71 | |||
72 | private static $singular_name = "Advertisement"; |
||
73 | |||
74 | private static $plural_name = "Advertisements"; |
||
75 | |||
76 | private static $summary_fields = array( |
||
77 | "FullTitle" => "Image", |
||
78 | "Link" => "Link" |
||
79 | ); |
||
80 | |||
81 | public function getLink() |
||
93 | |||
94 | public function Link() |
||
98 | |||
99 | public function getFullTitle() |
||
113 | |||
114 | public function FullTitle() |
||
118 | |||
119 | public function getGroupID() |
||
128 | |||
129 | public function GroupID() |
||
133 | |||
134 | public function getCMSFields() |
||
190 | |||
191 | public function onBeforeWrite() |
||
199 | |||
200 | |||
201 | public function requireDefaultRecords() |
||
206 | |||
207 | |||
208 | protected function callbackFilterFunctionForMultiSelect() |
||
223 | |||
224 | public function Image() |
||
229 | |||
230 | |||
231 | //back-up function... |
||
232 | public function ResizedAdvertisementImage() |
||
268 | |||
269 | public function ThinyThumb() |
||
273 | } |
||
274 |
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.