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 |
||
| 10 | class LabsHelper |
||
| 11 | { |
||
| 12 | /** @var string */ |
||
| 13 | protected $dbName; |
||
| 14 | |||
| 15 | /** @var \Doctrine\DBAL\Connection */ |
||
| 16 | protected $client; |
||
| 17 | |||
| 18 | /** @var ContainerInterface */ |
||
| 19 | protected $container; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | protected $url; |
||
| 23 | |||
| 24 | public function __construct(ContainerInterface $container) |
||
| 28 | |||
| 29 | public function checkEnabled($tool) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Is xTools connecting to WMF Labs? |
||
| 38 | * |
||
| 39 | * @return boolean |
||
| 40 | */ |
||
| 41 | public function isLabs() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get the record for the given project in the meta.wiki table |
||
| 48 | * @param string $project Valid project in the formats: |
||
| 49 | * https://en.wikipedia.org, en.wikipedia, enwiki |
||
| 50 | * @return array|false Database record or false if no record was found. |
||
| 51 | * Relevant values returned include the 'dbname' (enwiki), |
||
| 52 | * 'lang', 'name' (Wikipedia) and 'url' (https://en.wikipeda.org) |
||
| 53 | */ |
||
| 54 | private function getProjectMetadata($project) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns a project's domain (en.wikipedia) given various formats |
||
| 108 | * @param string $project Valid project in the formats: |
||
| 109 | * https://en.wikipedia.org, en.wikipedia, enwiki |
||
| 110 | * @return string|false lang.project.org ('url' value for that wiki) |
||
| 111 | * or false if project was not found |
||
| 112 | */ |
||
| 113 | public function normalizeProject($project) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get a list of all projects. |
||
| 128 | */ |
||
| 129 | public function allProjects() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * All mapping tables to environment-specific names, as specified in config/table_map.yml |
||
| 140 | * Used for example to convert revision -> revision_replica |
||
| 141 | * https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs |
||
| 142 | * |
||
| 143 | * @param string $table Table name |
||
| 144 | * @param string $dbName Database name |
||
| 145 | * @param string|null $table_extension Optional table extension, which will only get used if we're on labs. |
||
| 146 | * |
||
| 147 | * @return string Converted table name |
||
| 148 | */ |
||
| 149 | public function getTable($table, $dbName = null, $table_extension = null) |
||
| 175 | |||
| 176 | // TODO: figure out how to use Doctrine to query host 'tools-db' |
||
| 177 | } |
||
| 178 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.