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 |
||
| 17 | class ExtractUrlTest extends \PHPUnit_Framework_TestCase{ |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var \chillerlan\TinyCurl\Request |
||
| 21 | */ |
||
| 22 | protected $requestWithCA; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \chillerlan\TinyCurl\Request |
||
| 26 | */ |
||
| 27 | protected $requestNoCA; |
||
| 28 | |||
| 29 | protected function setUp(){ |
||
| 30 | $co = [CURLOPT_FOLLOWLOCATION => false]; |
||
| 31 | |||
| 32 | $o1 = new RequestOptions; |
||
| 33 | $o1->curl_options = $co; |
||
| 34 | $o1->ca_info = __DIR__.'/test-cacert.pem'; |
||
| 35 | $this->requestWithCA = new Request($o1); |
||
| 36 | |||
| 37 | $o2 = new RequestOptions; |
||
| 38 | $o2->curl_options = $co; |
||
| 39 | $this->requestNoCA = new Request($o2); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function shortURLDataProvider(){ |
||
| 43 | return [ |
||
| 44 | [ |
||
| 45 | [ |
||
| 46 | 'https://t.co/YK4EuyMbl3', |
||
| 47 | 'http://buff.ly/20TJh3q', |
||
| 48 | 'http://www.ebay.com/sch/gillianandersoncharity/m.html?utm_content=buffer5675f&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer', |
||
| 49 | ] |
||
| 50 | ], |
||
| 51 | [ |
||
| 52 | [ |
||
| 53 | 'https://t.co/ZSS6nVOcVp', // i wonder how long twitter will store this URL since the test tweet has been deleted |
||
| 54 | 'http://bit.ly/1oesmr8', |
||
| 55 | 'http://tinyurl.com/jvc5y98', |
||
| 56 | 'https://api.guildwars2.com/v2/build', |
||
| 57 | ], |
||
| 58 | ], |
||
| 59 | [ |
||
| 60 | [ |
||
| 61 | 'http://curl.haxx.se/ca/cacert.pem', |
||
| 62 | 'https://curl.haxx.se/ca/cacert.pem', |
||
| 63 | |||
| 64 | // grabbing the body is perhaps a little too greedy... |
||
| 65 | # 'http://hg.mozilla.org/releases/mozilla-release/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt', |
||
| 66 | # 'http://mozilla.org/MPL/2.0/', |
||
| 67 | # 'https://www.mozilla.org/MPL/2.0/', |
||
| 68 | # 'https://www.mozilla.org/en-US/MPL/2.0/', |
||
| 69 | # 'http://html5shim.googlecode.com/svn/trunk/html5.js', |
||
| 70 | ], |
||
| 71 | ], |
||
| 72 | ]; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @dataProvider shortURLDataProvider |
||
| 77 | */ |
||
| 78 | public function testExtractShortUrlWithCA($expected){ |
||
| 79 | $this->assertEquals($expected, $this->requestWithCA->extractShortUrl($expected[0])); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @dataProvider shortURLDataProvider |
||
| 84 | */ |
||
| 85 | public function testExtractShortUrlNoCA($expected){ |
||
| 86 | $this->assertEquals($expected, $this->requestNoCA->extractShortUrl($expected[0])); |
||
| 87 | } |
||
| 88 | |||
| 89 | } |
||
| 90 |