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 |
||
3 | class UserAgentTest extends PHPUnit_Framework_TestCase |
||
|
|||
4 | { |
||
5 | protected $CrawlerDetect; |
||
6 | |||
7 | public function setUp() |
||
8 | { |
||
9 | $this->CrawlerDetect = new Jaybizzle\CrawlerDetect\CrawlerDetect(); |
||
10 | } |
||
11 | |||
12 | public function testBots() |
||
13 | { |
||
14 | $lines = file(__DIR__.'/crawlers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)); |
||
15 | |||
16 | foreach ($lines as $line) { |
||
17 | $test = $this->CrawlerDetect->isCrawler($line); |
||
18 | $this->assertEquals($test, true, $line); |
||
19 | } |
||
20 | } |
||
21 | |||
22 | public function testDevices() |
||
23 | { |
||
24 | $lines = file(__DIR__.'/devices.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)); |
||
25 | |||
26 | foreach ($lines as $line) { |
||
27 | $test = $this->CrawlerDetect->isCrawler($line); |
||
28 | $this->assertEquals($test, false, $line); |
||
29 | } |
||
30 | } |
||
31 | } |
||
32 |
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.