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:
Complex classes like PHPCSHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PHPCSHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class PHPCSHelper |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * Get the PHPCS version number. |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | public static function getVersion() |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Pass config data to PHPCS. |
||
48 | * |
||
49 | * PHPCS cross-version compatibility helper. |
||
50 | * |
||
51 | * @param string $key The name of the config value. |
||
52 | * @param string|null $value The value to set. If null, the config entry |
||
53 | * is deleted, reverting it to the default value. |
||
54 | * @param boolean $temp Set this config data temporarily for this script run. |
||
55 | * This will not write the config data to the config file. |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | public static function setConfigData($key, $value, $temp = false) |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Get the value of a single PHPCS config key. |
||
73 | * |
||
74 | * @param string $key The name of the config value. |
||
75 | * |
||
76 | * @return string|null |
||
77 | */ |
||
78 | public static function getConfigData($key) |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Returns the name of the class that the specified class extends |
||
92 | * (works for classes, anonymous classes and interfaces). |
||
93 | * |
||
94 | * Returns FALSE on error or if there is no extended class name. |
||
95 | * |
||
96 | * {@internal Duplicate of same method as contained in the `\PHP_CodeSniffer_File` |
||
97 | * class, but with some improvements which have been introduced in |
||
98 | * PHPCS 2.8.0. |
||
99 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/commit/0011d448119d4c568e3ac1f825ae78815bf2cc34}. |
||
100 | * |
||
101 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
102 | * that, this method can be removed and calls to it replaced with |
||
103 | * `$phpcsFile->findExtendedClassName($stackPtr)` calls. |
||
104 | * |
||
105 | * Last synced with PHPCS version: PHPCS 3.1.0-alpha at commit a9efcc9b0703f3f9f4a900623d4e97128a6aafc6}} |
||
106 | * |
||
107 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
108 | * @param int $stackPtr The position of the class token in the stack. |
||
109 | * |
||
110 | * @return string|false |
||
111 | */ |
||
112 | public static function findExtendedClassName(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
163 | * |
||
164 | * Returns FALSE on error or if there are no implemented interface names. |
||
165 | * |
||
166 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
167 | * This method also includes an improvement we use which was only introduced |
||
168 | * in PHPCS 2.8.0, so only defer to upstream for higher versions. |
||
169 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
170 | * that, this method can be removed and calls to it replaced with |
||
171 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
172 | * |
||
173 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
174 | * @param int $stackPtr The position of the class token. |
||
175 | * |
||
176 | * @return array|false |
||
177 | */ |
||
178 | public static function findImplementedInterfaceNames(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Returns the method parameters for the specified function token. |
||
231 | * |
||
232 | * Each parameter is in the following format: |
||
233 | * |
||
234 | * <code> |
||
235 | * 0 => array( |
||
236 | * 'token' => int, // The position of the var in the token stack. |
||
237 | * 'name' => '$var', // The variable name. |
||
238 | * 'content' => string, // The full content of the variable definition. |
||
239 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
240 | * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
||
241 | * 'type_hint' => string, // The type hint for the variable. |
||
242 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
243 | * ) |
||
244 | * </code> |
||
245 | * |
||
246 | * Parameters with default values have an additional array index of |
||
247 | * 'default' with the value of the default as a string. |
||
248 | * |
||
249 | * {@internal Duplicate of same method as contained in the `\PHP_CodeSniffer_File` |
||
250 | * class, but with some improvements which have been introduced in |
||
251 | * PHPCS 2.8.0. |
||
252 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117}, |
||
253 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and |
||
254 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}. |
||
255 | * |
||
256 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
257 | * that, this method can be removed and calls to it replaced with |
||
258 | * `$phpcsFile->getMethodParameters($stackPtr)` calls. |
||
259 | * |
||
260 | * NOTE: This version does not deal with the new T_NULLABLE token type. |
||
261 | * This token is included upstream only in 2.8.0+ and as we defer to upstream |
||
262 | * in that case, no need to deal with it here. |
||
263 | * |
||
264 | * Last synced with PHPCS version: PHPCS 2.9.0-alpha at commit f1511adad043edfd6d2e595e77385c32577eb2bc}} |
||
265 | * |
||
266 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
267 | * @param int $stackPtr The position in the stack of the |
||
268 | * function token to acquire the |
||
269 | * parameters for. |
||
270 | * |
||
271 | * @return array|false |
||
272 | * @throws \PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
273 | * type T_FUNCTION or T_CLOSURE. |
||
274 | */ |
||
275 | public static function getMethodParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
433 | } |
||
434 |