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:
| 1 | <?php |
||
| 17 | class Upgrade_259 extends XoopsUpgrade |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * __construct |
||
| 21 | */ |
||
| 22 | public function __construct() |
||
| 23 | { |
||
| 24 | parent::__construct(basename(__DIR__)); |
||
| 25 | $this->tasks = array('sess_id', 'mainfile', 'zaplegacy'); |
||
| 26 | $this->usedFiles = array( |
||
| 27 | 'mainfile.php', |
||
| 28 | XOOPS_VAR_PATH . '/data/secure.php', |
||
| 29 | 'modules/system/themes/legacy/legacy.php' |
||
| 30 | ); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Return the length of a database table column |
||
| 35 | * |
||
| 36 | * @param string $table table name |
||
| 37 | * @param string $column column name |
||
| 38 | * |
||
| 39 | * @return int column length or zero on error |
||
| 40 | */ |
||
| 41 | View Code Duplication | private function getColumnLength($table, $column) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * In PHP 7.1 Session ID length could be any length between 22 and 256 |
||
| 71 | * |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public function check_sess_id() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Expand session id column to varchar(256) to accommodate expanded size possible in PHP 7.1 |
||
| 81 | * Force ascii character set to prevent key length issues. |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function apply_sess_id() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Copy a configuration file from template, then rewrite with actual configuration values |
||
| 95 | * |
||
| 96 | * @param string[] $vars config values |
||
| 97 | * @param string $path directory path where files reside |
||
| 98 | * @param string $sourceName template file name |
||
| 99 | * @param string $fileName configuration file name |
||
| 100 | * |
||
| 101 | * @return true|string true on success, error message on failure |
||
| 102 | */ |
||
| 103 | protected function writeConfigurationFile($vars, $path, $sourceName, $fileName) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Do we need to rewrite mainfile and secure? |
||
| 136 | * |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public function check_mainfile() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Rewrite mainfile and secure file with current templates |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function apply_mainfile() |
||
| 180 | |||
| 181 | //modules/system/themes/legacy/legacy.php |
||
| 182 | /** |
||
| 183 | * Do we need to rewrite mainfile and secure? |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function check_zaplegacy() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Rewrite mainfile and secure file with current templates |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function apply_zaplegacy() |
||
| 206 | } |
||
| 207 | |||
| 210 |
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.