Complex classes like Config 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 Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Config |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public static $version = "1.4.8"; |
||
35 | |||
36 | /** |
||
37 | * String that separates the parent section name |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $sectionSeparator = ':'; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $configIniDefault = '/configs/config.ini'; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | public $_basePath; |
||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $argv = array (); |
||
56 | |||
57 | /** |
||
58 | * @var \Classes\AdapterConfig\AbstractAdapter |
||
59 | */ |
||
60 | private $adapterConfig; |
||
61 | |||
62 | /** |
||
63 | * @var \Classes\AdaptersDriver\AbsractAdapter |
||
64 | */ |
||
65 | private $adapterDriver; |
||
66 | |||
67 | private $frameworkList = array ( |
||
68 | 'none' , |
||
69 | 'zf1' , |
||
70 | 'phalcon' |
||
71 | ); |
||
72 | |||
73 | private $parameterList = array ( |
||
74 | 'init' => 'Creates the necessary configuration file to start using the orm-generator.' , |
||
75 | 'name-ini' => 'reference to another .ini file configuration (relative path).' , |
||
76 | 'config-env' => 'orm-generator configuration environment.' , |
||
77 | 'framework' => 'name framework used, which has the contents of the database configurations and framework template.' , |
||
78 | 'driver' => 'database driver name (Ex.: pgsql).' , |
||
79 | 'database' => 'database name.' , |
||
80 | 'schema' => 'database schema name (one or more than one).' , |
||
81 | 'tables' => 'table name (parameter can be used more then once).' , |
||
82 | 'clean-trash' => 'delete all files that do not belong to your Database due' , |
||
83 | 'status' => 'show status of implementation carried out after completing the process.' , |
||
84 | 'version' => 'shows the version of orm-generator.' , |
||
85 | 'help' => "help command explaining all the options and manner of use." , |
||
86 | 'path' => "specify where to create the files (default is current directory)." , |
||
87 | ); |
||
88 | |||
89 | public function __construct ( $argv , $basePath , $numArgs ) |
||
108 | |||
109 | /** |
||
110 | * Lista de ajuda quando digita 'help' |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function getUsage () |
||
127 | |||
128 | public function renderParam () |
||
145 | |||
146 | public function checkHasNewVersion () |
||
169 | |||
170 | public function getVersion () |
||
176 | |||
177 | /** |
||
178 | * Analisa e estrutura a Configuracao do generate |
||
179 | * |
||
180 | * @param string $basePath |
||
181 | * @param array $argv |
||
182 | * |
||
183 | * @return array |
||
184 | * @throws \Exception |
||
185 | */ |
||
186 | private function parseConfig ( $basePath , $argv ) |
||
212 | |||
213 | /** |
||
214 | * |
||
215 | * @param $configTemp |
||
216 | * @param $argv |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | private static function parseConfigEnv ( $configTemp , $argv ) |
||
239 | |||
240 | /** |
||
241 | * Carregar o arquivo ini e pré-processa o separador de seção ':' |
||
242 | * no nome da seção (que é usado para a extensão seção) de modo a que a |
||
243 | * matriz resultante tem os nomes de seção corretos e as informações de |
||
244 | * extensão é armazenado em uma sub-ch ve |
||
245 | * |
||
246 | * @param string $filename |
||
247 | * |
||
248 | * @throws \Exception |
||
249 | * @return array |
||
250 | */ |
||
251 | protected function loadIniFile ( $filename ) |
||
282 | |||
283 | /** |
||
284 | * analisa a opção e cria a instancia do Atapter do determinado framework |
||
285 | * |
||
286 | * @return \Classes\AdapterConfig\AbstractAdapter |
||
287 | * |
||
288 | */ |
||
289 | private function factoryConfig () |
||
302 | |||
303 | /** |
||
304 | * Analisa a opção e instancia o determinado banco de dados |
||
305 | * |
||
306 | * @param AdapterConfig\AbstractAdapter $config |
||
307 | * |
||
308 | * @return AdaptersDriver\AbsractAdapter |
||
309 | */ |
||
310 | private function factoryDriver ( AdapterConfig\AbstractAdapter $config ) |
||
328 | |||
329 | /** |
||
330 | * @return AdapterConfig\AbstractAdapter |
||
331 | */ |
||
332 | public function getAdapterConfig () |
||
341 | |||
342 | /** |
||
343 | * @return AdaptersDriver\AbsractAdapter |
||
344 | */ |
||
345 | public function getAdapterDriver ( AdapterConfig\AbstractAdapter $config ) |
||
354 | |||
355 | } |
||
356 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.