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 |
||
29 | class ForeignDBFile extends LocalFile { |
||
30 | /** |
||
31 | * @param Title $title |
||
32 | * @param FileRepo $repo |
||
33 | * @param null $unused |
||
34 | * @return ForeignDBFile |
||
35 | */ |
||
36 | static function newFromTitle( $title, $repo, $unused = null ) { |
||
39 | |||
40 | /** |
||
41 | * Create a ForeignDBFile from a title |
||
42 | * Do not call this except from inside a repo class. |
||
43 | * |
||
44 | * @param stdClass $row |
||
45 | * @param FileRepo $repo |
||
46 | * @return ForeignDBFile |
||
47 | */ |
||
48 | View Code Duplication | static function newFromRow( $row, $repo ) { |
|
55 | |||
56 | /** |
||
57 | * @param string $srcPath |
||
58 | * @param int $flags |
||
59 | * @param array $options |
||
60 | * @return FileRepoStatus |
||
61 | * @throws MWException |
||
62 | */ |
||
63 | function publish( $srcPath, $flags = 0, array $options = [] ) { |
||
66 | |||
67 | /** |
||
68 | * @param string $oldver |
||
69 | * @param string $desc |
||
70 | * @param string $license |
||
71 | * @param string $copyStatus |
||
72 | * @param string $source |
||
73 | * @param bool $watch |
||
74 | * @param bool|string $timestamp |
||
75 | * @param User $user User object or null to use $wgUser |
||
76 | * @return bool |
||
77 | * @throws MWException |
||
78 | */ |
||
79 | function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', |
||
83 | |||
84 | /** |
||
85 | * @param array $versions |
||
86 | * @param bool $unsuppress |
||
87 | * @return FileRepoStatus |
||
88 | * @throws MWException |
||
89 | */ |
||
90 | function restore( $versions = [], $unsuppress = false ) { |
||
93 | |||
94 | /** |
||
95 | * @param string $reason |
||
96 | * @param bool $suppress |
||
97 | * @param User|null $user |
||
98 | * @return FileRepoStatus |
||
99 | * @throws MWException |
||
100 | */ |
||
101 | function delete( $reason, $suppress = false, $user = null ) { |
||
104 | |||
105 | /** |
||
106 | * @param Title $target |
||
107 | * @return FileRepoStatus |
||
108 | * @throws MWException |
||
109 | */ |
||
110 | function move( $target ) { |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | function getDescriptionUrl() { |
||
121 | |||
122 | /** |
||
123 | * @param bool|Language $lang Optional language to fetch description in. |
||
124 | * @return string |
||
125 | */ |
||
126 | function getDescriptionText( $lang = false ) { |
||
173 | |||
174 | /** |
||
175 | * Get short description URL for a file based on the page ID. |
||
176 | * |
||
177 | * @return string |
||
178 | * @throws DBUnexpectedError |
||
179 | * @since 1.27 |
||
180 | */ |
||
181 | public function getDescriptionShortUrl() { |
||
200 | |||
201 | } |
||
202 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: