Complex classes like ResourceMapTrait 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 ResourceMapTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait ResourceMapTrait |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $sTitle; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $aResources = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return string |
||
| 26 | * @throws ExceptionSitemap |
||
| 27 | */ |
||
| 28 | abstract public function getModulePath(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $sTitle |
||
| 32 | */ |
||
| 33 | 1 | public function setTitle($sTitle) { |
|
| 36 | |||
| 37 | /** |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | 20 | public function getTitle() { |
|
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * @param array $aResources |
||
| 47 | */ |
||
| 48 | 1 | public function setResources($aResources) { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param ResourceMapTrait $oMap |
||
|
|
|||
| 54 | */ |
||
| 55 | 22 | protected function mergeResources($oMap) { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @param $sVar |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | 1 | public function removeHeader($sVar) { |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @param $sVar |
||
| 91 | * @param $sVal |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | 1 | public function addHeader($sVar, $sVal) { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param $sVar |
||
| 100 | * @param $sVal |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | 1 | public function addSetting($sVar, $sVal) { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $sPath |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | private function getResourcePath($sPath) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $sPath |
||
| 125 | * @param string $sMedia |
||
| 126 | */ |
||
| 127 | 1 | public function addStyle($sPath, $sMedia = 'screen') { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Adds a path for a JavaScript resource |
||
| 133 | * @param string $sPath |
||
| 134 | * @param bool $bInHead |
||
| 135 | */ |
||
| 136 | 2 | public function addScript($sPath, $bInHead = false) { |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param string $sType The type of the link element (eg, application/rss+xml or image/png) |
||
| 143 | * @param string $aData The rest of the link's attributes (href, rel, s/a) |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | 1 | public function addLink($sType, $aData) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $sName |
||
| 158 | * @param string $sValue |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | 2 | public function addMeta($sName, $sValue) { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $sType |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 20 | public function getResources($sType = null) { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param null $sMedia |
||
| 185 | * @return array|null |
||
| 186 | */ |
||
| 187 | 1 | public function getStyles($sMedia = null) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param null $sName |
||
| 199 | * @return array|string |
||
| 200 | */ |
||
| 201 | 3 | public function getMetas($sName = null) { |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param bool $bInHead |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 1 | public function getScripts($bInHead = false) { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | 1 | public function getSettings() { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $sType |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | 2 | public function getLinks($sType = null) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $sVar |
||
| 253 | * @return array|string |
||
| 254 | */ |
||
| 255 | 1 | public function getSetting($sVar) { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | 2 | public function getHeaders() { |
|
| 271 | |||
| 272 | } |
||
| 273 |