Complex classes like DevController 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 DevController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class DevController extends Controller |
||
26 | { |
||
27 | public $defaultAction = 'all'; |
||
28 | |||
29 | /** |
||
30 | * @var bool whether to use HTTP when cloning github repositories |
||
31 | */ |
||
32 | public $useHttp = false; |
||
33 | |||
34 | public $apps = [ |
||
35 | 'basic' => '[email protected]:yiisoft/yii2-app-basic.git', |
||
36 | 'advanced' => '[email protected]:yiisoft/yii2-app-advanced.git', |
||
37 | 'benchmark' => '[email protected]:yiisoft/yii2-app-benchmark.git', |
||
38 | ]; |
||
39 | |||
40 | public $extensions = [ |
||
41 | 'apidoc' => '[email protected]:yiisoft/yii2-apidoc.git', |
||
42 | 'authclient' => '[email protected]:yiisoft/yii2-authclient.git', |
||
43 | 'bootstrap' => '[email protected]:yiisoft/yii2-bootstrap.git', |
||
44 | 'codeception' => '[email protected]:yiisoft/yii2-codeception.git', |
||
45 | 'composer' => '[email protected]:yiisoft/yii2-composer.git', |
||
46 | 'debug' => '[email protected]:yiisoft/yii2-debug.git', |
||
47 | 'elasticsearch' => '[email protected]:yiisoft/yii2-elasticsearch.git', |
||
48 | 'faker' => '[email protected]:yiisoft/yii2-faker.git', |
||
49 | 'gii' => '[email protected]:yiisoft/yii2-gii.git', |
||
50 | 'httpclient' => '[email protected]:yiisoft/yii2-httpclient.git', |
||
51 | 'imagine' => '[email protected]:yiisoft/yii2-imagine.git', |
||
52 | 'jui' => '[email protected]:yiisoft/yii2-jui.git', |
||
53 | 'mongodb' => '[email protected]:yiisoft/yii2-mongodb.git', |
||
54 | 'queue' => '[email protected]:yiisoft/yii2-queue.git', |
||
55 | 'redis' => '[email protected]:yiisoft/yii2-redis.git', |
||
56 | 'shell' => '[email protected]:yiisoft/yii2-shell.git', |
||
57 | 'smarty' => '[email protected]:yiisoft/yii2-smarty.git', |
||
58 | 'sphinx' => '[email protected]:yiisoft/yii2-sphinx.git', |
||
59 | 'swiftmailer' => '[email protected]:yiisoft/yii2-swiftmailer.git', |
||
60 | 'twig' => '[email protected]:yiisoft/yii2-twig.git', |
||
61 | ]; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Install all extensions and advanced + basic app. |
||
66 | */ |
||
67 | public function actionAll() |
||
89 | |||
90 | /** |
||
91 | * Runs a command in all extension and application directories. |
||
92 | * |
||
93 | * Can be used to run e.g. `git pull`. |
||
94 | * |
||
95 | * ./build/build dev/run git pull |
||
96 | * |
||
97 | * @param string $command the command to run |
||
98 | */ |
||
99 | public function actionRun($command) |
||
119 | |||
120 | /** |
||
121 | * This command installs a project template in the `apps` directory and links the framework and extensions. |
||
122 | * |
||
123 | * It basically runs the following commands in the dev repo root: |
||
124 | * |
||
125 | * - Run `composer update` |
||
126 | * - `rm -rf apps/basic/vendor/yiisoft/yii2` |
||
127 | * - `rm -rf apps/basic/vendor/yiisoft/yii2-*` |
||
128 | * |
||
129 | * And replaces them with symbolic links to the extensions and framework path in the dev repo. |
||
130 | * |
||
131 | * Extensions required by the application are automatically installed using the `ext` action. |
||
132 | * |
||
133 | * @param string $app the application name e.g. `basic` or `advanced`. |
||
134 | * @param string $repo url of the git repo to clone if it does not already exist. |
||
135 | * @return int return code |
||
136 | */ |
||
137 | public function actionApp($app, $repo = null) |
||
179 | |||
180 | /** |
||
181 | * This command installs an extension in the `extensions` directory and links the framework and other extensions. |
||
182 | * |
||
183 | * @param string $extension the application name e.g. `basic` or `advanced`. |
||
184 | * @param string $repo url of the git repo to clone if it does not already exist. |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | public function actionExt($extension, $repo = null) |
||
230 | |||
231 | /** |
||
232 | * @inheritdoc |
||
233 | */ |
||
234 | public function options($actionID) |
||
243 | |||
244 | |||
245 | /** |
||
246 | * Remove all symlinks in the vendor subdirectory of the directory specified. |
||
247 | * @param string $dir base directory |
||
248 | */ |
||
249 | protected function cleanupVendorDir($dir) |
||
263 | |||
264 | /** |
||
265 | * Creates symlinks to framework and extension sources for the application. |
||
266 | * @param string $dir application directory |
||
267 | * @param string $base Yii sources base directory |
||
268 | * |
||
269 | * @return int |
||
270 | */ |
||
271 | protected function linkFrameworkAndExtensions($dir, $base) |
||
295 | |||
296 | /** |
||
297 | * Get a list of subdirectories for directory specified. |
||
298 | * @param string $dir directory to read |
||
299 | * |
||
300 | * @return array list of subdirectories |
||
301 | */ |
||
302 | protected function listSubDirs($dir) |
||
324 | |||
325 | /** |
||
326 | * Finds linkable applications. |
||
327 | * |
||
328 | * @param string $dir directory to search in |
||
329 | * @return array list of applications command can link |
||
330 | */ |
||
331 | protected function findDirs($dir) |
||
357 | } |
||
358 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.