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 | /** |
||
11 | * |
||
12 | * @var int |
||
13 | */ |
||
14 | private static $thumbnail_size = 140; |
||
15 | |||
16 | /** |
||
17 | * |
||
18 | * @var int |
||
19 | */ |
||
20 | private static $width = 0; |
||
21 | |||
22 | /** |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | private static $height = 0; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | private static $show_title = false; |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | private static $show_description = false; |
||
39 | |||
40 | /** |
||
41 | * must be a string as booleans dont work well on configs |
||
42 | * @varchar yes / no |
||
43 | */ |
||
44 | private static $resize_images = "yes"; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | public static function recommended_image_size_statement() |
||
51 | { |
||
52 | $array = array(); |
||
53 | if (Config::inst()->get("Advertisement", "width")) { |
||
54 | $array[] = "width = ".Config::inst()->get("Advertisement", "width")."px"; |
||
55 | } |
||
56 | if (Config::inst()->get("Advertisement", "height")) { |
||
57 | $array[] = "height = ".Config::inst()->get("Advertisement", "height")."px"; |
||
58 | } |
||
59 | $count = count($array); |
||
60 | if ($count == 0) { |
||
61 | return _t("Advertisement.NO_RECOMMENDED_SIZE_HAS_BEEN_SET", "No recommeded image size has been set."); |
||
62 | } else { |
||
63 | return _t("Advertisement.RECOMMENDED_SIZE", "Recommended Size").": ".implode(" "._t("Advertisement.AND", "and")." ", $array)."."; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | |||
68 | private static $db = array( |
||
69 | "Title" => "Varchar(255)", |
||
70 | "ExternalLink" => "Varchar(150)", |
||
71 | "Description" => "Text", |
||
72 | "Sort" => "Int" |
||
73 | ); |
||
74 | |||
75 | private static $has_one = array( |
||
76 | "AdvertisementImage" => "Image", |
||
77 | "LinkedPage" => "SiteTree", |
||
78 | "AdditionalImage" => "Image" |
||
79 | ); |
||
80 | |||
81 | private static $belongs_many_many = array( |
||
82 | "Parents" => "SiteTree", |
||
83 | ); |
||
84 | |||
85 | private static $casting = array( |
||
86 | "FullTitle" => "HTMLText", |
||
87 | "Link" => "Varchar", |
||
88 | "GroupID" => "Int" |
||
89 | ); |
||
90 | |||
91 | private static $defaults = array( |
||
92 | "Sort" => 1000 |
||
93 | ); |
||
94 | |||
95 | private static $default_sort = "\"Sort\" ASC, \"Title\" ASC"; |
||
96 | |||
97 | private static $searchable_fields = array( |
||
98 | "Title" => "PartialMatchFilter" |
||
99 | ); |
||
100 | |||
101 | private static $singular_name = "Advertisement"; |
||
102 | |||
103 | private static $plural_name = "Advertisements"; |
||
104 | |||
105 | private static $summary_fields = array( |
||
106 | "FullTitle" => "Image", |
||
107 | "Link" => "Link" |
||
108 | ); |
||
109 | |||
110 | |||
111 | /** |
||
112 | * @alias getLink |
||
113 | * @return string |
||
114 | */ |
||
115 | public function Link() |
||
119 | |||
120 | |||
121 | /** |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getLink() |
||
137 | |||
138 | |||
139 | /** |
||
140 | * |
||
141 | * @return HTMLText |
||
142 | */ |
||
143 | public function getFullTitle($additionalStyle = '') |
||
158 | |||
159 | /** |
||
160 | * |
||
161 | * @return HTMLText |
||
162 | */ |
||
163 | public function FullTitle() |
||
167 | |||
168 | /** |
||
169 | * |
||
170 | * @return int | null |
||
171 | */ |
||
172 | public function getGroupID() |
||
181 | |||
182 | /** |
||
183 | * |
||
184 | * @return int | null |
||
185 | */ |
||
186 | public function GroupID() |
||
190 | |||
191 | public function getCMSFields() |
||
257 | |||
258 | public function onBeforeWrite() |
||
266 | |||
267 | |||
268 | public function requireDefaultRecords() |
||
273 | |||
274 | |||
275 | /** |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | protected function callbackFilterFunctionForMultiSelect() |
||
294 | |||
295 | public function Image() |
||
300 | |||
301 | /** |
||
302 | * |
||
303 | * @return Image | null |
||
304 | */ |
||
305 | public function ResizedAdvertisementImage() |
||
342 | |||
343 | public function ThinyThumb() |
||
347 | |||
348 | /** |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function ShowTitle() |
||
356 | |||
357 | /** |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function ShowDescription() |
||
365 | } |
||
366 |
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.