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 |
||
| 30 | class UploadFromStash extends UploadBase { |
||
| 31 | protected $mFileKey; |
||
| 32 | protected $mVirtualTempPath; |
||
| 33 | protected $mFileProps; |
||
| 34 | protected $mSourceType; |
||
| 35 | |||
| 36 | // an instance of UploadStash |
||
| 37 | private $stash; |
||
| 38 | |||
| 39 | // LocalFile repo |
||
| 40 | private $repo; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param User|bool $user Default: false |
||
| 44 | * @param UploadStash|bool $stash Default: false |
||
| 45 | * @param FileRepo|bool $repo Default: false |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function __construct( $user = false, $stash = false, $repo = false ) { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $key |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public static function isValidKey( $key ) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param WebRequest $request |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public static function isValidRequest( $request ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $key |
||
| 92 | * @param string $name |
||
| 93 | * @param bool $initTempFile |
||
| 94 | */ |
||
| 95 | public function initialize( $key, $name = 'upload_file', $initTempFile = true ) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param WebRequest $request |
||
| 117 | */ |
||
| 118 | public function initializeFromRequest( &$request ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function getSourceType() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the base 36 SHA1 of the file |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function getTempFileSha1Base36() { |
||
| 145 | |||
| 146 | /* |
||
| 147 | * protected function verifyFile() inherited |
||
| 148 | */ |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Stash the file. |
||
| 152 | * |
||
| 153 | * @param User $user |
||
| 154 | * @return UploadStashFile |
||
| 155 | */ |
||
| 156 | public function stashFile( User $user = null ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Remove a temporarily kept file stashed by saveTempUploadedFile(). |
||
| 166 | * @return bool Success |
||
| 167 | */ |
||
| 168 | public function unsaveUploadedFile() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Remove the database record after a successful upload. |
||
| 174 | */ |
||
| 175 | public function postProcessUpload() { |
||
| 179 | } |
||
| 180 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: