Total Complexity | 55 |
Total Lines | 229 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TActiveRecordAction 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.
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 TActiveRecordAction, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class TActiveRecordAction extends TShellAction |
||
30 | { |
||
31 | protected $action = 'activerecord'; |
||
32 | protected $methods = ['generate', 'generate-all']; |
||
33 | protected $parameters = [['table', 'output'], ['output']]; |
||
34 | protected $optional = [['soap', 'overwrite'], ['soap', 'overwrite', 'prefix', 'suffix']]; |
||
35 | protected $description = [ |
||
36 | 'Provides Active Record skeleton generation.', |
||
37 | 'Generate Active Record skeleton for <table> to <output>. May also generate [soap] properties.', |
||
38 | "Generate Active Record skeleton for all Tables to <output>. May also generate [soap] properties.\nGenerated Classes are named like the Table with optional [Prefix] and/or [Suffix]. [Overwrite] is used to overwrite existing Files.", |
||
39 | ]; |
||
40 | private $_soap = false; |
||
41 | private $_overwrite = false; |
||
42 | |||
43 | private $_soapall = false; |
||
44 | private $_overwriteall = false; |
||
45 | private $_prefix = ''; |
||
46 | private $_postfix = ''; |
||
47 | |||
48 | |||
49 | |||
50 | /** |
||
51 | * This is the Shell Command for Generating all Action Record table skeletons |
||
52 | * @param array $args parameters |
||
53 | * @return bool is the action handled |
||
54 | */ |
||
55 | public function actionGenerateAll($args) |
||
56 | { |
||
57 | $app_dir = Prado::getApplication()->getBasePath(); |
||
58 | $this->_soapall = count($args) > 2 ? ($args[2] == "soap" || $args[2] == "true" ? true : false) : false; |
||
59 | $this->_overwriteall = count($args) > 3 ? ($args[3] == "overwrite" || $args[3] == "true" ? true : false) : false; |
||
60 | $this->_prefix = count($args) > 4 ? $args[4] : ''; |
||
61 | $this->_postfix = count($args) > 5 ? $args[5] : ''; |
||
62 | |||
63 | if ($app_dir !== false) { |
||
|
|||
64 | $config = $this->getActiveRecordConfig(); |
||
65 | |||
66 | $manager = TActiveRecordManager::getInstance(); |
||
67 | $con = $manager->getDbConnection(); |
||
68 | $con->setActive(true); |
||
69 | $command = null; |
||
70 | |||
71 | switch ($con->getDriverName()) { |
||
72 | case 'mysqli': |
||
73 | case 'mysql': |
||
74 | $command = $con->createCommand("SHOW TABLES"); |
||
75 | break; |
||
76 | case 'sqlite': //sqlite 3 |
||
77 | case 'sqlite2': //sqlite 2 |
||
78 | $command = $con->createCommand("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'"); |
||
79 | break; |
||
80 | case 'pgsql': |
||
81 | case 'mssql': // Mssql driver on windows hosts |
||
82 | case 'sqlsrv': // sqlsrv driver on windows hosts |
||
83 | case 'dblib': // dblib drivers on linux (and maybe others os) hosts |
||
84 | case 'oci': |
||
85 | // case 'ibm': |
||
86 | default: |
||
87 | $this->_outWriter->writeError("Sorry, generateAll is not implemented for " . $con->getDriverName() . "."); |
||
88 | } |
||
89 | |||
90 | $dataReader = $command->query(); |
||
91 | $dataReader->bindColumn(1, $table); |
||
92 | $tables = []; |
||
93 | while ($dataReader->read() !== false) { |
||
94 | $tables[] = $table; |
||
95 | } |
||
96 | $con->setActive(false); |
||
97 | foreach ($tables as $key => $table) { |
||
98 | $output = $args[1] . "." . $this->_prefix . ucfirst($table) . $this->_postfix; |
||
99 | if ($config !== false && $output !== false) { |
||
100 | $this->generate("generate " . $table . " " . $output . " " . $this->_soapall . " " . $this->_overwriteall); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | return true; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $l commandline |
||
109 | */ |
||
110 | public function generate($l) |
||
111 | { |
||
112 | $input = explode(" ", trim($l)); |
||
113 | if (count($input) > 2) { |
||
114 | $app_dir = dirname(Prado::getApplication()->getBasePath()); |
||
115 | $args = [$input[0], $input[1], $input[2]]; |
||
116 | if (count($input) > 3) { |
||
117 | $args[] = 'soap'; |
||
118 | } |
||
119 | if (count($input) > 4) { |
||
120 | $args[] = 'overwrite'; |
||
121 | } |
||
122 | $this->actionGenerate($args); |
||
123 | } else { |
||
124 | $this->_outWriter->writeLine("\n Usage: generate table_name Application.pages.RecordClassName"); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | |||
129 | /** |
||
130 | * This is the Shell Command for Generating a specific Action Record table skeleton |
||
131 | * @param array $args parameters |
||
132 | * @return bool is the action handled |
||
133 | */ |
||
134 | public function actionGenerate($args) |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * gets the TActiveRecordConfig for the application |
||
150 | * @return false|TActiveRecordConfig |
||
151 | */ |
||
152 | protected function getActiveRecordConfig() |
||
153 | { |
||
154 | foreach (Prado::getApplication()->getModules() as $module) { |
||
155 | if ($module instanceof TActiveRecordConfig) { |
||
156 | return $module; |
||
157 | } |
||
158 | } |
||
159 | return null; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param string $namespace output file in namespace format |
||
164 | * @return false|string |
||
165 | */ |
||
166 | protected function getOutputFile($namespace) |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param TActiveRecordConfig $config database configuration |
||
184 | * @param string $tablename table name |
||
185 | * @param string $output output file name |
||
186 | * @return bool |
||
187 | */ |
||
188 | protected function generateActiveRecord($config, $tablename, $output) |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param string $field php variable name |
||
216 | * @param \Prado\Data\Common\TDbTableColumn $column database column name |
||
217 | * @return string |
||
218 | */ |
||
219 | protected function generateProperty($field, $column) |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param array $properties class varibles |
||
240 | * @param string $tablename database table name |
||
241 | * @param string $class php class name |
||
242 | * @return string |
||
243 | */ |
||
244 | protected function generateClass($properties, $tablename, $class) |
||
258 | |||
259 | public static function finder(\$className=__CLASS__) |
||
268 |