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 |
||
| 13 | class ToContainTextExpectationTest extends \PHPUnit_Framework_TestCase |
||
| 14 | { |
||
| 15 | const URL = 'http://www.example.com/'; |
||
| 16 | const HAYSTACK = 'Trollface skeptical Fry wat me gusta'; |
||
| 17 | const NEEDLE = 'skep'; |
||
| 18 | const NEEDLE_REGEX = '/\bfr\w\b/i'; |
||
| 19 | const BAD_NEEDLE = 'norris'; |
||
| 20 | const BAD_NEEDLE_REGEX = '/wot\b/'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @expectedException \InvalidArgumentException |
||
| 24 | * @expectedExceptionMessage The actual value provided is not a valid URL |
||
| 25 | */ |
||
| 26 | public function testInvalidUrl() |
||
| 30 | |||
| 31 | View Code Duplication | public function testBasicStringMatching() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
||
| 43 | * @expectedExceptionMessage Expected http://www.example.com/ to contain the text "norris", but wasn't found in the response |
||
| 44 | */ |
||
| 45 | public function testFailedBasicMatch() |
||
| 51 | |||
| 52 | View Code Duplication | public function testRegexMatching() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
||
| 64 | * @expectedExceptionMessage Expected http://www.example.com/ to contain the text "/wot\b/", but wasn't found in the response |
||
| 65 | */ |
||
| 66 | public function testFailedRegexMatch() |
||
| 72 | |||
| 73 | View Code Duplication | public function testHtmlBasicStringMatching() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException |
||
| 88 | * @expectedExceptionMessage Expected http://www.example.com/ to contain the text "norris", but wasn't found in the textual content of any element |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function testFailedHtmlBasicMatch() |
|
| 99 | |||
| 100 | View Code Duplication | public function testHtmlErrorRegexMatching() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @expectedException GuzzleHttp\Exception\ServerException |
||
| 117 | * @expectedExceptionMessage Server error: `GET http://www.example.com/` resulted in a `500 Internal Server Error` response: |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function testHttpErrorWhenNotAllowed() |
|
| 129 | |||
| 130 | private function createExpectationWithMockedResponse($body = self::HAYSTACK, $contentType = 'text/plain', $result = 200, $allowErrors = false) |
||
| 144 | } |
||
| 145 |
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.