Complex classes like CodendiUpgrade 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 CodendiUpgrade, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | /*abstract*/ class CodendiUpgrade extends DataAccessObject { |
||
53 | |||
54 | //abstract public function _process(); // signature for the _process function. |
||
55 | |||
56 | /** |
||
57 | * @var array{string} $_upgradeError an array of errors appeared in upgrade process |
||
|
|||
58 | */ |
||
59 | var $_upgradeErrors = array(); |
||
60 | /** |
||
61 | * @var string $_environment execution environment |
||
62 | */ |
||
63 | var $_environment; |
||
64 | |||
65 | |||
66 | function CodendiUpgrade() { |
||
67 | $this->_upgradeError = null; |
||
68 | $this->setEnvironment(); |
||
69 | $da =& CodendiDataAccess::instance(); |
||
70 | parent::DataAccessObject($da); |
||
71 | } |
||
72 | |||
73 | function getUpgradeErrors() { |
||
82 | function setEnvironment() { |
||
83 | $default_environment = WEB_ENVIRONMENT; |
||
84 | $this->_environment = $default_environment; |
||
85 | if ($this->_isWebExecution()) { |
||
86 | $this->_environment = WEB_ENVIRONMENT; |
||
87 | } else { |
||
88 | $this->_environment = CONSOLE_ENVIRONMENT; |
||
89 | } |
||
90 | } |
||
91 | function getEnvironment() { |
||
94 | function _isWebExecution() { |
||
95 | if (isset($_SERVER["HTTP_HOST"])) { |
||
96 | return true; |
||
97 | } |
||
98 | return false; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Set a connection to the database |
||
103 | */ |
||
104 | function databaseConnect() { |
||
107 | /** |
||
108 | * Returns if the database connection is set or not |
||
109 | * @return true if the database connection is set, false otherwise |
||
110 | */ |
||
111 | function isDatabaseConnected() { |
||
112 | /*$isConnected = false; |
||
113 | if (getConnection()) { |
||
114 | $isConnected = true; |
||
115 | } |
||
116 | return $isConnected;*/ |
||
117 | return true; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Test if the current upgrade has already been applied or not |
||
122 | * |
||
123 | * @return boolean true if the current upgrade has already been applied, false otherwise. |
||
124 | */ |
||
125 | function isAlreadyApplied() { |
||
134 | |||
135 | /** |
||
136 | * Test if the current upgrade has already been applied WITH SUCCESS or not |
||
137 | * |
||
138 | * @return boolean true if the current upgrade has already been applied with success, false otherwise. |
||
139 | */ |
||
140 | function isAlreadyAppliedWithSuccess() { |
||
149 | |||
150 | /** |
||
151 | * Test if a table exists. |
||
152 | * |
||
153 | * @return boolean true if given table exists in database. |
||
154 | */ |
||
155 | function tableExists($table) { |
||
165 | |||
166 | /** |
||
167 | * Test if field exists in given table. |
||
168 | * |
||
169 | * @return boolean true if given field exists in given table. |
||
170 | */ |
||
171 | function fieldExists($table, $field) { |
||
182 | |||
183 | /** |
||
184 | * Test if index exists in given table. |
||
185 | * |
||
186 | * @return boolean true if given index exists in given table. |
||
187 | */ |
||
188 | function indexNameExists($table, $index) { |
||
201 | |||
202 | /** |
||
203 | * Apply the upgrade |
||
204 | * This is the generic function : |
||
205 | * It checks some recurrent things (database connection, etc.) |
||
206 | * and call the _process function redefined in the concrete subclasses |
||
207 | * |
||
208 | */ |
||
209 | function apply() { |
||
230 | |||
231 | /** |
||
232 | * Store the result of the upgrade in the database. |
||
233 | * |
||
234 | * @return boolean true if the storage was fine, false otherwise |
||
235 | */ |
||
236 | function storeUpgrade() { |
||
253 | |||
254 | |||
255 | /** |
||
256 | * Write a message in the ad-hoc output. |
||
257 | * - the web interface if the execution is a web one |
||
258 | * - the standard output error if the execution if a console one |
||
259 | * |
||
260 | * @param string $feedback the text to display |
||
261 | */ |
||
262 | function writeFeedback($feedback) { |
||
276 | |||
277 | /** |
||
278 | * Returns the line separator regarding the execution environment |
||
279 | * |
||
280 | * @return string the string representing the line separator depending the execution mode |
||
281 | */ |
||
282 | function getLineSeparator() { |
||
294 | |||
295 | } |
||
296 | |||
298 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.