Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Helper |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var ServiceManager |
||
| 34 | */ |
||
| 35 | protected $serviceManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \HaaseIT\HCSF\HelperConfig |
||
| 39 | */ |
||
| 40 | protected $config; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $core = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $secrets = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $shop = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Helper constructor. |
||
| 59 | * @param ServiceManager $serviceManager |
||
| 60 | */ |
||
| 61 | public function __construct(ServiceManager $serviceManager) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $target |
||
| 72 | * @param bool $replace |
||
| 73 | * @param int $http_response_header |
||
| 74 | * @return void|false |
||
| 75 | */ |
||
| 76 | public function redirectToPage($target = '', $replace = false, $http_response_header = 302) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $message |
||
| 88 | */ |
||
| 89 | public function terminateScript($message = '') |
||
| 93 | |||
| 94 | public function formatNumber($number) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $file |
||
| 106 | * @param int $width |
||
| 107 | * @param int $height |
||
| 108 | * @return bool|string |
||
| 109 | */ |
||
| 110 | public function getSignedGlideURL($file, $width = 0, $height = 0) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param $to |
||
| 133 | * @param string $subject |
||
| 134 | * @param string $message |
||
| 135 | * @param array $aImagesToEmbed |
||
| 136 | * @param array $aFilesToAttach |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public function mailWrapper($to, $subject = '(No subject)', $message = '', $aImagesToEmbed = [], $aFilesToAttach = []) { |
||
| 187 | |||
| 188 | // don't remove this, this is the fallback for unavailable twig functions |
||
| 189 | /** |
||
| 190 | * @param $string |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | public function reachThrough($string) { |
||
| 196 | // don't remove this, this is the fallback for unavailable twig functions |
||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function returnEmptyString() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param array $aP |
||
| 206 | * @param Page $P |
||
| 207 | */ |
||
| 208 | public function getDebug($aP, $P) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $purpose |
||
| 224 | * @return bool|\HTMLPurifier |
||
| 225 | */ |
||
| 226 | public function getPurifier($purpose) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param $callback |
||
| 263 | * @param $parameters |
||
| 264 | * @return bool|mixed |
||
| 265 | */ |
||
| 266 | public function twigCallback($callback, $parameters) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param \Doctrine\DBAL\Connection $dbal |
||
| 284 | * @param string $table |
||
| 285 | * @param array $data |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function autoInsert(\Doctrine\DBAL\Connection $dbal, $table, array $data) |
||
| 304 | } |
||
| 305 |