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 | final class ImageTest extends \PHPUnit_Framework_TestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Verify basic behavior of getPath. |
||
| 14 | * |
||
| 15 | * @test |
||
| 16 | * |
||
| 17 | * @return void |
||
| 18 | */ |
||
| 19 | public function getPath() |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Verify basic behavior of getExtension. |
||
| 26 | * |
||
| 27 | * @test |
||
| 28 | * |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | public function getExtension() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Verify basic behavior of getUrl. |
||
| 38 | * |
||
| 39 | * @test |
||
| 40 | * @covers ::getUrl |
||
| 41 | * |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | public function getUrl() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Verify invalid constructor parameters cause exceptions. |
||
| 54 | * |
||
| 55 | * @param mixed $path The full URL (including scheme, domain, and path). |
||
| 56 | * @param mixed $extension The text identifier for the URL. |
||
| 57 | * |
||
| 58 | * @test |
||
| 59 | * @dataProvider constructorBadData |
||
| 60 | * @expectedException \InvalidArgumentException |
||
| 61 | * |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | public function constructWithInvalidParameters($path, $extension) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Data provider for constructWithInvalidParameters. |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function constructorBadData() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Verify basic functionality of fromArray(). |
||
| 84 | * |
||
| 85 | * @test |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function fromArray() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Verify fromArray() throws filter exception. |
||
| 98 | * |
||
| 99 | * @test |
||
| 100 | * @expectedException \Exception |
||
| 101 | * |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function fromArrayInvalidPath() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Verify basic behavior of fromArrays(). |
||
| 111 | * |
||
| 112 | * @test |
||
| 113 | * |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function fromArrays() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Verify fromArrays throws when input is invalid. |
||
| 134 | * |
||
| 135 | * @test |
||
| 136 | * @expectedException \Exception |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function fromArraysWithInvalidInput() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Verify behaviour with null values. |
||
| 152 | * |
||
| 153 | * @test |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | public function constructWithNulls() |
||
| 163 | } |
||
| 164 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: