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 LogPager 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 LogPager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class LogPager extends ReverseChronologicalPager { |
||
| 30 | /** @var array Log types */ |
||
| 31 | private $types = []; |
||
| 32 | |||
| 33 | /** @var string Events limited to those by performer when set */ |
||
| 34 | private $performer = ''; |
||
| 35 | |||
| 36 | /** @var string|Title Events limited to those about Title when set */ |
||
| 37 | private $title = ''; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | private $pattern = ''; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | private $typeCGI = ''; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | private $action = ''; |
||
| 47 | |||
| 48 | /** @var LogEventsList */ |
||
| 49 | public $mLogEventsList; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | * |
||
| 54 | * @param LogEventsList $list |
||
| 55 | * @param string|array $types Log types to show |
||
| 56 | * @param string $performer The user who made the log entries |
||
| 57 | * @param string|Title $title The page title the log entries are for |
||
| 58 | * @param string $pattern Do a prefix search rather than an exact title match |
||
| 59 | * @param array $conds Extra conditions for the query |
||
| 60 | * @param int|bool $year The year to start from. Default: false |
||
| 61 | * @param int|bool $month The month to start from. Default: false |
||
| 62 | * @param string $tagFilter Tag |
||
| 63 | * @param string $action Specific action (subtype) requested |
||
| 64 | */ |
||
| 65 | public function __construct( $list, $types = [], $performer = '', $title = '', |
||
| 83 | |||
| 84 | public function getDefaultQuery() { |
||
| 93 | |||
| 94 | // Call ONLY after calling $this->limitType() already! |
||
| 95 | public function getFilterParams() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set the log reader to return only entries of the given type. |
||
| 117 | * Type restrictions enforced here |
||
| 118 | * |
||
| 119 | * @param string|array $types Log types ('upload', 'delete', etc); |
||
| 120 | * empty string means no restriction |
||
| 121 | */ |
||
| 122 | private function limitType( $types ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set the log reader to return only entries by the given user. |
||
| 162 | * |
||
| 163 | * @param string $name (In)valid user name |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | private function limitPerformer( $name ) { |
||
| 167 | if ( $name == '' ) { |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | $usertitle = Title::makeTitleSafe( NS_USER, $name ); |
||
| 171 | if ( is_null( $usertitle ) ) { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | // Normalize username first so that non-existent users used |
||
| 175 | // in maintenance scripts work |
||
| 176 | $name = $usertitle->getText(); |
||
| 177 | /* Fetch userid at first, if known, provides awesome query plan afterwards */ |
||
| 178 | $userid = User::idFromName( $name ); |
||
| 179 | if ( !$userid ) { |
||
| 180 | $this->mConds['log_user_text'] = IP::sanitizeIP( $name ); |
||
| 181 | } else { |
||
| 182 | $this->mConds['log_user'] = $userid; |
||
| 183 | } |
||
| 184 | // Paranoia: avoid brute force searches (bug 17342) |
||
| 185 | $user = $this->getUser(); |
||
| 186 | if ( !$user->isAllowed( 'deletedhistory' ) ) { |
||
| 187 | $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0'; |
||
| 188 | } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
||
| 189 | $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) . |
||
| 190 | ' != ' . LogPage::SUPPRESSED_USER; |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->performer = $name; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set the log reader to return only entries affecting the given page. |
||
| 198 | * (For the block and rights logs, this is a user page.) |
||
| 199 | * |
||
| 200 | * @param string|Title $page Title name |
||
| 201 | * @param string $pattern |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | private function limitTitle( $page, $pattern ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Set the log_action field to a specified value (or values) |
||
| 271 | * |
||
| 272 | * @param string $action |
||
| 273 | */ |
||
| 274 | private function limitAction( $action ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Constructs the most part of the query. Extra conditions are sprinkled in |
||
| 296 | * all over this class. |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | public function getQueryInfo() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Checks if $this->mConds has $field matched to a *single* value |
||
| 347 | * @param string $field |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | protected function hasEqualsClause( $field ) { |
||
| 356 | |||
| 357 | function getIndexField() { |
||
| 360 | |||
| 361 | public function getStartBody() { |
||
| 380 | |||
| 381 | public function formatRow( $row ) { |
||
| 384 | |||
| 385 | public function getType() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Guaranteed to either return a valid title string or a Zero-Length String |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function getPerformer() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function getPage() { |
||
| 404 | |||
| 405 | public function getPattern() { |
||
| 408 | |||
| 409 | public function getYear() { |
||
| 412 | |||
| 413 | public function getMonth() { |
||
| 416 | |||
| 417 | public function getTagFilter() { |
||
| 420 | |||
| 421 | public function getAction() { |
||
| 424 | |||
| 425 | public function doQuery() { |
||
| 431 | } |
||
| 432 |
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: