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 |
||
| 12 | class RoutesTest extends PHPUnit_Framework_TestCase |
||
| 13 | { |
||
| 14 | protected $url; |
||
| 15 | protected $token; |
||
| 16 | protected $client; |
||
| 17 | |||
| 18 | public function __construct () |
||
| 26 | |||
| 27 | public function setUp () |
||
| 31 | |||
| 32 | public function getTestId () |
||
| 36 | |||
| 37 | /** |
||
| 38 | * testInvalidEndpoint |
||
| 39 | */ |
||
| 40 | public function testInvalidEndpoint() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Test if the ouput of getAll is an object |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function testGetAllEmoji () |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Test get a single emoji endpoint |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function testGetSingleEmoji () |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Test Post endpoint |
||
| 71 | */ |
||
| 72 | public function testPOSTEmoji() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Test if Authorization Header is set |
||
| 88 | */ |
||
| 89 | public function testPostIfAuthorizationNotSet () |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Test PUT/PATCH emoji |
||
| 106 | */ |
||
| 107 | public function testPutPatchEmoji () |
||
| 108 | { |
||
| 109 | $data = array( |
||
| 110 | 'name' => 'TestName' |
||
| 111 | ); |
||
| 112 | $request = $this->client->request('PUT', $this->url.'/emojis/'.$this->getTestId(),[ 'headers' => ['Authorization'=> $this->token],'form_params' => $data ]); |
||
| 113 | |||
| 114 | $this->assertInternalType('object' , $request); |
||
| 115 | $this->assertEquals('200', $request->getStatusCode()); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Test DELETE an emoji |
||
| 120 | */ |
||
| 121 | public function testDeleteEmoji() |
||
| 133 | } |
||
| 134 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: