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 RemovedFunctionParameterSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | |||
| 19 | const TEST_FILE = 'sniff-examples/removed_function_parameter.php'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * testRemovedParameter |
||
| 23 | * |
||
| 24 | * @dataProvider dataRemovedParameter |
||
| 25 | * |
||
| 26 | * @param string $functionName Function name. |
||
| 27 | * @param string $parameterName Parameter name. |
||
| 28 | * @param string $removedIn The PHP version in which the parameter was removed. |
||
| 29 | * @param array $lines The line numbers in the test file which apply to this class. |
||
| 30 | * @param string $okVersion A PHP version in which the parameter was ok to be used. |
||
| 31 | * @param string $testVersion Optional. A PHP version in which to test for the removal message. |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | public function testRemovedParameter($functionName, $parameterName, $removedIn, $lines, $okVersion, $testVersion = null) |
||
| 36 | { |
||
| 37 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 38 | foreach ($lines as $line) { |
||
| 39 | $this->assertNoViolation($file, $line); |
||
| 40 | } |
||
| 41 | |||
| 42 | if (isset($testVersion)){ |
||
| 43 | $file = $this->sniffFile(self::TEST_FILE, $testVersion); |
||
| 44 | } |
||
| 45 | else { |
||
| 46 | $file = $this->sniffFile(self::TEST_FILE, $removedIn); |
||
| 47 | } |
||
| 48 | foreach ($lines as $line) { |
||
| 49 | $this->assertError($file, $line, "The \"{$parameterName}\" parameter for function {$functionName} was removed in PHP version {$removedIn}"); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Data provider. |
||
| 55 | * |
||
| 56 | * @see testRemovedParameter() |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function dataRemovedParameter() |
||
| 61 | { |
||
| 62 | return array( |
||
| 63 | array('ldap_first_attribute', 'ber_identifier', '5.2.4', array(11), '5.2', '5.3'), |
||
| 64 | array('ldap_next_attribute', 'ber_identifier', '5.2.4', array(12), '5.2', '5.3'), |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * testDeprecatedRemovedParameter |
||
| 71 | * |
||
| 72 | * @dataProvider dataDeprecatedRemovedParameter |
||
| 73 | * |
||
| 74 | * @param string $functionName Function name. |
||
| 75 | * @param string $parameterName Parameter name. |
||
| 76 | * @param string $deprecatedIn The PHP version in which the parameter was deprecated. |
||
| 77 | * @param string $removedIn The PHP version in which the parameter was removed. |
||
| 78 | * @param array $lines The line numbers in the test file which apply to this class. |
||
| 79 | * @param string $okVersion A PHP version in which the parameter was ok to be used. |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function testDeprecatedRemovedParameter($functionName, $parameterName, $deprecatedIn, $removedIn, $lines, $okVersion) |
||
| 84 | { |
||
| 85 | $file = $this->sniffFile(self::TEST_FILE, $okVersion); |
||
| 86 | foreach ($lines as $line) { |
||
| 87 | $this->assertNoViolation($file, $line); |
||
| 88 | } |
||
| 89 | |||
| 90 | $file = $this->sniffFile(self::TEST_FILE, $deprecatedIn); |
||
| 91 | foreach ($lines as $line) { |
||
| 92 | $this->assertWarning($file, $line, "The \"{$parameterName}\" parameter for function {$functionName} was deprecated in PHP version {$deprecatedIn}"); |
||
| 93 | } |
||
| 94 | |||
| 95 | $file = $this->sniffFile(self::TEST_FILE, $removedIn); |
||
| 96 | foreach ($lines as $line) { |
||
| 97 | $this->assertError($file, $line, "The \"{$parameterName}\" parameter for function {$functionName} was deprecated in PHP version {$deprecatedIn} and removed in PHP version {$removedIn}"); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Data provider. |
||
| 103 | * |
||
| 104 | * @see testDeprecatedRemovedParameter() |
||
| 105 | * |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function dataDeprecatedRemovedParameter() |
||
| 109 | { |
||
| 110 | return array( |
||
| 111 | array('mktime', 'is_dst', '5.1', '7.0', array(8), '5.0'), |
||
| 112 | array('gmmktime', 'is_dst', '5.1', '7.0', array(9), '5.0'), |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * testValidParameter |
||
| 119 | * |
||
| 120 | * @dataProvider dataValidParameter |
||
| 121 | * |
||
| 122 | * @param int $line The line number. |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | public function testValidParameter($line) |
||
| 127 | { |
||
| 128 | $file = $this->sniffFile(self::TEST_FILE, '7.0'); |
||
| 129 | $this->assertNoViolation($file, $line); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Data provider. |
||
| 134 | * |
||
| 135 | * @see testValidParameter() |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function dataValidParameter() |
||
| 140 | { |
||
| 141 | return array( |
||
| 142 | array(4), |
||
| 143 | array(5), |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | } |
||
| 148 |