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 |
||
| 47 | class UsersController extends ResourceController |
||
| 48 | { |
||
| 49 | public function getResource() |
||
| 50 | { |
||
| 51 | $endpoint = new Endpoint(new Customer()); |
||
| 52 | $endpoint->string('name', 'first_name'); |
||
| 53 | $endpoint->boolean('published'); |
||
| 54 | $endpoint->dateTime('updated', 'Last update'); |
||
| 55 | |||
| 56 | return $endpoint; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getResourcesCollection() |
||
| 60 | { |
||
| 61 | $collection = new Endpoint(new Customer()); |
||
| 62 | $collection->string('name', 'first_name'); |
||
| 63 | $collection->boolean('published', true); |
||
| 64 | $collection->dateTime('updated', 'Last update'); |
||
| 65 | |||
| 66 | return $collection; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 97 |