Complex classes like NginxController 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 NginxController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class NginxController extends \hidev\controllers\CommonController |
||
22 | { |
||
23 | use \hiqdev\yii2\collection\ManagerTrait; |
||
24 | |||
25 | protected $_logDir; |
||
26 | protected $_etcDir; |
||
27 | protected $_fpmSocket; |
||
28 | |||
29 | public $defaultClass = VhostController::class; |
||
30 | |||
31 | public function actionDoDump() |
||
32 | { |
||
33 | foreach ($this->getItems() as $vhost) { |
||
34 | $conf = $vhost->renderConf(); |
||
35 | file_put_contents($vhost->getDomain() . '.conf', $conf); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | public function actionDump() |
||
40 | { |
||
41 | return $this->perform('do-dump'); |
||
42 | } |
||
43 | |||
44 | public function actionDeploy($aliases = []) |
||
48 | |||
49 | public function actionDoDeploy() |
||
68 | |||
69 | public function actionStart() |
||
73 | |||
74 | public function actionStop() |
||
78 | |||
79 | public function actionReload() |
||
83 | |||
84 | public function actionRestart() |
||
88 | |||
89 | public function actionStatus() |
||
93 | |||
94 | public function make($operation, $sudo = true) |
||
103 | |||
104 | public function actionMake() |
||
108 | |||
109 | public function actionLetsencrypt() |
||
113 | |||
114 | public function actionDoLetsencrypt() |
||
132 | |||
133 | public function actionChmodSsl() |
||
139 | |||
140 | public static function mkdir($path) |
||
148 | |||
149 | /** |
||
150 | * Prepares item config. |
||
151 | */ |
||
152 | public function getItemConfig($name = null, array $config = []) |
||
153 | { |
||
154 | return array_merge([ |
||
155 | 'domain' => $name, |
||
156 | 'nginx' => $this, |
||
157 | 'class' => $this->defaultClass, |
||
158 | ], $config); |
||
159 | } |
||
160 | |||
161 | public function createItem($id, $config = []) |
||
162 | { |
||
163 | return Yii::createObject($this->getItemConfig($id, $config), [$id, Yii::$app]); |
||
164 | } |
||
165 | |||
166 | public function setLogDir($value) |
||
170 | |||
171 | public function getLogDir() |
||
172 | { |
||
173 | if ($this->_logDir === null) { |
||
174 | $this->_logDir = '/var/log/nginx'; |
||
175 | } |
||
176 | |||
177 | return $this->_logDir; |
||
178 | } |
||
179 | |||
180 | public function setEtcDir($value) |
||
184 | |||
185 | public function getEtcDir() |
||
193 | |||
194 | public function findEtcDir() |
||
205 | |||
206 | public function setFpmSocket($value) |
||
207 | { |
||
208 | $this->_fpmSocket = $value; |
||
209 | } |
||
210 | |||
211 | public function getFpmSocket() |
||
212 | { |
||
213 | if ($this->_fpmSocket === null) { |
||
214 | $this->_fpmSocket = 'unix:' . $this->findFpmSocketFile(); |
||
215 | } |
||
216 | |||
217 | return $this->_fpmSocket; |
||
218 | } |
||
219 | |||
220 | public function findFpmSocketFile() |
||
231 | } |
||
232 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.