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 |
||
14 | class LabsHelper |
||
15 | { |
||
16 | /** @var string The current database name. */ |
||
17 | protected $dbName; |
||
18 | |||
19 | /** @var Connection The database connection. */ |
||
20 | protected $client; |
||
21 | |||
22 | /** @var ContainerInterface The DI container. */ |
||
23 | protected $container; |
||
24 | |||
25 | /** @var string The project URL. */ |
||
26 | protected $url; |
||
27 | |||
28 | /** |
||
29 | * LabsHelper constructor. |
||
30 | * @param ContainerInterface $container |
||
31 | */ |
||
32 | public function __construct(ContainerInterface $container) |
||
33 | { |
||
34 | $this->container = $container; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Is XTools connecting to WMF Labs? |
||
39 | * |
||
40 | * @return boolean |
||
41 | */ |
||
42 | public function isLabs() |
||
46 | |||
47 | /** |
||
48 | * All mapping tables to environment-specific names, as specified in config/table_map.yml |
||
49 | * Used for example to convert revision -> revision_replica |
||
50 | * https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs |
||
51 | * |
||
52 | * @param string $table Table name |
||
53 | * @param string $dbName Database name |
||
54 | * @param string|null $table_extension Optional table extension, which will only get used if we're on labs. |
||
55 | * |
||
56 | * @return string Converted table name |
||
57 | */ |
||
58 | public function getTable($table, $dbName = null, $table_extension = null) |
||
84 | |||
85 | // TODO: figure out how to use Doctrine to query host 'tools-db' |
||
86 | } |
||
87 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.