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 |
||
16 | class UserAgentTest extends TestCase |
||
|
|||
17 | { |
||
18 | protected $CrawlerDetect; |
||
19 | |||
20 | public function setUp() |
||
24 | |||
25 | /** @test */ |
||
26 | View Code Duplication | public function user_agents_are_bots() |
|
35 | |||
36 | /** @test */ |
||
37 | View Code Duplication | public function user_agents_are_devices() |
|
46 | |||
47 | /** @test */ |
||
48 | View Code Duplication | public function it_returns_correct_matched_bot_name() |
|
56 | |||
57 | /** @test */ |
||
58 | View Code Duplication | public function it_returns_null_when_no_bot_detected() |
|
66 | |||
67 | /** @test */ |
||
68 | public function empty_user_agent() |
||
74 | |||
75 | /** @test */ |
||
76 | public function current_visitor() |
||
77 | { |
||
78 | $headers = (array) json_decode('{"DOCUMENT_ROOT":"\/home\/test\/public_html","GATEWAY_INTERFACE":"CGI\/1.1","HTTP_ACCEPT":"*\/*","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_CACHE_CONTROL":"no-cache","HTTP_CONNECTION":"Keep-Alive","HTTP_FROM":"bingbot(at)microsoft.com","HTTP_HOST":"www.test.com","HTTP_PRAGMA":"no-cache","HTTP_USER_AGENT":"Mozilla\/5.0 (compatible; bingbot\/2.0; +http:\/\/www.bing.com\/bingbot.htm)","PATH":"\/bin:\/usr\/bin","QUERY_STRING":"order=closingDate","REDIRECT_STATUS":"200","REMOTE_ADDR":"127.0.0.1","REMOTE_PORT":"3360","REQUEST_METHOD":"GET","REQUEST_URI":"\/?test=testing","SCRIPT_FILENAME":"\/home\/test\/public_html\/index.php","SCRIPT_NAME":"\/index.php","SERVER_ADDR":"127.0.0.1","SERVER_ADMIN":"[email protected]","SERVER_NAME":"www.test.com","SERVER_PORT":"80","SERVER_PROTOCOL":"HTTP\/1.1","SERVER_SIGNATURE":"","SERVER_SOFTWARE":"Apache","UNIQUE_ID":"Vx6MENRxerBUSDEQgFLAAAAAS","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1461619728.0705,"REQUEST_TIME":1461619728}'); |
||
79 | |||
80 | $cd = new CrawlerDetect($headers); |
||
81 | |||
82 | $this->assertTrue($cd->isCrawler()); |
||
83 | } |
||
84 | |||
85 | /** @test */ |
||
86 | public function user_agent_passed_via_contructor() |
||
92 | |||
93 | /** @test */ |
||
94 | public function http_from_header() |
||
95 | { |
||
96 | $headers = (array) json_decode('{"DOCUMENT_ROOT":"\/home\/test\/public_html","GATEWAY_INTERFACE":"CGI\/1.1","HTTP_ACCEPT":"*\/*","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_CACHE_CONTROL":"no-cache","HTTP_CONNECTION":"Keep-Alive","HTTP_FROM":"googlebot(at)googlebot.com","HTTP_HOST":"www.test.com","HTTP_PRAGMA":"no-cache","HTTP_USER_AGENT":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/28.0.1500.71 Safari\/537.36","PATH":"\/bin:\/usr\/bin","QUERY_STRING":"order=closingDate","REDIRECT_STATUS":"200","REMOTE_ADDR":"127.0.0.1","REMOTE_PORT":"3360","REQUEST_METHOD":"GET","REQUEST_URI":"\/?test=testing","SCRIPT_FILENAME":"\/home\/test\/public_html\/index.php","SCRIPT_NAME":"\/index.php","SERVER_ADDR":"127.0.0.1","SERVER_ADMIN":"[email protected]","SERVER_NAME":"www.test.com","SERVER_PORT":"80","SERVER_PROTOCOL":"HTTP\/1.1","SERVER_SIGNATURE":"","SERVER_SOFTWARE":"Apache","UNIQUE_ID":"Vx6MENRxerBUSDEQgFLAAAAAS","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1461619728.0705,"REQUEST_TIME":1461619728}'); |
||
97 | |||
98 | $cd = new CrawlerDetect($headers); |
||
99 | |||
100 | $this->assertTrue($cd->isCrawler()); |
||
101 | } |
||
102 | |||
103 | /** @test */ |
||
104 | public function the_regex_patterns_are_unique() |
||
110 | |||
111 | /** @test */ |
||
112 | public function there_are_no_regex_collisions() |
||
127 | } |
||
128 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.