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 RemovedHashAlgorithmsSniffTest extends BaseSniffTest |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Sniffed file |
||
| 20 | * |
||
| 21 | * @var PHP_CodeSniffer_File |
||
| 22 | */ |
||
| 23 | protected $_sniffFile; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * setUp |
||
| 27 | * |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public function setUp() |
||
| 31 | { |
||
| 32 | parent::setUp(); |
||
| 33 | |||
| 34 | $this->_sniffFile = $this->sniffFile('sniff-examples/removed_hash_algorithms.php'); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * testRemovedHashAlgorithms |
||
| 39 | * |
||
| 40 | * @dataProvider dataRemovedHashAlgorithms |
||
| 41 | * |
||
| 42 | * @param int $line Line number on which an error should occur. |
||
| 43 | * |
||
| 44 | * @return void |
||
| 45 | */ |
||
| 46 | public function testRemovedHashAlgorithms($line) |
||
| 47 | { |
||
| 48 | $this->assertError($this->_sniffFile, $line, 'The Salsa10 and Salsa20 hash algorithms have been removed since PHP 5.4'); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * dataRemovedHashAlgorithms |
||
| 53 | * |
||
| 54 | * @see testRemovedHashAlgorithms() |
||
| 55 | * |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | public function dataRemovedHashAlgorithms() |
||
| 59 | { |
||
| 60 | return array( |
||
| 61 | array(4), |
||
| 62 | array(5), |
||
| 63 | array(6), |
||
| 64 | array(7), |
||
| 65 | array(9), |
||
| 66 | array(11), |
||
| 67 | array(13), |
||
| 68 | array(15), |
||
| 69 | array(16), |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * testOkHashAlgorithm |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function testOkHashAlgorithm() |
||
| 80 | { |
||
| 81 | $this->assertNoViolation($this->_sniffFile, 3); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * testIgnoreSecondParameter |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function testIgnoreSecondParameter() |
||
| 90 | { |
||
| 91 | $this->assertNoViolation($this->_sniffFile, 17); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * testIgnoreNonFunctionCall |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function testIgnoreNonFunctionCall() |
||
| 100 | { |
||
| 101 | $this->assertNoViolation($this->_sniffFile, 18); |
||
| 102 | } |
||
| 103 | |||
| 104 | } |
||
| 105 |