| Total Complexity | 45 |
| Total Lines | 147 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TActiveRecordGenAllAction 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 TActiveRecordGenAllAction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class TActiveRecordGenAllAction extends TShellAction |
||
| 27 | { |
||
| 28 | protected $action = 'generateAll'; |
||
| 29 | protected $parameters = ['output']; |
||
| 30 | protected $optional = ['directory', 'soap', 'overwrite', 'prefix', 'postfix']; |
||
| 31 | protected $description = "Generate Active Record skeleton for all Tables to <output> file using application.xml in [directory]. May also generate [soap] properties.\nGenerated Classes are named like the Table with optional [Prefix] and/or [Postfix]. [Overwrite] is used to overwrite existing Files."; |
||
| 32 | private $_soap = false; |
||
| 33 | private $_prefix = ''; |
||
| 34 | private $_postfix = ''; |
||
| 35 | private $_overwrite = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param array $args parameters |
||
| 39 | * @return bool |
||
| 40 | */ |
||
| 41 | public function performAction($args) |
||
| 42 | { |
||
| 43 | $app_dir = count($args) > 2 ? $this->getAppDir($args[2]) : $this->getAppDir(); |
||
| 44 | $this->_soap = count($args) > 3 ? ($args[3] == "soap" || $args[3] == "true" ? true : false) : false; |
||
| 45 | $this->_overwrite = count($args) > 4 ? ($args[4] == "overwrite" || $args[4] == "true" ? true : false) : false; |
||
| 46 | $this->_prefix = count($args) > 5 ? $args[5] : ''; |
||
| 47 | $this->_postfix = count($args) > 6 ? $args[6] : ''; |
||
| 48 | |||
| 49 | if ($app_dir !== false) { |
||
| 50 | $config = $this->getActiveRecordConfig($app_dir); |
||
| 51 | |||
| 52 | $manager = TActiveRecordManager::getInstance(); |
||
| 53 | $con = $manager->getDbConnection(); |
||
| 54 | $con->Active = true; |
||
|
|
|||
| 55 | $command = null; |
||
| 56 | |||
| 57 | switch ($con->getDriverName()) { |
||
| 58 | case 'mysqli': |
||
| 59 | case 'mysql': |
||
| 60 | $command = $con->createCommand("SHOW TABLES"); |
||
| 61 | break; |
||
| 62 | case 'sqlite': //sqlite 3 |
||
| 63 | case 'sqlite2': //sqlite 2 |
||
| 64 | $command = $con->createCommand("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'"); |
||
| 65 | break; |
||
| 66 | case 'pgsql': |
||
| 67 | case 'mssql': // Mssql driver on windows hosts |
||
| 68 | case 'sqlsrv': // sqlsrv driver on windows hosts |
||
| 69 | case 'dblib': // dblib drivers on linux (and maybe others os) hosts |
||
| 70 | case 'oci': |
||
| 71 | // case 'ibm': |
||
| 72 | default: |
||
| 73 | echo "\n Sorry, generateAll is not implemented for " . $con->getDriverName() . "\n"; |
||
| 74 | |||
| 75 | } |
||
| 76 | |||
| 77 | $dataReader = $command->query(); |
||
| 78 | $dataReader->bindColumn(1, $table); |
||
| 79 | $tables = []; |
||
| 80 | while ($dataReader->read() !== false) { |
||
| 81 | $tables[] = $table; |
||
| 82 | } |
||
| 83 | $con->Active = false; |
||
| 84 | foreach ($tables as $key => $table) { |
||
| 85 | $output = $args[1] . "." . $this->_prefix . ucfirst($table) . $this->_postfix; |
||
| 86 | if ($config !== false && $output !== false) { |
||
| 87 | $this->generate("generate " . $table . " " . $output . " " . $this->_soap . " " . $this->_overwrite); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | return true; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $l commandline |
||
| 96 | */ |
||
| 97 | public function generate($l) |
||
| 98 | { |
||
| 99 | $input = explode(" ", trim($l)); |
||
| 100 | if (count($input) > 2) { |
||
| 101 | $app_dir = '.'; |
||
| 102 | if (Prado::getApplication() !== null) { |
||
| 103 | $app_dir = dirname(Prado::getApplication()->getBasePath()); |
||
| 104 | } |
||
| 105 | $args = [$input[0], $input[1], $input[2], $app_dir]; |
||
| 106 | if (count($input) > 3) { |
||
| 107 | $args[] = 'soap'; |
||
| 108 | } |
||
| 109 | if (count($input) > 4) { |
||
| 110 | $args[] = 'overwrite'; |
||
| 111 | } |
||
| 112 | $cmd = new TActiveRecordGenAction; |
||
| 113 | $cmd->performAction($args); |
||
| 114 | } else { |
||
| 115 | echo "\n Usage: generate table_name Application.pages.RecordClassName\n"; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $dir application directory |
||
| 121 | * @return false|string |
||
| 122 | */ |
||
| 123 | protected function getAppDir($dir = ".") |
||
| 124 | { |
||
| 125 | if (is_dir($dir)) { |
||
| 126 | return realpath($dir); |
||
| 127 | } |
||
| 128 | if (false !== ($app_dir = realpath($dir . '/protected/')) && is_dir($app_dir)) { |
||
| 129 | return $app_dir; |
||
| 130 | } |
||
| 131 | echo '** Unable to find directory "' . $dir . "\".\n"; |
||
| 132 | return false; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $app_dir application directory |
||
| 137 | * @return false|string |
||
| 138 | */ |
||
| 139 | protected function getActiveRecordConfig($app_dir) |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $app_dir application directory |
||
| 157 | * @param string $namespace output file in namespace format |
||
| 158 | * @return false|string |
||
| 159 | */ |
||
| 160 | protected function getOutputFile($app_dir, $namespace) |
||
| 175 |