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 position of the last non-whitespace token in a statement. |
||
92 | * |
||
93 | * {@internal Duplicate of same method as contained in the `\PHP_CodeSniffer_File` |
||
94 | * class and introduced in PHPCS 2.1.0. |
||
95 | * |
||
96 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
97 | * that, this method can be removed and calls to it replaced with |
||
98 | * `$phpcsFile->findEndOfStatement($start, $ignore)` calls. |
||
99 | * |
||
100 | * Last synced with PHPCS version: PHPCS 3.3.0-alpha at commit f5d899dcb5c534a1c3cca34668624517856ba823}} |
||
101 | * |
||
102 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
103 | * @param int $start The position to start searching from in the token stack. |
||
104 | * @param int|array $ignore Token types that should not be considered stop points. |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | public static function findEndOfStatement(\PHP_CodeSniffer_File $phpcsFile, $start, $ignore = null) |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Returns the name of the class that the specified class extends |
||
187 | * (works for classes, anonymous classes and interfaces). |
||
188 | * |
||
189 | * Returns FALSE on error or if there is no extended class name. |
||
190 | * |
||
191 | * {@internal Duplicate of same method as contained in the `\PHP_CodeSniffer_File` |
||
192 | * class, but with some improvements which have been introduced in |
||
193 | * PHPCS 2.8.0. |
||
194 | * {@link https://github.com/squizlabs/PHP_CodeSniffer/commit/0011d448119d4c568e3ac1f825ae78815bf2cc34}. |
||
195 | * |
||
196 | * Once the minimum supported PHPCS version for this standard goes beyond |
||
197 | * that, this method can be removed and calls to it replaced with |
||
198 | * `$phpcsFile->findExtendedClassName($stackPtr)` calls. |
||
199 | * |
||
200 | * Last synced with PHPCS version: PHPCS 3.1.0-alpha at commit a9efcc9b0703f3f9f4a900623d4e97128a6aafc6}} |
||
201 | * |
||
202 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
203 | * @param int $stackPtr The position of the class token in the stack. |
||
204 | * |
||
205 | * @return string|false |
||
206 | */ |
||
207 | public static function findExtendedClassName(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Returns the name(s) of the interface(s) that the specified class implements. |
||
258 | * |
||
259 | * Returns FALSE on error or if there are no implemented interface names. |
||
260 | * |
||
261 | * {@internal Duplicate of same method as introduced in PHPCS 2.7. |
||
262 | * This method also includes an improvement we use which was only introduced |
||
263 | * in PHPCS 2.8.0, so only defer to upstream for higher versions. |
||
264 | * Once the minimum supported PHPCS version for this sniff library goes beyond |
||
265 | * that, this method can be removed and calls to it replaced with |
||
266 | * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
||
267 | * |
||
268 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
269 | * @param int $stackPtr The position of the class token. |
||
270 | * |
||
271 | * @return array|false |
||
272 | */ |
||
273 | public static function findImplementedInterfaceNames(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
322 | |||
323 | |||
324 | /** |
||
325 | * Returns the method parameters for the specified function token. |
||
326 | * |
||
327 | * Each parameter is in the following format: |
||
328 | * |
||
329 | * <code> |
||
330 | * 0 => array( |
||
331 | * 'name' => '$var', // The variable name. |
||
332 | * 'token' => integer, // The stack pointer to the variable name. |
||
333 | * 'content' => string, // The full content of the variable definition. |
||
334 | * 'pass_by_reference' => boolean, // Is the variable passed by reference? |
||
335 | * 'variable_length' => boolean, // Is the param of variable length through use of `...` ? |
||
336 | * 'type_hint' => string, // The type hint for the variable. |
||
337 | * 'type_hint_token' => integer, // The stack pointer to the type hint |
||
338 | * // or false if there is no type hint. |
||
339 | * 'nullable_type' => boolean, // Is the variable using a nullable type? |
||
340 | * ) |
||
341 | * </code> |
||
342 | * |
||
343 | * Parameters with default values have an additional array index of |
||
344 | * 'default' with the value of the default as a string. |
||
345 | * |
||
346 | * {@internal Duplicate of same method as contained in the `\PHP_CodeSniffer_File` |
||
347 | * class. |
||
348 | * |
||
349 | * Last synced with PHPCS version: PHPCS 3.3.0-alpha at commit 53a28408d345044c0360c2c1b4a2aaebf4a3b8c9}} |
||
350 | * |
||
351 | * @param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
||
352 | * @param int $stackPtr The position in the stack of the |
||
353 | * function token to acquire the |
||
354 | * parameters for. |
||
355 | * |
||
356 | * @return array|false |
||
357 | * @throws \PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
||
358 | * type T_FUNCTION or T_CLOSURE. |
||
359 | */ |
||
360 | public static function getMethodParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
542 | } |
||
543 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: