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 |
||
| 15 | class StatsTemplateStats extends StatisticsPage |
||
| 16 | { |
||
| 17 | protected function execute() |
||
| 18 | { |
||
| 19 | $query = <<<SQL |
||
| 20 | SELECT |
||
| 21 | t.id as "Template ID", |
||
| 22 | t.usercode as "Template Code", |
||
| 23 | u.count as "Active users using template", |
||
| 24 | countall as "All users using template" |
||
| 25 | FROM welcometemplate t |
||
| 26 | LEFT JOIN |
||
| 27 | ( |
||
| 28 | SELECT |
||
| 29 | welcome_template, |
||
| 30 | COUNT(*) as count |
||
| 31 | FROM user |
||
| 32 | WHERE |
||
| 33 | (status = "User" OR status = "Admin") |
||
| 34 | AND welcome_template IS NOT NULL |
||
| 35 | GROUP BY welcome_template |
||
| 36 | ) u ON u.welcome_template = t.id |
||
| 37 | LEFT JOIN |
||
| 38 | ( |
||
| 39 | SELECT |
||
| 40 | welcome_template as allid, |
||
| 41 | COUNT(*) as countall |
||
| 42 | FROM user |
||
| 43 | WHERE welcome_template IS NOT NULL |
||
| 44 | GROUP BY welcome_template |
||
| 45 | ) u2 ON u2.allid = t.id; |
||
| 46 | SQL; |
||
| 47 | |||
| 48 | $qb = new QueryBrowser(); |
||
| 49 | $r = $qb->executeQueryToTable($query); |
||
| 50 | |||
| 51 | return $r; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function getPageName() |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getPageTitle() |
||
| 62 | } |
||
| 63 | |||
| 64 | public function isProtected() |
||
| 67 | } |
||
| 68 | |||
| 69 | public function requiresWikiDatabase() |
||
| 72 | } |
||
| 73 | } |
||
| 74 |