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 NewClassesSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/new_classes.php'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * testNewClass |
||
| 23 | * |
||
| 24 | * @dataProvider dataNewClass |
||
| 25 | * |
||
| 26 | * @param string $className Class 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 testNewClass($className, $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 class {$className} 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 testNewClass() |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public function dataNewClass() |
||
| 54 | { |
||
| 55 | return array( |
||
| 56 | array('DateTime', '5.1', array(25, 65, 105), '5.2'), |
||
| 57 | array('DateTimeZone', '5.1', array(26, 66, 106), '5.2'), |
||
| 58 | array('RegexIterator', '5.1', array(27, 67, 107), '5.2'), |
||
| 59 | array('RecursiveRegexIterator', '5.1', array(28, 68, 108), '5.2'), |
||
| 60 | array('DateInterval', '5.2', array(17, 18, 19, 20, 29, 69, 109), '5.3'), |
||
| 61 | array('DatePeriod', '5.2', array(30, 70, 110), '5.3'), |
||
| 62 | array('Phar', '5.2', array(31, 71, 111), '5.3'), |
||
| 63 | array('PharData', '5.2', array(32, 72, 112), '5.3'), |
||
| 64 | array('PharException', '5.2', array(33, 73, 113), '5.3'), |
||
| 65 | array('PharFileInfo', '5.2', array(34, 74, 114), '5.3'), |
||
| 66 | array('FilesystemIterator', '5.2', array(35, 75, 115), '5.3'), |
||
| 67 | array('GlobIterator', '5.2', array(36, 76, 116), '5.3'), |
||
| 68 | array('MultipleIterator', '5.2', array(37, 77, 117), '5.3'), |
||
| 69 | array('RecursiveTreeIterator', '5.2', array(38, 78, 118), '5.3'), |
||
| 70 | array('SplDoublyLinkedList', '5.2', array(39, 79, 119), '5.3'), |
||
| 71 | array('SplFixedArray', '5.2', array(40, 80, 120), '5.3'), |
||
| 72 | array('SplHeap', '5.2', array(41, 81, 121), '5.3'), |
||
| 73 | array('SplMaxHeap', '5.2', array(42, 82, 122), '5.3'), |
||
| 74 | array('SplMinHeap', '5.2', array(43, 83, 123), '5.3'), |
||
| 75 | array('SplPriorityQueue', '5.2', array(44, 84, 124), '5.3'), |
||
| 76 | array('SplQueue', '5.2', array(45, 85, 125), '5.3'), |
||
| 77 | array('SplStack', '5.2', array(46, 86, 126), '5.3'), |
||
| 78 | array('CallbackFilterIterator', '5.3', array(47, 87, 127), '5.4'), |
||
| 79 | array('RecursiveCallbackFilterIterator', '5.3', array(48, 88, 128), '5.4'), |
||
| 80 | array('ReflectionZendExtension', '5.3', array(49, 89, 129), '5.4'), |
||
| 81 | array('SessionHandler', '5.3', array(50, 90, 130), '5.4'), |
||
| 82 | array('SNMP', '5.3', array(51, 91, 131), '5.4'), |
||
| 83 | array('Transliterator', '5.3', array(52, 92, 132), '5.4'), |
||
| 84 | array('CURLFile', '5.4', array(53, 93, 133), '5.5'), |
||
| 85 | array('DateTimeImmutable', '5.4', array(54, 94, 134), '5.5'), |
||
| 86 | array('IntlCalendar', '5.4', array(55, 95, 135), '5.5'), |
||
| 87 | array('IntlGregorianCalendar', '5.4', array(56, 96, 136), '5.5'), |
||
| 88 | array('IntlTimeZone', '5.4', array(57, 97, 137), '5.5'), |
||
| 89 | array('IntlBreakIterator', '5.4', array(58, 98, 138), '5.5'), |
||
| 90 | array('IntlRuleBasedBreakIterator', '5.4', array(59, 99, 139), '5.5'), |
||
| 91 | array('IntlCodePointBreakIterator', '5.4', array(60, 100, 140), '5.5'), |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * testNoViolation |
||
| 98 | * |
||
| 99 | * @dataProvider dataNoViolation |
||
| 100 | * |
||
| 101 | * @param int $line The line number. |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public function testNoViolation($line) |
||
| 106 | { |
||
| 107 | $file = $this->sniffFile(self::TEST_FILE, '5.3'); |
||
| 108 | $this->assertNoViolation($file, $line); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Data provider. |
||
| 113 | * |
||
| 114 | * @see testNoViolation() |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function dataNoViolation() |
||
| 119 | { |
||
| 120 | return array( |
||
| 121 | array(6), |
||
| 122 | array(7), |
||
| 123 | array(8), |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |