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 |
||
| 16 | class ProjectTest extends PHPUnit_Framework_TestCase |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * A project has its own domain name, database name, URL, script path, and article path. |
||
| 21 | */ |
||
| 22 | public function testBasicMetadata() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Make sure there's an error when trying to get project metadata without a Repository. |
||
| 41 | * @expectedException \Exception |
||
| 42 | * @expectedExceptionMessage Repository for Xtools\Project must be set before using. |
||
| 43 | */ |
||
| 44 | public function testNoRepository() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * A project has a set of namespaces, comprising integer IDs and string titles. |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function testNamespaces() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * XTools can be run in single-wiki mode, where there is only one project. |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function testSingleWiki() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * A project is considered to exist if it has at least a domain name. |
||
| 82 | */ |
||
| 83 | public function testExists() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * A user or a whole project can opt in to displaying restricted statistics. |
||
| 97 | * @dataProvider optedInProvider |
||
| 98 | * @param string[] $optedInProjects List of projects. |
||
| 99 | * @param string $dbname The database name. |
||
| 100 | * @param bool $hasOptedIn The result to check against. |
||
| 101 | */ |
||
| 102 | public function testOptedIn($optedInProjects, $dbname, $hasOptedIn) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Data for self::testOptedIn(). |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function optedInProvider() |
||
| 133 | } |
||
| 134 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.