Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UpgradeService 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 UpgradeService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class UpgradeService { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Global Elgg configuration |
||
| 19 | * |
||
| 20 | * @var \stdClass |
||
| 21 | */ |
||
| 22 | private $CONFIG; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor |
||
| 26 | */ |
||
| 27 | public function __construct() { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Run the upgrade process |
||
| 34 | * |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | public function run() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Run any php upgrade scripts which are required |
||
| 71 | * |
||
| 72 | * @param int $version Version upgrading from. |
||
| 73 | * @param bool $quiet Suppress errors. Don't use this. |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | protected function upgradeCode($version, $quiet = false) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * PHP include a file with a very limited scope |
||
| 150 | * |
||
| 151 | * @param string $file File path to include |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | protected static function includeCode($file) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Saves a processed upgrade to a dataset. |
||
| 163 | * |
||
| 164 | * @param string $upgrade Filename of the processed upgrade |
||
| 165 | * (not the path, just the file) |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | protected function setProcessedUpgrade($upgrade) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Gets a list of processes upgrades |
||
| 177 | * |
||
| 178 | * @return mixed Array of processed upgrade filenames or false |
||
| 179 | */ |
||
| 180 | protected function getProcessedUpgrades() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the version of the upgrade filename. |
||
| 188 | * |
||
| 189 | * @param string $filename The upgrade filename. No full path. |
||
| 190 | * @return int|false |
||
| 191 | * @since 1.8.0 |
||
| 192 | */ |
||
| 193 | View Code Duplication | protected function getUpgradeFileVersion($filename) { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Returns a list of upgrade files relative to the $upgrade_path dir. |
||
| 205 | * |
||
| 206 | * @param string $upgrade_path The up |
||
| 207 | * @return array|false |
||
| 208 | */ |
||
| 209 | protected function getUpgradeFiles($upgrade_path = null) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Checks if any upgrades need to be run. |
||
| 241 | * |
||
| 242 | * @param null|array $upgrade_files Optional upgrade files |
||
| 243 | * @param null|array $processed_upgrades Optional processed upgrades |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | protected function getUnprocessedUpgrades($upgrade_files = null, $processed_upgrades = null) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Upgrades Elgg Database and code |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | protected function processUpgrades() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Boot straps into 1.8 upgrade system from 1.7 |
||
| 300 | * |
||
| 301 | * This runs all the 1.7 upgrades, then sets the processed_upgrades to all existing 1.7 upgrades. |
||
| 302 | * Control is then passed back to the main upgrade function which detects and runs the |
||
| 303 | * 1.8 upgrades, regardless of filename convention. |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | protected function bootstrap17to18() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Creates a table {prefix}upgrade_lock that is used as a mutex for upgrades. |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | protected function getUpgradeMutex() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Unlocks upgrade. |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function releaseUpgradeMutex() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Checks if upgrade is locked |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | public function isUpgradeLocked() { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * *************************************************************************** |
||
| 383 | * NOTE: If this is ever removed from Elgg, sites lose the ability to upgrade |
||
| 384 | * from 1.7.x and earlier to the latest version of Elgg without upgrading to |
||
| 385 | * 1.8 first. |
||
| 386 | * *************************************************************************** |
||
| 387 | * |
||
| 388 | * Upgrade the database schema in an ordered sequence. |
||
| 389 | * |
||
| 390 | * Executes all upgrade files in elgg/engine/schema/upgrades/ in sequential order. |
||
| 391 | * Upgrade files must be in the standard Elgg release format of YYYYMMDDII.sql |
||
| 392 | * where II is an incrementor starting from 01. |
||
| 393 | * |
||
| 394 | * Files that are < $version will be ignored. |
||
| 395 | * |
||
| 396 | * @param int $version The version you are upgrading from in the format YYYYMMDDII. |
||
| 397 | * @param string $fromdir Optional directory to load upgrades from. default: engine/schema/upgrades/ |
||
| 398 | * @param bool $quiet If true, suppress all error messages. Only use for the upgrade from <=1.6. |
||
| 399 | * |
||
| 400 | * @return int The number of upgrades run. |
||
| 401 | * @deprecated 1.8 Use PHP upgrades for sql changes. |
||
| 402 | */ |
||
| 403 | protected function dbUpgrade($version, $fromdir = "", $quiet = false) { |
||
| 450 | |||
| 451 | } |
||
| 452 | |||
| 453 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: