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 NewMagicMethodsSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Whether or not traits will be recognized in PHPCS. |
||
| 20 | * |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | protected static $recognizesTraits = true; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Set up skip condition. |
||
| 28 | */ |
||
| 29 | public static function setUpBeforeClass() |
||
| 30 | { |
||
| 31 | // When using PHPCS 1.x combined with PHP 5.3 or lower, traits are not recognized. |
||
| 32 | if (version_compare(PHP_CodeSniffer::VERSION, '2.0', '<') && version_compare(phpversion(), '5.4', '<')) { |
||
| 33 | self::$recognizesTraits = false; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Get the correct test file. |
||
| 40 | * |
||
| 41 | * (@internal |
||
| 42 | * The test file has been split into two: |
||
| 43 | * - one covering classes and interfaces |
||
| 44 | * - one covering traits |
||
| 45 | * |
||
| 46 | * This is to avoid test failing because PHPCS 1.x gets confused about the scope |
||
| 47 | * openers/closers when run on PHP 5.3 or lower. |
||
| 48 | * In a 'normal' situation you won't often find classes, interfaces and traits all |
||
| 49 | * mixed in one file anyway, so this issue for which this is a work-around, |
||
| 50 | * should not cause real world issues anyway.}} |
||
| 51 | * |
||
| 52 | * @param bool $isTrait Whether to load the class/interface test file or the trait test file. |
||
| 53 | * @param string $testVersion Value of 'testVersion' to set on PHPCS object. |
||
| 54 | * |
||
| 55 | * @return PHP_CodeSniffer_File File object|false |
||
| 56 | */ |
||
| 57 | protected function getTestFile($isTrait, $testVersion = null) { |
||
| 58 | if ($isTrait === false) { |
||
| 59 | return $this->sniffFile('sniff-examples/new_magic_methods.php', $testVersion); |
||
| 60 | } |
||
| 61 | else { |
||
| 62 | return $this->sniffFile('sniff-examples/new_magic_methods_traits.php', $testVersion); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Test magic methods that shouldn't be flagged by this sniff. |
||
| 69 | * |
||
| 70 | * @group newMagicMethods |
||
| 71 | * |
||
| 72 | * @dataProvider dataMagicMethodsThatShouldntBeFlagged |
||
| 73 | * |
||
| 74 | * @param int $line The line number. |
||
| 75 | * |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function testMagicMethodsThatShouldntBeFlagged($line) |
||
| 79 | { |
||
| 80 | $file = $this->getTestFile(false, '5.0'); |
||
| 81 | $this->assertNoViolation($file, $line); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Data provider. |
||
| 86 | * |
||
| 87 | * @see testMagicMethodsThatShouldntBeFlagged() |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function dataMagicMethodsThatShouldntBeFlagged() { |
||
| 92 | return array( |
||
| 93 | array(8), |
||
| 94 | array(9), |
||
| 95 | array(10), |
||
| 96 | array(11), |
||
| 97 | array(12), |
||
| 98 | array(13), |
||
| 99 | array(14), |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * testNewMagicMethod |
||
| 106 | * |
||
| 107 | * @group newMagicMethods |
||
| 108 | * |
||
| 109 | * @dataProvider dataNewMagicMethod |
||
| 110 | * |
||
| 111 | * @param string $methodName Name of the method. |
||
| 112 | * @param string $lastVersionBefore The PHP version just *before* the method became magic. |
||
| 113 | * @param array $lines The line numbers in the test file which apply to this method. |
||
| 114 | * @param string $okVersion A PHP version in which the method was magic. |
||
| 115 | * @param bool $isTrait Whether the test relates to a method in a trait. |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | public function testNewMagicMethod($methodName, $lastVersionBefore, $lines, $okVersion, $isTrait = false) |
||
| 120 | { |
||
| 121 | if ($isTrait === true && self::$recognizesTraits === false) { |
||
| 122 | $this->markTestSkipped(); |
||
| 123 | return; |
||
| 124 | } |
||
| 125 | |||
| 126 | $file = $this->getTestFile($isTrait, $lastVersionBefore); |
||
| 127 | foreach ($lines as $line) { |
||
| 128 | $this->assertWarning($file, $line, "The method {$methodName}() was not magical in PHP version {$lastVersionBefore} and earlier. The associated magic functionality will not be invoked."); |
||
| 129 | } |
||
| 130 | |||
| 131 | $file = $this->getTestFile($isTrait, $okVersion); |
||
| 132 | foreach ($lines as $line) { |
||
| 133 | $this->assertNoViolation($file, $line); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Data provider. |
||
| 139 | * |
||
| 140 | * @see testNewMagicMethod() |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function dataNewMagicMethod() { |
||
| 145 | return array( |
||
| 146 | // new_magic_methods.php |
||
| 147 | array('__get', '4.4', array(22, 34), '5.0'), |
||
| 148 | array('__isset', '5.0', array(23, 35), '5.1'), |
||
| 149 | array('__unset', '5.0', array(24, 36), '5.1'), |
||
| 150 | array('__set_state', '5.0', array(25, 37), '5.1'), |
||
| 151 | array('__callStatic', '5.2', array(27, 39), '5.3'), |
||
| 152 | array('__invoke', '5.2', array(28, 40), '5.3'), |
||
| 153 | |||
| 154 | // new_magic_methods_traits.php |
||
| 155 | array('__get', '4.4', array(5), '5.0', true), |
||
| 156 | array('__isset', '5.0', array(6), '5.1', true), |
||
| 157 | array('__unset', '5.0', array(7), '5.1', true), |
||
| 158 | array('__set_state', '5.0', array(8), '5.1', true), |
||
| 159 | array('__callStatic', '5.2', array(10), '5.3', true), |
||
| 160 | array('__invoke', '5.2', array(11), '5.3', true), |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * testNewDebugInfo |
||
| 167 | * |
||
| 168 | * {@internal Separate test for __debugInfo() as the noViolation check needs a wrapper |
||
| 169 | * for PHPCS 2.5.1 as the Naming Convention sniff in PHPCS < 2.5.1 does not recognize |
||
| 170 | * __debugInfo() yet, causing the noViolation sniff to fail. |
||
| 171 | * |
||
| 172 | * @group newMagicMethods |
||
| 173 | * |
||
| 174 | * @dataProvider dataNewDebugInfo |
||
| 175 | * |
||
| 176 | * @param string $methodName Name of the method. |
||
| 177 | * @param string $lastVersionBefore The PHP version just *before* the method became magic. |
||
| 178 | * @param array $lines The line numbers in the test file which apply to this method. |
||
| 179 | * @param string $okVersion A PHP version in which the method was magic. |
||
| 180 | * @param bool $isTrait Whether the test relates to a method in a trait. |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function testNewDebugInfo($methodName, $lastVersionBefore, $lines, $okVersion, $isTrait = false) |
||
| 185 | { |
||
| 186 | if ($isTrait === true && self::$recognizesTraits === false) { |
||
| 187 | $this->markTestSkipped(); |
||
| 188 | return; |
||
| 189 | } |
||
| 190 | |||
| 191 | $file = $this->getTestFile($isTrait, $lastVersionBefore); |
||
| 192 | foreach ($lines as $line) { |
||
| 193 | $this->assertWarning($file, $line, "The method {$methodName}() was not magical in PHP version {$lastVersionBefore} and earlier. The associated magic functionality will not be invoked."); |
||
| 194 | } |
||
| 195 | |||
| 196 | if (version_compare(PHP_CodeSniffer::VERSION, '2.5.1', '>=')) { |
||
| 197 | $file = $this->getTestFile($isTrait, $okVersion); |
||
| 198 | foreach ($lines as $line) { |
||
| 199 | $this->assertNoViolation($file, $line); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Data provider. |
||
| 206 | * |
||
| 207 | * @see testNewDebugInfo() |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function dataNewDebugInfo() { |
||
| 212 | return array( |
||
| 213 | // new_magic_methods.php |
||
| 214 | array('__debugInfo', '5.5', array(29, 41), '5.6'), |
||
| 215 | |||
| 216 | // new_magic_methods_traits.php |
||
| 217 | array('__debugInfo', '5.5', array(12), '5.6', true), |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * testChangedToStringMethod |
||
| 224 | * |
||
| 225 | * @group newMagicMethods |
||
| 226 | * |
||
| 227 | * @dataProvider dataChangedToStringMethod |
||
| 228 | * |
||
| 229 | * @param int $line The line number. |
||
| 230 | * @param bool $isTrait Whether the test relates to a method in a trait. |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | public function testChangedToStringMethod($line, $isTrait = false) |
||
| 235 | { |
||
| 236 | if ($isTrait === true && self::$recognizesTraits === false) { |
||
| 237 | $this->markTestSkipped(); |
||
| 238 | return; |
||
| 239 | } |
||
| 240 | |||
| 241 | $file = $this->getTestFile($isTrait, '5.1'); |
||
| 242 | $this->assertWarning($file, $line, "The method __toString() was not truly magical in PHP version 5.1 and earlier. The associated magic functionality will only be called when directly combined with echo or print."); |
||
| 243 | |||
| 244 | $file = $this->getTestFile($isTrait, '5.2'); |
||
| 245 | $this->assertNoViolation($file, $line); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Data provider. |
||
| 250 | * |
||
| 251 | * @see testChangedToStringMethod() |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function dataChangedToStringMethod() { |
||
| 256 | return array( |
||
| 257 | // new_magic_methods.php |
||
| 258 | array(26), |
||
| 259 | array(38), |
||
| 260 | |||
| 261 | // new_magic_methods_traits.php |
||
| 262 | array(9, true), |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | } |
||
| 267 |