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 |
||
| 25 | class StateApiClientTest extends ApiClientTest |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var StateApiClient |
||
| 29 | */ |
||
| 30 | private $client; |
||
| 31 | |||
| 32 | View Code Duplication | protected function setUp() |
|
|
|
|||
| 33 | { |
||
| 34 | parent::setUp(); |
||
| 35 | $this->client = new StateApiClient( |
||
| 36 | $this->requestHandler, |
||
| 37 | '1.0.1', |
||
| 38 | new DocumentDataSerializer($this->serializer), |
||
| 39 | new ActorSerializer($this->serializer) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | View Code Duplication | public function testCreateOrUpdateDocument() |
|
| 44 | { |
||
| 45 | $document = DocumentFixtures::getStateDocument(); |
||
| 46 | |||
| 47 | $this->validateStoreApiCall( |
||
| 48 | 'post', |
||
| 49 | 'activities/state', |
||
| 50 | array( |
||
| 51 | 'activityId' => 'activity-id', |
||
| 52 | 'agent' => 'agent-as-json', |
||
| 53 | 'stateId' => 'state-id', |
||
| 54 | ), |
||
| 55 | 204, |
||
| 56 | '', |
||
| 57 | $document->getData(), |
||
| 58 | array(array('data' => $document->getState()->getActor(), 'result' => 'agent-as-json')) |
||
| 59 | ); |
||
| 60 | |||
| 61 | $this->client->createOrUpdateDocument($document); |
||
| 62 | } |
||
| 63 | |||
| 64 | View Code Duplication | public function testCreateOrReplaceDocument() |
|
| 65 | { |
||
| 66 | $document = DocumentFixtures::getStateDocument(); |
||
| 67 | |||
| 68 | $this->validateStoreApiCall( |
||
| 69 | 'put', |
||
| 70 | 'activities/state', |
||
| 71 | array( |
||
| 72 | 'activityId' => 'activity-id', |
||
| 73 | 'agent' => 'agent-as-json', |
||
| 74 | 'stateId' => 'state-id', |
||
| 75 | ), |
||
| 76 | 204, |
||
| 77 | '', |
||
| 78 | $document->getData(), |
||
| 79 | array(array('data' => $document->getState()->getActor(), 'result' => 'agent-as-json')) |
||
| 80 | ); |
||
| 81 | |||
| 82 | $this->client->createOrReplaceDocument($document); |
||
| 83 | } |
||
| 84 | |||
| 85 | View Code Duplication | public function testDeleteDocument() |
|
| 86 | { |
||
| 87 | $state = $this->createState(); |
||
| 88 | |||
| 89 | $this->validateDeleteDocumentCall( |
||
| 90 | 'activities/state', |
||
| 91 | array( |
||
| 92 | 'activityId' => 'activity-id', |
||
| 93 | 'agent' => 'agent-as-json', |
||
| 94 | 'stateId' => 'state-id', |
||
| 95 | ), |
||
| 96 | array(array('data' => $state->getActor(), 'result' => 'agent-as-json')) |
||
| 97 | ); |
||
| 98 | |||
| 99 | $this->client->deleteDocument($state); |
||
| 100 | } |
||
| 101 | |||
| 102 | View Code Duplication | public function testGetDocument() |
|
| 126 | |||
| 127 | private function createState() |
||
| 128 | { |
||
| 129 | $agent = new Agent('mailto:[email protected]'); |
||
| 130 | $activity = new Activity('activity-id'); |
||
| 135 | } |
||
| 136 |
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.