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 NewInterfacesSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/new_interfaces.php'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * testNewInterface |
||
| 23 | * |
||
| 24 | * @dataProvider dataNewInterface |
||
| 25 | * |
||
| 26 | * @param string $interfaceName Interface name. |
||
| 27 | * @param string $lastVersionBefore The PHP version just *before* the class was introduced. |
||
| 28 | * @param array $lines The line numbers in the test file which apply to this class. |
||
| 29 | * @param string $okVersion A PHP version in which the class was ok to be used. |
||
| 30 | * |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | public function testNewInterface($interfaceName, $lastVersionBefore, $lines, $okVersion) |
||
| 34 | { |
||
| 35 | $file = $this->sniffFile(self::TEST_FILE, $lastVersionBefore); |
||
| 36 | foreach ($lines as $line) { |
||
| 37 | $this->assertError($file, $line, "The built-in interface {$interfaceName} is not present in PHP version {$lastVersionBefore} or earlier"); |
||
| 38 | } |
||
| 39 | |||
| 40 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 41 | foreach ($lines as $line) { |
||
| 42 | $this->assertNoViolation($file, $line); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Data provider. |
||
| 48 | * |
||
| 49 | * @see testNewInterface() |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public function dataNewInterface() |
||
| 54 | { |
||
| 55 | return array( |
||
| 56 | array('Countable', '5.0', array(3, 17), '5.5'), |
||
| 57 | array('OuterIterator', '5.0', array(4), '5.5'), |
||
| 58 | array('RecursiveIterator', '5.0', array(5), '5.5'), |
||
| 59 | array('SeekableIterator', '5.0', array(6), '5.5'), |
||
| 60 | array('Serializable', '5.0', array(7), '5.5'), |
||
| 61 | array('SplObserver', '5.0', array(11), '5.5'), |
||
| 62 | array('SplSubject', '5.0', array(12, 17), '5.5'), |
||
| 63 | array('JsonSerializable', '5.3', array(13, 17), '5.5'), |
||
| 64 | array('SessionHandlerInterface', '5.3', array(14), '5.5'), |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Test unsupported methods |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | public function testUnsupportedMethods() |
||
| 74 | { |
||
| 75 | $file = $this->sniffFile('sniff-examples/new_interfaces.php'); |
||
| 76 | $this->assertError($file, 8, 'Classes that implement interface Serializable do not support the method __sleep(). See http://php.net/serializable'); |
||
| 77 | $this->assertError($file, 9, 'Classes that implement interface Serializable do not support the method __wakeup(). See http://php.net/serializable'); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Test interfaces in different cases. |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function testCaseInsensitive() |
||
| 86 | { |
||
| 87 | $file = $this->sniffFile('sniff-examples/new_interfaces.php', '5.0'); |
||
| 88 | $this->assertError($file, 20, 'The built-in interface COUNTABLE is not present in PHP version 5.0 or earlier'); |
||
| 89 | $this->assertError($file, 21, 'The built-in interface countable is not present in PHP version 5.0 or earlier'); |
||
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 |