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 PriceTest extends \PHPUnit_Framework_TestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Verify invalid constructor parameters cause exceptions. |
||
| 14 | * |
||
| 15 | * @param mixed $type The description of the price. |
||
| 16 | * @param mixed $price The price of the price. |
||
| 17 | * |
||
| 18 | * @test |
||
| 19 | * @dataProvider constructorBadData |
||
| 20 | * @expectedException \InvalidArgumentException |
||
| 21 | * |
||
| 22 | * @return void |
||
| 23 | */ |
||
| 24 | public function constructWithInvalidParameters($type, $price) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Data provider for constructWithInvalidParameters. |
||
| 31 | * |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | public function constructorBadData() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Verify valid constructor parameters cause no exceptions. |
||
| 46 | * |
||
| 47 | * @param mixed $type The description of the price. |
||
| 48 | * @param mixed $price The price of the price. |
||
| 49 | * |
||
| 50 | * @test |
||
| 51 | * @dataProvider constructorGoodData |
||
| 52 | * |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | public function constructWithValidParameters($type, $price) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Data provider for constructWithInvalidParameters. |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function constructorGoodData() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Verify basic behavior of fromArray(). |
||
| 79 | * |
||
| 80 | * @test |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | public function fromArray() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Verify fromArray throws when input is invalid. |
||
| 93 | * |
||
| 94 | * @test |
||
| 95 | * @expectedException \Exception |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function fromArrayWithInvalidInput() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Verify basic behavior of fromArrays(). |
||
| 106 | * |
||
| 107 | * @test |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function fromArrays() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Verify fromArrays throws when input is invalid. |
||
| 129 | * |
||
| 130 | * @test |
||
| 131 | * @expectedException \Exception |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function fromArraysWithInvalidInput() |
||
| 144 | } |
||
| 145 |
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: