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 RemovedImplodeFlexibleParamOrderSniff 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 RemovedImplodeFlexibleParamOrderSniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class RemovedImplodeFlexibleParamOrderSniff extends AbstractFunctionCallParameterSniff |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * Functions to check for. |
||
34 | * |
||
35 | * @since 9.3.0 |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $targetFunctions = array( |
||
40 | 'implode' => true, |
||
41 | 'join' => true, |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * List of PHP native constants which should be recognized as text strings. |
||
46 | * |
||
47 | * @since 9.3.0 |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $constantStrings = array( |
||
52 | 'DIRECTORY_SEPARATOR' => true, |
||
53 | 'PHP_EOL' => true, |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * List of PHP native functions which should be recognized as returning an array. |
||
58 | * |
||
59 | * Note: The array_*() functions will always be taken into account. |
||
60 | * |
||
61 | * @since 9.3.0 |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | private $arrayFunctions = array( |
||
66 | 'compact' => true, |
||
67 | 'explode' => true, |
||
68 | 'range' => true, |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * List of PHP native array functions which should *not* be recognized as returning an array. |
||
73 | * |
||
74 | * @since 9.3.0 |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | private $arrayFunctionExceptions = array( |
||
79 | 'array_key_exists' => true, |
||
80 | 'array_key_first' => true, |
||
81 | 'array_key_last' => true, |
||
82 | 'array_multisort' => true, |
||
83 | 'array_pop' => true, |
||
84 | 'array_product' => true, |
||
85 | 'array_push' => true, |
||
86 | 'array_search' => true, |
||
87 | 'array_shift' => true, |
||
88 | 'array_sum' => true, |
||
89 | 'array_unshift' => true, |
||
90 | 'array_walk_recursive' => true, |
||
91 | 'array_walk' => true, |
||
92 | ); |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Do a version check to determine if this sniff needs to run at all. |
||
97 | * |
||
98 | * @since 9.3.0 |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | protected function bowOutEarly() |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Process the parameters of a matched function. |
||
110 | * |
||
111 | * @since 9.3.0 |
||
112 | * |
||
113 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
114 | * @param int $stackPtr The position of the current token in the stack. |
||
115 | * @param string $functionName The token content (function name) which was matched. |
||
116 | * @param array $parameters Array with information about the parameters. |
||
117 | * |
||
118 | * @return int|void Integer stack pointer to skip forward or void to continue |
||
119 | * normal file processing. |
||
120 | */ |
||
121 | public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters) |
||
289 | |||
290 | |||
291 | /** |
||
292 | * Throw the error/warning. |
||
293 | * |
||
294 | * @since 9.3.0 |
||
295 | * |
||
296 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
297 | * @param int $stackPtr The position of the current token in the stack. |
||
298 | * @param string $functionName The token content (function name) which was matched. |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | protected function throwNotice(File $phpcsFile, $stackPtr, $functionName) |
||
323 | } |
||
324 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.