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 OldLocalFile 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 OldLocalFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class OldLocalFile extends LocalFile { |
||
30 | /** @var string Timestamp */ |
||
31 | protected $requestedTime; |
||
32 | |||
33 | /** @var string Archive name */ |
||
34 | protected $archive_name; |
||
35 | |||
36 | const CACHE_VERSION = 1; |
||
37 | const MAX_CACHE_ROWS = 20; |
||
38 | |||
39 | /** |
||
40 | * @param Title $title |
||
41 | * @param FileRepo $repo |
||
42 | * @param null|int $time Timestamp or null |
||
43 | * @return OldLocalFile |
||
44 | * @throws MWException |
||
45 | */ |
||
46 | static function newFromTitle( $title, $repo, $time = null ) { |
||
54 | |||
55 | /** |
||
56 | * @param Title $title |
||
57 | * @param FileRepo $repo |
||
58 | * @param string $archiveName |
||
59 | * @return OldLocalFile |
||
60 | */ |
||
61 | static function newFromArchiveName( $title, $repo, $archiveName ) { |
||
64 | |||
65 | /** |
||
66 | * @param stdClass $row |
||
67 | * @param FileRepo $repo |
||
68 | * @return OldLocalFile |
||
69 | */ |
||
70 | static function newFromRow( $row, $repo ) { |
||
77 | |||
78 | /** |
||
79 | * Create a OldLocalFile from a SHA-1 key |
||
80 | * Do not call this except from inside a repo class. |
||
81 | * |
||
82 | * @param string $sha1 Base-36 SHA-1 |
||
83 | * @param LocalRepo $repo |
||
84 | * @param string|bool $timestamp MW_timestamp (optional) |
||
85 | * |
||
86 | * @return bool|OldLocalFile |
||
87 | */ |
||
88 | View Code Duplication | static function newFromKey( $sha1, $repo, $timestamp = false ) { |
|
103 | |||
104 | /** |
||
105 | * Fields in the oldimage table |
||
106 | * @return array |
||
107 | */ |
||
108 | static function selectFields() { |
||
128 | |||
129 | /** |
||
130 | * @param Title $title |
||
131 | * @param FileRepo $repo |
||
132 | * @param string $time Timestamp or null to load by archive name |
||
133 | * @param string $archiveName Archive name or null to load by timestamp |
||
134 | * @throws MWException |
||
135 | */ |
||
136 | function __construct( $title, $repo, $time, $archiveName ) { |
||
144 | |||
145 | /** |
||
146 | * @return bool |
||
147 | */ |
||
148 | function getCacheKey() { |
||
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | function getArchiveName() { |
||
162 | |||
163 | /** |
||
164 | * @return bool |
||
165 | */ |
||
166 | function isOld() { |
||
169 | |||
170 | /** |
||
171 | * @return bool |
||
172 | */ |
||
173 | function isVisible() { |
||
176 | |||
177 | function loadFromDB( $flags = 0 ) { |
||
198 | |||
199 | /** |
||
200 | * Load lazy file metadata from the DB |
||
201 | */ |
||
202 | protected function loadExtraFromDB() { |
||
229 | |||
230 | /** |
||
231 | * @param string $prefix |
||
232 | * @return array |
||
233 | */ |
||
234 | function getCacheFields( $prefix = 'img_' ) { |
||
241 | |||
242 | /** |
||
243 | * @return string |
||
244 | */ |
||
245 | function getRel() { |
||
248 | |||
249 | /** |
||
250 | * @return string |
||
251 | */ |
||
252 | function getUrlRel() { |
||
255 | |||
256 | function upgradeRow() { |
||
287 | |||
288 | /** |
||
289 | * @param int $field One of DELETED_* bitfield constants for file or |
||
290 | * revision rows |
||
291 | * @return bool |
||
292 | */ |
||
293 | function isDeleted( $field ) { |
||
298 | |||
299 | /** |
||
300 | * Returns bitfield value |
||
301 | * @return int |
||
302 | */ |
||
303 | function getVisibility() { |
||
308 | |||
309 | /** |
||
310 | * Determine if the current user is allowed to view a particular |
||
311 | * field of this image file, if it's marked as deleted. |
||
312 | * |
||
313 | * @param int $field |
||
314 | * @param User|null $user User object to check, or null to use $wgUser |
||
315 | * @return bool |
||
316 | */ |
||
317 | function userCan( $field, User $user = null ) { |
||
322 | |||
323 | /** |
||
324 | * Upload a file directly into archive. Generally for Special:Import. |
||
325 | * |
||
326 | * @param string $srcPath File system path of the source file |
||
327 | * @param string $archiveName Full archive name of the file, in the form |
||
328 | * $timestamp!$filename, where $filename must match $this->getName() |
||
329 | * @param string $timestamp |
||
330 | * @param string $comment |
||
331 | * @param User $user |
||
332 | * @return Status |
||
333 | */ |
||
334 | function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) { |
||
350 | |||
351 | /** |
||
352 | * Record a file upload in the oldimage table, without adding log entries. |
||
353 | * |
||
354 | * @param string $srcPath File system path to the source file |
||
355 | * @param string $archiveName The archive name of the file |
||
356 | * @param string $timestamp |
||
357 | * @param string $comment Upload comment |
||
358 | * @param User $user User who did this upload |
||
359 | * @return bool |
||
360 | */ |
||
361 | protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) { |
||
392 | |||
393 | /** |
||
394 | * If archive name is an empty string, then file does not "exist" |
||
395 | * |
||
396 | * This is the case for a couple files on Wikimedia servers where |
||
397 | * the old version is "lost". |
||
398 | */ |
||
399 | public function exists() { |
||
406 | } |
||
407 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: