Complex classes like MappingA 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 MappingA, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class MappingA extends BaseObject { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $sRegex; |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $sPath; |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $sTemplate; |
||
30 | /** |
||
31 | * the local template path - will be used to compose something like |
||
32 | * this->sViewPath . view->typeOfView . this->sTemplate |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $sViewPath; |
||
37 | |||
38 | private $bIsStatic = false; |
||
39 | |||
40 | private $aControllerMaps = array(); |
||
41 | |||
42 | private $aTaintedVars = []; |
||
43 | |||
44 | private $sMatchingUrl; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $iAuthenticationType = null; |
||
50 | |||
51 | /** |
||
52 | * @param MappingA $oMap |
||
53 | */ |
||
54 | abstract protected function mergeResources($oMap); |
||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | abstract public function getTitle(); |
||
59 | /** |
||
60 | * @param string $sTitle |
||
61 | */ |
||
62 | abstract public function setTitle($sTitle); |
||
63 | /** |
||
64 | * @param string $sPath |
||
65 | * @param string $sRegex |
||
66 | */ |
||
67 | 24 | public function __construct($sPath, $sRegex) { |
|
71 | |||
72 | 20 | public function getRegex() { |
|
75 | |||
76 | /** |
||
77 | * @param bool $bStatic |
||
78 | */ |
||
79 | 2 | public function setIsStatic($bStatic) { |
|
82 | |||
83 | /** |
||
84 | * @return bool |
||
85 | */ |
||
86 | 2 | public function isStatic() { |
|
89 | |||
90 | /** |
||
91 | * @param MappingA $oMap |
||
92 | */ |
||
93 | 21 | protected function mergePaths($oMap) { |
|
94 | 21 | $sParentPath = $oMap->getTemplatePath(); |
|
95 | 21 | if (!is_null($sParentPath) && is_null($this->getTemplatePath())) { |
|
96 | 1 | $this->setTemplatePath($sParentPath); |
|
97 | } |
||
98 | |||
99 | 21 | $sParentTemplate = $oMap->getTemplate(); |
|
100 | 21 | if (!is_null($sParentTemplate) && is_null($this->getTemplate())) { |
|
101 | $this->setTemplate($sParentTemplate); |
||
102 | } |
||
103 | |||
104 | 21 | if (($this instanceof ContentTypeMappingInterface) && ($oMap instanceof ContentTypeMappingInterface)) { |
|
105 | /** @var ContentTypeMappingInterface $oMap */ |
||
106 | 20 | $sParentMainTemplatePath = $oMap->getMainTemplatePath(); |
|
107 | 20 | if (is_null($this->getMainTemplatePath())) { |
|
108 | 1 | $this->setMainTemplatePath($sParentMainTemplatePath); |
|
109 | } |
||
110 | 20 | $sParentMainTemplate = $oMap->getMainTemplate(); |
|
111 | 20 | if (is_null($this->getMainTemplate())) { |
|
112 | 1 | $this->setMainTemplate($sParentMainTemplate); |
|
113 | } |
||
114 | } |
||
115 | 21 | } |
|
116 | |||
117 | /** |
||
118 | * @param MappingA $oMap |
||
119 | * @return $this |
||
120 | */ |
||
121 | 21 | public function merge($oMap = null) { |
|
122 | 21 | if (MappingA::isValid($oMap)) { |
|
123 | 21 | $this->mergeResources($oMap); |
|
124 | 21 | $this->mergePaths($oMap); |
|
125 | // maybe I should merge the regexes too like processor_regex . '.*' . controller_regex |
||
126 | |||
127 | 21 | $sTitle = $this->getTitle(); |
|
128 | 21 | $sMapTitle = $oMap->getTitle(); |
|
129 | 21 | if (empty($sTitle) && !empty($sMapTitle)) { |
|
130 | $this->setTitle($sMapTitle); |
||
131 | } |
||
132 | 21 | $this->iAuthenticationType |= $oMap->getAuthenticationType(); |
|
133 | } |
||
134 | 21 | return $this; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param string $sPath |
||
139 | * @return bool |
||
140 | * @throws ExceptionSitemap |
||
141 | */ |
||
142 | 21 | public function setTemplatePath($sPath) { |
|
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | 21 | public function getTemplatePath() { |
|
152 | |||
153 | /** |
||
154 | * @param string $sPath |
||
155 | */ |
||
156 | 18 | public function setTemplate($sPath) { |
|
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | 19 | public function getTemplate() { |
|
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | 24 | public function getPath() { |
|
173 | |||
174 | /** |
||
175 | * @deprecated |
||
176 | * @param string $sRegex |
||
177 | * @param string $sPath |
||
178 | * @throws ExceptionController |
||
179 | * @throws ExceptionSitemap |
||
180 | * @returns ClassMap |
||
181 | */ |
||
182 | public function mapController($sRegex, $sPath = null) { |
||
183 | return $this->map($sRegex, $sPath); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * |
||
188 | * @param string $sRegex |
||
189 | * @param string $sPath |
||
190 | * @throws ExceptionController |
||
191 | * @throws ExceptionSitemap |
||
192 | * @returns ClassMap |
||
193 | */ |
||
194 | 22 | public function map($sRegex, $sPath = null) { |
|
195 | 22 | if (empty($sRegex)) { |
|
196 | throw new ExceptionSitemap('An URI must be present.'); |
||
197 | } |
||
198 | 22 | if (is_null($sPath)) { |
|
199 | // if we only have one parameter, we treat it as a path |
||
200 | 1 | $sPath = $sRegex; |
|
201 | 1 | $sRegex = $this->getRegex(); |
|
202 | } |
||
203 | |||
204 | 22 | $sKey = $sRegex; |
|
205 | 22 | if (array_key_exists($sKey, $this->aControllerMaps)) { |
|
206 | unset($this->aControllerMaps[$sKey]); |
||
207 | } |
||
208 | 22 | if (ClassMap::isValidMap($sPath)) { |
|
209 | // instead of a path we have a namespace |
||
210 | 21 | $oNewMap = new ClassMap($sPath, $sKey); |
|
211 | 21 | $oNewMap->setModuleMap($this); |
|
212 | 21 | $oNewMap->merge($this); |
|
213 | |||
214 | 21 | $this->aControllerMaps[$sKey] = $oNewMap; |
|
215 | |||
216 | 21 | return $oNewMap; |
|
217 | } |
||
218 | 1 | } |
|
219 | |||
220 | /** |
||
221 | * @return MappingA[] |
||
222 | */ |
||
223 | 20 | public function getControllerMaps() { |
|
226 | |||
227 | /** |
||
228 | * @param string[] $aVars |
||
229 | */ |
||
230 | 18 | public function setTaintedVars($aVars) { |
|
233 | |||
234 | 10 | public function getTaintedVars() { |
|
237 | |||
238 | /** |
||
239 | * @param string $sUrl |
||
240 | */ |
||
241 | 19 | public function setUrl($sUrl) { |
|
244 | |||
245 | /** |
||
246 | * @returns Url |
||
247 | */ |
||
248 | 2 | public function getUrl() { |
|
249 | 2 | $sRegex = '#(' . str_replace('#', '\#', $this->getRegex()) . ')#iUu'; |
|
250 | 2 | $bHaveMatch = preg_match($sRegex, $this->sMatchingUrl, $aMatches); |
|
251 | |||
252 | 2 | if ($bHaveMatch) { |
|
253 | 2 | $url = new Url(); |
|
254 | 2 | $url->setPath($aMatches[0]); |
|
255 | 2 | return $url; |
|
256 | } else { |
||
257 | return new Base(); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | 2 | public function setAuthenticationType($iAuthenticationType) { |
|
264 | |||
265 | 22 | public function getAuthenticationType() { |
|
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | 1 | public function getValidAuthenticationSchemas() { |
|
275 | |||
276 | /** |
||
277 | * @return bool |
||
278 | */ |
||
279 | 2 | public function requiresAuthentication() { |
|
282 | |||
283 | /** |
||
284 | * @param Object $mappedObject |
||
285 | * @return boolean |
||
286 | */ |
||
287 | 1 | public function maps(BaseObject $mappedObject) |
|
291 | |||
292 | /** |
||
293 | * @param string $sPath |
||
294 | * @return string |
||
295 | * @throws ExceptionSitemap |
||
296 | */ |
||
297 | 21 | protected function getValidPath($sPath) { |
|
311 | } |
||
312 | |||
313 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.