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 |
||
24 | class RCCacheEntryFactory { |
||
25 | |||
26 | /* @var IContextSource */ |
||
27 | private $context; |
||
28 | |||
29 | /* @var string[] */ |
||
30 | private $messages; |
||
31 | |||
32 | /** |
||
33 | * @var LinkRenderer |
||
34 | */ |
||
35 | private $linkRenderer; |
||
36 | |||
37 | /** |
||
38 | * @param IContextSource $context |
||
39 | * @param string[] $messages |
||
40 | * @param LinkRenderer $linkRenderer |
||
41 | */ |
||
42 | public function __construct( |
||
43 | IContextSource $context, $messages, LinkRenderer $linkRenderer |
||
44 | ) { |
||
45 | $this->context = $context; |
||
46 | $this->messages = $messages; |
||
47 | $this->linkRenderer = $linkRenderer; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param RecentChange $baseRC |
||
52 | * @param bool $watched |
||
53 | * |
||
54 | * @return RCCacheEntry |
||
55 | */ |
||
56 | public function newFromRecentChange( RecentChange $baseRC, $watched ) { |
||
91 | |||
92 | /** |
||
93 | * @param RecentChange $cacheEntry |
||
94 | * @param User $user |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | private function showDiffLinks( RecentChange $cacheEntry, User $user ) { |
||
101 | |||
102 | /** |
||
103 | * @param RecentChange $cacheEntry |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | private function buildCLink( RecentChange $cacheEntry ) { |
||
108 | $type = $cacheEntry->mAttribs['rc_type']; |
||
109 | |||
110 | // New unpatrolled pages |
||
111 | if ( $cacheEntry->unpatrolled && $type == RC_NEW ) { |
||
|
|||
112 | $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() ); |
||
113 | // Log entries |
||
114 | } elseif ( $type == RC_LOG ) { |
||
115 | $logType = $cacheEntry->mAttribs['rc_log_type']; |
||
116 | |||
117 | if ( $logType ) { |
||
118 | $clink = $this->getLogLink( $logType ); |
||
119 | } else { |
||
120 | wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' ); |
||
121 | $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() ); |
||
122 | } |
||
123 | // Log entries (old format) and special pages |
||
124 | } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) { |
||
125 | wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' ); |
||
126 | $clink = ''; |
||
127 | // Edits |
||
128 | } else { |
||
129 | $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() ); |
||
130 | } |
||
131 | |||
132 | return $clink; |
||
133 | } |
||
134 | |||
135 | private function getLogLink( $logType ) { |
||
136 | $logtitle = SpecialPage::getTitleFor( 'Log', $logType ); |
||
137 | $logpage = new LogPage( $logType ); |
||
138 | $logname = $logpage->getName()->text(); |
||
139 | |||
140 | $logLink = $this->context->msg( 'parentheses' ) |
||
141 | ->rawParams( |
||
142 | $this->linkRenderer->makeKnownLink( $logtitle, $logname ) |
||
143 | )->escaped(); |
||
144 | |||
145 | return $logLink; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param RecentChange $cacheEntry |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | private function buildTimestamp( RecentChange $cacheEntry ) { |
||
159 | |||
160 | /** |
||
161 | * @param RecentChange $recentChange |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | View Code Duplication | private function buildCurQueryParams( RecentChange $recentChange ) { |
|
172 | |||
173 | /** |
||
174 | * @param RecentChange $cacheEntry |
||
175 | * @param bool $showDiffLinks |
||
176 | * @param int $counter |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) { |
||
194 | |||
195 | /** |
||
196 | * @param RecentChange $recentChange |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | View Code Duplication | private function buildDiffQueryParams( RecentChange $recentChange ) { |
|
207 | |||
208 | /** |
||
209 | * @param RecentChange $cacheEntry |
||
210 | * @param bool $showDiffLinks |
||
211 | * @param int $counter |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) { |
||
216 | $queryParams = $this->buildDiffQueryParams( $cacheEntry ); |
||
217 | $diffMessage = $this->getMessage( 'diff' ); |
||
218 | $logTypes = [ RC_NEW, RC_LOG ]; |
||
219 | |||
220 | if ( !$showDiffLinks ) { |
||
221 | $diffLink = $diffMessage; |
||
222 | } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) { |
||
223 | $diffLink = $diffMessage; |
||
224 | } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) { |
||
225 | $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' ); |
||
226 | $pageTitle = Title::newFromID( $rcCurId ); |
||
227 | if ( $pageTitle === null ) { |
||
228 | wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId ); |
||
229 | return $diffMessage; |
||
230 | } |
||
231 | $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) ); |
||
232 | $diffLink = "<a href=\"$diffUrl\">$diffMessage</a>"; |
||
233 | } else { |
||
234 | $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) ); |
||
235 | $diffLink = "<a href=\"$diffUrl\">$diffMessage</a>"; |
||
236 | } |
||
237 | |||
238 | return $diffLink; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param RecentChange $cacheEntry |
||
243 | * @param bool $showDiffLinks |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) { |
||
248 | $lastOldid = $cacheEntry->mAttribs['rc_last_oldid']; |
||
249 | $lastMessage = $this->getMessage( 'last' ); |
||
250 | $type = $cacheEntry->mAttribs['rc_type']; |
||
251 | $logTypes = [ RC_LOG ]; |
||
252 | |||
253 | // Make "last" link |
||
254 | if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) { |
||
255 | $lastLink = $lastMessage; |
||
256 | } else { |
||
257 | $lastLink = $this->linkRenderer->makeKnownLink( |
||
258 | $cacheEntry->getTitle(), |
||
259 | new HtmlArmor( $lastMessage ), |
||
260 | [], |
||
261 | $this->buildDiffQueryParams( $cacheEntry ) |
||
262 | ); |
||
263 | } |
||
264 | |||
265 | return $lastLink; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param RecentChange $cacheEntry |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | private function getUserLink( RecentChange $cacheEntry ) { |
||
286 | |||
287 | /** |
||
288 | * @param string $key |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | private function getMessage( $key ) { |
||
295 | |||
296 | } |
||
297 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.