Complex classes like BaseWidget 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 BaseWidget, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | abstract class BaseWidget |
||
7 | { |
||
8 | protected $template = null; |
||
9 | protected $minifyOutput = true; |
||
10 | protected $cacheLifeTime = 'env_default'; |
||
11 | protected $contextAs = '$data'; |
||
12 | protected $presenter = 'default'; |
||
13 | protected $controller = null; |
||
14 | protected $cacheTags = null; |
||
15 | private $html; |
||
16 | private $viewData; |
||
17 | |||
18 | /** |
||
19 | * BaseWidget constructor. |
||
20 | */ |
||
21 | public function __construct() |
||
30 | |||
31 | /** |
||
32 | * Figures out which method should be called as the controller. |
||
33 | * @return null |
||
34 | */ |
||
35 | private function normalizeControllerMethod() |
||
54 | |||
55 | /** |
||
56 | * Figures out which method should be called as the presenter |
||
57 | * @return null |
||
58 | */ |
||
59 | private function normalizePresenterName() |
||
77 | |||
78 | /** |
||
79 | * Figures out which template to render. |
||
80 | * @return null |
||
81 | */ |
||
82 | private function normalizeTemplateName() |
||
97 | |||
98 | /** |
||
99 | * Figures out what the variable name should be in view file. |
||
100 | * @return null |
||
101 | */ |
||
102 | private function normalizeContextAs() |
||
107 | |||
108 | /** |
||
109 | * ّFigures out how long the cache life time should be. |
||
110 | * @return null |
||
111 | */ |
||
112 | private function normalizeCacheLifeTime() |
||
118 | |||
119 | /** |
||
120 | * ّFigures out what the cache tags should be. |
||
121 | * @return null |
||
122 | */ |
||
123 | private function normalizeCacheTags() |
||
135 | |||
136 | /** |
||
137 | * Determine whether cache tags should be applied or not |
||
138 | * @return bool |
||
139 | */ |
||
140 | private function cacheShouldBeTagged() |
||
144 | |||
145 | /** |
||
146 | * This method is called when you try to invoke the object like a function in blade files. |
||
147 | * like this : {!! $myWidgetObj('param1') !!} |
||
148 | * @param array $args |
||
149 | * @return string |
||
150 | */ |
||
151 | public function __invoke(...$args) |
||
155 | |||
156 | /** |
||
157 | * @param array $args |
||
158 | * @return string |
||
159 | */ |
||
160 | private function renderWidget(...$args) |
||
169 | |||
170 | /** |
||
171 | * It tries to get the html from cache if possible, otherwise generates it. |
||
172 | * @param array ...$args |
||
173 | * @return string |
||
174 | */ |
||
175 | private function generateHtml(...$args) |
||
194 | |||
195 | /** |
||
196 | * @param $args |
||
197 | * @return null |
||
198 | */ |
||
199 | private function prepareDataForView($args) |
||
212 | |||
213 | private function renderTemplate() |
||
231 | |||
232 | /** |
||
233 | * @return bool |
||
234 | */ |
||
235 | private function widgetShouldBeMinified() |
||
239 | |||
240 | /** |
||
241 | * @return null |
||
242 | */ |
||
243 | private function minifyHtml() |
||
258 | |||
259 | private function addIdentifierToHtml() |
||
264 | |||
265 | /** |
||
266 | * Generates a string of current cache configurations. |
||
267 | * @return string |
||
268 | */ |
||
269 | private function cacheState() |
||
276 | |||
277 | /** |
||
278 | * @return bool |
||
279 | */ |
||
280 | private function widgetShouldUseCache() |
||
292 | |||
293 | /** |
||
294 | * @param $arg |
||
295 | * @return string |
||
296 | */ |
||
297 | private function makeCacheKey($arg) |
||
301 | |||
302 | private function cacheResult($key, $phpCode) |
||
318 | |||
319 | /** |
||
320 | * This method is called when you try to print the object like an string in blade files. |
||
321 | * like this : {!! $myWidgetObj !!} |
||
322 | */ |
||
323 | public function __toString() |
||
327 | |||
328 | private function addDebugInfo() |
||
332 | |||
333 | private function addHtmlComments() |
||
337 | |||
338 | } |
||
339 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: