Complex classes like VartypePHP5 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 VartypePHP5, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class VartypePHP5 { |
||
|
|||
28 | |||
29 | /** |
||
30 | * The PHP5 specific tests which will overrule the PHP4 compatible tests. |
||
31 | * |
||
32 | * @var array $tests Multi-dimensional array. |
||
33 | */ |
||
34 | public static $tests = array( |
||
35 | |||
36 | /* |
||
37 | * String comparison functions. |
||
38 | * |
||
39 | * @see class.vartype-compare.php |
||
40 | */ |
||
41 | 'strcmp' => array( |
||
42 | 'function' => 'VartypePHP5::compare_strings( $a, $b, "strcmp" );', |
||
43 | ), |
||
44 | 'strcasecmp' => array( |
||
45 | 'function' => 'VartypePHP5::compare_strings( $a, $b, "strcasecmp" );', |
||
46 | ), |
||
47 | 'strnatcmp' => array( |
||
48 | 'function' => 'VartypePHP5::compare_strings( $a, $b, "strnatcmp" );', |
||
49 | ), |
||
50 | 'strnatcasecmp' => array( |
||
51 | 'function' => 'VartypePHP5::compare_strings( $a, $b, "strnatcasecmp" );', |
||
52 | ), |
||
53 | 'strcoll' => array( |
||
54 | 'function' => 'VartypePHP5::compare_strings( $a, $b, "strcoll" );', |
||
55 | ), |
||
56 | 'similar_text' => array( |
||
57 | 'function' => 'VartypePHP5::compare_strings( $a, $b, "similar_text" );', |
||
58 | ), |
||
59 | 'levenshtein' => array( |
||
60 | 'function' => 'VartypePHP5::compare_strings( $a, $b, "levenshtein" );', |
||
61 | ), |
||
62 | |||
63 | |||
64 | |||
65 | /* |
||
66 | * Loose type juggling. |
||
67 | * |
||
68 | * @see class.vartype-test.php |
||
69 | */ |
||
70 | 'juggle_int' => array( |
||
71 | 'function' => ' |
||
72 | try { |
||
73 | if ( ! is_array( $x ) && ( PHP_VERSION_ID > 50005 || ! is_object( $x ) ) ) { |
||
74 | $x = $x + 0; |
||
75 | if ( is_int( $x ) ) { |
||
76 | pr_int( $x ); |
||
77 | } |
||
78 | else if ( is_float( $x ) ) { |
||
79 | pr_flt( $x ); |
||
80 | } |
||
81 | else { |
||
82 | pr_var( $x, \'\', true, true ); |
||
83 | } |
||
84 | } |
||
85 | else { |
||
86 | trigger_error( \'Unsupported operand types\', E_USER_ERROR ); |
||
87 | } |
||
88 | } |
||
89 | catch ( Exception $e ) { |
||
90 | $message = $e->getMessage(); |
||
91 | $key = array_search( $message, $GLOBALS[\'encountered_errors\'] ); |
||
92 | if ( $key === false ) { |
||
93 | $GLOBALS[\'encountered_errors\'][] = $message; |
||
94 | $key = array_search( $message, $GLOBALS[\'encountered_errors\'] ); |
||
95 | } |
||
96 | echo \'<span class="error">Fatal error <a href="#\', $GLOBALS[\'test\'], \'-errors">#\', ( $key + 1 ), \'</a></span>\'; |
||
97 | } |
||
98 | ', |
||
99 | ), |
||
100 | 'juggle_flt' => array( |
||
101 | 'function' => ' |
||
102 | try { |
||
103 | if ( ! is_array( $x ) && ( PHP_VERSION_ID > 50005 || ! is_object( $x ) ) ) { |
||
104 | $r = $x + 0.0; |
||
105 | if ( is_float( $r ) ) { |
||
106 | pr_flt( $r ); |
||
107 | } |
||
108 | else if ( is_int( $r ) ) { |
||
109 | pr_int( $r ); |
||
110 | } |
||
111 | else { |
||
112 | pr_var( $r, \'\', true, true ); |
||
113 | } |
||
114 | } |
||
115 | else { |
||
116 | trigger_error( \'Unsupported operand types\', E_USER_ERROR ); |
||
117 | } |
||
118 | } |
||
119 | catch ( Exception $e ) { |
||
120 | $message = $e->getMessage(); |
||
121 | $key = array_search( $message, $GLOBALS[\'encountered_errors\'] ); |
||
122 | if ( $key === false ) { |
||
123 | $GLOBALS[\'encountered_errors\'][] = $message; |
||
124 | $key = array_search( $message, $GLOBALS[\'encountered_errors\'] ); |
||
125 | } |
||
126 | echo \'<span class="error">Fatal error <a href="#\', $GLOBALS[\'test\'], \'-errors">#\', ( $key + 1 ), \'</a></span>\'; |
||
127 | } |
||
128 | ', |
||
129 | ), |
||
130 | |||
131 | |||
132 | /* |
||
133 | * Some object related functions. |
||
134 | * |
||
135 | * @see class.vartype-test.php |
||
136 | */ |
||
137 | 'instanceof' => array( |
||
138 | 'function' => '$c = \'TestObject\'; $r = ( $x instanceof $c ); if ( is_bool( $r ) ) { pr_bool( $r ); } else { pr_var( $r, \'\', true, true ); }', |
||
139 | ), |
||
140 | ); |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Helper method to retrieve the static variable. |
||
145 | * Needed to prevent parse error in PHP4.. *sigh*. |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | public static function get_tests() { |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Ensure we clone an object before using it to avoid contamination by results of previous actions. |
||
156 | * |
||
157 | * @param mixed $value |
||
158 | * |
||
159 | * @return mixed |
||
160 | */ |
||
161 | public static function generate_value( $value ) { |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Smarter way to compare strings in PHP5. |
||
177 | * |
||
178 | * @param mixed $var1 |
||
179 | * @param mixed $var2 |
||
180 | * @param string $function |
||
181 | */ |
||
182 | public static function compare_strings( $var1, $var2, $function ) { |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Helper function to handle exceptions from the string compare function. |
||
208 | * |
||
209 | * @param string $message The error message. |
||
210 | */ |
||
211 | public static function handle_exception( $message ) { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * Run tests using the filter extension. |
||
219 | * |
||
220 | * @param mixed $value Value to test. |
||
221 | * @param string|null $expected Expected variable type of the output of the test. |
||
222 | * @param int $filter The Filter to apply. |
||
223 | * @param mixed|null $flags Which filter flags to apply. |
||
224 | * @param mixed|null $options Which options to apply. |
||
225 | */ |
||
226 | public static function filter_combined( $value, $expected = null, $filter = FILTER_DEFAULT, $flags = null, $options = null ) { |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Helper function: Run tests using the `filter_var()` function. |
||
244 | * |
||
245 | * @param mixed $value Value to test. |
||
246 | * @param string|null $expected Expected variable type of the output of the test. |
||
247 | * @param int $filter The Filter to apply. |
||
248 | * @param mixed|null $flags Which filter flags to apply. |
||
249 | * @param mixed|null $options Which options to apply. |
||
250 | */ |
||
251 | public static function do_filter_var( $value, $expected = null, $filter = FILTER_DEFAULT, $flags = null, $options = null ) { |
||
269 | |||
270 | |||
271 | /** |
||
272 | * Helper function: Run tests using the `filter_var_array()` function. |
||
273 | * |
||
274 | * @param mixed $value Value to test. |
||
275 | * @param int $filter The Filter to apply. |
||
276 | * @param mixed|null $flags Which filter flags to apply. |
||
277 | * @param mixed|null $options Which options to apply. |
||
278 | */ |
||
279 | public static function do_filter_var_array( $value, $filter = FILTER_DEFAULT, $flags = null, $options = null ) { |
||
302 | |||
303 | |||
304 | /** |
||
305 | * Helper function to print the filter result. |
||
306 | * |
||
307 | * @param mixed $result |
||
308 | * @param string|null $expected Expected variable type of the output of the test. |
||
309 | */ |
||
310 | public static function print_typed_result( $result, $expected = null ) { |
||
333 | } |
||
334 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.