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 |
||
22 | class NginxController extends \hidev\controllers\AbstractController |
||
23 | { |
||
24 | use \hiqdev\yii2\collection\ManagerTrait; |
||
25 | |||
26 | protected $_logDir; |
||
27 | protected $_etcDir; |
||
28 | protected $_fpmSocket; |
||
29 | |||
30 | public $defaultClass = VhostController::class; |
||
31 | |||
32 | public function actionDump() |
||
39 | |||
40 | public function actionDeploy() |
||
59 | |||
60 | public function actionStart() |
||
64 | |||
65 | public function actionStop() |
||
69 | |||
70 | public function actionReload() |
||
74 | |||
75 | public function actionRestart() |
||
79 | |||
80 | public function actionStatus() |
||
84 | |||
85 | public function actionPerform($operation, $sudo = true) |
||
94 | |||
95 | public function actionLetsencrypt($aliases = []) |
||
126 | |||
127 | public function actionChmodSsl() |
||
133 | |||
134 | public static function mkdir($path) |
||
142 | |||
143 | /** |
||
144 | * Prepares item config. |
||
145 | */ |
||
146 | public function getItemConfig($name = null, array $config = []) |
||
154 | |||
155 | public function createItem($id, $config = []) |
||
159 | |||
160 | public function setLogDir($value) |
||
164 | |||
165 | public function getLogDir() |
||
173 | |||
174 | public function setEtcDir($value) |
||
178 | |||
179 | public function getEtcDir() |
||
187 | |||
188 | public function findEtcDir() |
||
199 | |||
200 | public function setFpmSocket($value) |
||
204 | |||
205 | public function getFpmSocket() |
||
213 | |||
214 | public function findFpmSocketFile() |
||
225 | } |
||
226 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.