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 RemovedExtensionsSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/removed_extensions.php'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Sniffed file |
||
| 23 | * |
||
| 24 | * @var PHP_CodeSniffer_File |
||
| 25 | */ |
||
| 26 | protected $_sniffFile; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * setUp |
||
| 30 | * |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | public function setUp() |
||
| 34 | { |
||
| 35 | parent::setUp(); |
||
| 36 | |||
| 37 | $this->_sniffFile = $this->sniffFile(self::TEST_FILE); |
||
| 38 | } |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * testRemovedExtension |
||
| 43 | * |
||
| 44 | * @dataProvider dataRemovedExtension |
||
| 45 | * |
||
| 46 | * @param string $extensionName Name of the PHP extension. |
||
| 47 | * @param string $removedIn The PHP version in which the extension was removed. |
||
| 48 | * @param array $lines The line numbers in the test file which apply to this extension. |
||
| 49 | * @param string $okVersion A PHP version in which the extension was still present. |
||
| 50 | * @param string $removedVersion Optional PHP version to test removal message with - |
||
| 51 | * if different from the $removedIn version. |
||
| 52 | * |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | public function testRemovedExtension($extensionName, $removedIn, $lines, $okVersion, $removedVersion = null) |
||
| 56 | { |
||
| 57 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 58 | foreach($lines as $line) { |
||
| 59 | $this->assertNoViolation($file, $line); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (isset($removedVersion)){ |
||
| 63 | $file = $this->sniffFile(self::TEST_FILE, $removedVersion); |
||
| 64 | } |
||
| 65 | else { |
||
| 66 | $file = $this->sniffFile(self::TEST_FILE, $removedIn); |
||
| 67 | } |
||
| 68 | foreach($lines as $line) { |
||
| 69 | $this->assertError($file, $line, "Extension '{$extensionName}' is removed since PHP {$removedIn}"); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Data provider. |
||
| 75 | * |
||
| 76 | * @see testRemovedExtension() |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function dataRemovedExtension() |
||
| 81 | { |
||
| 82 | return array( |
||
| 83 | array('activescript', '5.1', array(3, 4), '5.0'), |
||
| 84 | array('cpdf', '5.1', array(6, 7, 8), '5.0'), |
||
| 85 | array('dbase', '5.3', array(10), '5.2'), |
||
| 86 | array('dbx', '5.1', array(12), '5.0'), |
||
| 87 | array('dio', '5.1', array(14), '5.0'), |
||
| 88 | array('fam', '5.1', array(16), '5.0'), |
||
| 89 | array('fbsql', '5.3', array(18), '5.2'), |
||
| 90 | array('fdf', '5.3', array(20), '5.2'), |
||
| 91 | array('filepro', '5.2', array(22), '5.1'), |
||
| 92 | array('hw_api', '5.2', array(24), '5.1'), |
||
| 93 | array('ingres', '5.1', array(26), '5.0'), |
||
| 94 | array('ircg', '5.1', array(28), '5.0'), |
||
| 95 | array('mcve', '5.1', array(30), '5.0'), |
||
| 96 | array('ming', '5.3', array(32), '5.2'), |
||
| 97 | array('mnogosearch', '5.1', array(34), '5.0'), |
||
| 98 | array('msql', '5.3', array(36), '5.2'), |
||
| 99 | array('ncurses', '5.3', array(40), '5.2'), |
||
| 100 | array('oracle', '5.1', array(42), '5.0'), |
||
| 101 | array('ovrimos', '5.1', array(44), '5.0'), |
||
| 102 | array('pfpro', '5.3', array(46), '5.2'), |
||
| 103 | array('sqlite', '5.4', array(48), '5.3'), |
||
| 104 | array('sybase', '5.3', array(50), '5.2'), |
||
| 105 | array('w32api', '5.1', array(52), '5.0'), |
||
| 106 | array('yp', '5.1', array(54), '5.0'), |
||
| 107 | array('mssql', '7.0', array(63), '5.6'), |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * testDeprecatedRemovedExtension |
||
| 114 | * |
||
| 115 | * @dataProvider dataDeprecatedRemovedExtension |
||
| 116 | * |
||
| 117 | * @param string $extensionName Name of the PHP extension. |
||
| 118 | * @param string $deprecatedIn The PHP version in which the extension was deprecated. |
||
| 119 | * @param string $removedIn The PHP version in which the extension was removed. |
||
| 120 | * @param array $lines The line numbers in the test file which apply to this extension. |
||
| 121 | * @param string $okVersion A PHP version in which the extension was still present. |
||
| 122 | * @param string $deprecatedVersion Optional PHP version to test deprecation message with - |
||
| 123 | * if different from the $deprecatedIn version. |
||
| 124 | * @param string $removedVersion Optional PHP version to test removal message with - |
||
| 125 | * if different from the $removedIn version. |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | public function testDeprecatedRemovedExtension($extensionName, $deprecatedIn, $removedIn, $lines, $okVersion, $deprecatedVersion = null, $removedVersion = null) |
||
| 130 | { |
||
| 131 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 132 | foreach($lines as $line) { |
||
| 133 | $this->assertNoViolation($file, $line); |
||
| 134 | } |
||
| 135 | |||
| 136 | if (isset($deprecatedVersion)){ |
||
| 137 | $file = $this->sniffFile(self::TEST_FILEE, $deprecatedVersion); |
||
| 138 | } |
||
| 139 | else { |
||
| 140 | $file = $this->sniffFile(self::TEST_FILE, $deprecatedIn); |
||
| 141 | } |
||
| 142 | foreach($lines as $line) { |
||
| 143 | $this->assertWarning($file, $line, "Extension '{$extensionName}' is deprecated since PHP {$deprecatedIn}"); |
||
| 144 | } |
||
| 145 | |||
| 146 | if (isset($removedVersion)){ |
||
| 147 | $file = $this->sniffFile(self::TEST_FILE, $removedVersion); |
||
| 148 | } |
||
| 149 | else { |
||
| 150 | $file = $this->sniffFile(self::TEST_FILE, $removedIn); |
||
| 151 | } |
||
| 152 | foreach($lines as $line) { |
||
| 153 | $this->assertError($file, $line, "Extension '{$extensionName}' is deprecated since PHP {$deprecatedIn} and removed since PHP {$removedIn}"); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Data provider. |
||
| 159 | * |
||
| 160 | * @see testDeprecatedRemovedExtension() |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function dataDeprecatedRemovedExtension() |
||
| 165 | { |
||
| 166 | return array( |
||
| 167 | array('ereg', '5.3', '7.0', array(65), '5.2'), |
||
| 168 | array('mysql_', '5.5', '7.0', array(38), '5.4'), |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * testNotAFunctionCall |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | public function testNotAFunctionCall() |
||
| 179 | { |
||
| 180 | $this->assertNoViolation($this->_sniffFile, 57); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * testFunctionDeclaration |
||
| 185 | * |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | public function testFunctionDeclaration() |
||
| 189 | { |
||
| 190 | $this->assertNoViolation($this->_sniffFile, 58); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * testNewClass |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function testNewClass() |
||
| 199 | { |
||
| 200 | $this->assertNoViolation($this->_sniffFile, 59); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * testMethod |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | public function testMethod() |
||
| 209 | { |
||
| 210 | $this->assertNoViolation($this->_sniffFile, 60); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * testWhiteListing |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function testWhiteListing() |
||
| 219 | { |
||
| 220 | $this->assertNoViolation($this->_sniffFile, 68); |
||
| 221 | } |
||
| 222 | } |
||
| 223 |