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() |
||
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) { |
||
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) |
||
83 | |||
84 | /** |
||
85 | * Data provider. |
||
86 | * |
||
87 | * @see testMagicMethodsThatShouldntBeFlagged() |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | View Code Duplication | public function dataMagicMethodsThatShouldntBeFlagged() { |
|
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) |
||
136 | |||
137 | /** |
||
138 | * Data provider. |
||
139 | * |
||
140 | * @see testNewMagicMethod() |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function dataNewMagicMethod() { |
||
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) |
||
203 | |||
204 | /** |
||
205 | * Data provider. |
||
206 | * |
||
207 | * @see testNewDebugInfo() |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | public function dataNewDebugInfo() { |
||
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) |
||
247 | |||
248 | /** |
||
249 | * Data provider. |
||
250 | * |
||
251 | * @see testChangedToStringMethod() |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | View Code Duplication | public function dataChangedToStringMethod() { |
|
265 | |||
266 | } |
||
267 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.