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 namespace VojtaSvoboda\Reservations\Tests\Models; |
||
9 | class ReservationTest extends PluginTestCase |
||
10 | { |
||
11 | /** |
||
12 | * Get tested model. |
||
13 | * |
||
14 | * @return Reservation |
||
15 | */ |
||
16 | public function getModel() |
||
20 | |||
21 | public function testGetHash() |
||
30 | |||
31 | public function testGetNumber() |
||
40 | |||
41 | public function testScopeNotCancelled() |
||
42 | { |
||
43 | $model = $this->getModel(); |
||
44 | |||
45 | // create reservation |
||
46 | $reservation = $model->create($this->getTestingReservationData()); |
||
47 | $reservations = $model->notCancelled()->get(); |
||
48 | $this->assertNotEmpty($reservations); |
||
49 | |||
50 | // change reservation to cancelled |
||
51 | $reservation->status = Status::where('ident', 'cancelled')->first(); |
||
52 | $reservation->save(); |
||
53 | $reservations = $model->notCancelled()->get(); |
||
54 | $this->assertEmpty($reservations); |
||
55 | } |
||
56 | |||
57 | View Code Duplication | private function getTestingReservationData($statusIdent = 'received') |
|
69 | } |
||
70 |
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.