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 |
||
| 7 | class SubscriptionPeriodTest extends \PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | |||
| 10 | /** @test */ |
||
| 11 | public function UnitMustBeValid() |
||
| 16 | |||
| 17 | /** @test */ |
||
| 18 | public function SettingUnitToWeeklyChecksMoment() |
||
| 24 | |||
| 25 | /** @test */ |
||
| 26 | public function SettingUnitToMonthlyChecksMoment() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @test |
||
| 35 | * @dataProvider unitProvider |
||
| 36 | */ |
||
| 37 | public function UnitCanBeSetRight($unit) |
||
| 42 | |||
| 43 | /** @test */ |
||
| 44 | public function IntervalMustBeInteger() |
||
| 49 | |||
| 50 | /** @test */ |
||
| 51 | public function IntervalMustBePositive() |
||
| 56 | |||
| 57 | /** @test */ |
||
| 58 | public function IntervalMustNotBeTooBig() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @test |
||
| 66 | * @dataProvider intProvider |
||
| 67 | */ |
||
| 68 | public function IntervalCanBeSetRight($interval) |
||
| 73 | |||
| 74 | /** @test */ |
||
| 75 | public function MomentMustBeInt() |
||
| 80 | |||
| 81 | /** @test */ |
||
| 82 | public function MomentMustBePositive() |
||
| 87 | |||
| 88 | /** @test */ |
||
| 89 | public function MomentMustNotBeTooBig() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @test |
||
| 97 | * @dataProvider badUnitMomentComboProvider |
||
| 98 | */ |
||
| 99 | public function MomentChecksUnit($unit, $interval) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @test |
||
| 107 | * @dataProvider intProvider |
||
| 108 | */ |
||
| 109 | public function MomentCanBeSetRight($moment) |
||
| 114 | |||
| 115 | View Code Duplication | public function unitProvider() |
|
| 123 | |||
| 124 | public function intProvider() |
||
| 133 | |||
| 134 | View Code Duplication | public function badUnitMomentComboProvider() |
|
| 141 | |||
| 142 | protected function createPeriod($unit = SubscriptionPeriod::UNIT_DAILY, $interval = 12, $moment = 6) |
||
| 146 | } |
||
| 147 |
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.