Complex classes like Jetpack_Cxn_Test_Base 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 Jetpack_Cxn_Test_Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Jetpack_Cxn_Test_Base { |
||
17 | |||
18 | /** |
||
19 | * Tests to run on the Jetpack connection. |
||
20 | * |
||
21 | * @var array $tests |
||
22 | */ |
||
23 | protected $tests = array(); |
||
24 | |||
25 | /** |
||
26 | * Results of the Jetpack connection tests. |
||
27 | * |
||
28 | * @var array $results |
||
29 | */ |
||
30 | protected $results = array(); |
||
31 | |||
32 | /** |
||
33 | * Status of the testing suite. |
||
34 | * |
||
35 | * Used internally to determine if a test should be skipped since the tests are already failing. Assume passing. |
||
36 | * |
||
37 | * @var bool $pass |
||
38 | */ |
||
39 | protected $pass = true; |
||
40 | |||
41 | /** |
||
42 | * Jetpack_Cxn_Test constructor. |
||
43 | */ |
||
44 | public function __construct() { |
||
48 | |||
49 | /** |
||
50 | * Adds a new test to the Jetpack Connection Testing suite. |
||
51 | * |
||
52 | * @param callable $callable Test to add to queue. |
||
53 | * @param array $groups Testing groups to add test to. |
||
54 | * |
||
55 | * @return bool True if successfully added. False for a failure. |
||
56 | */ |
||
57 | public function add_test( $callable, $groups = array( 'default' ) ) { |
||
68 | |||
69 | /** |
||
70 | * Runs the Jetpack connection suite. |
||
71 | */ |
||
72 | public function run_tests() { |
||
82 | |||
83 | /** |
||
84 | * Returns the full results array. |
||
85 | * |
||
86 | * @param string $group Testing group whose results we want. Defaults to "default" group. Use "all" for all tests. |
||
87 | * @return array Array of test results. |
||
88 | */ |
||
89 | public function raw_results( $group = 'default' ) { |
||
108 | |||
109 | /** |
||
110 | * Returns the status of the connection suite. |
||
111 | * |
||
112 | * @param string $group Testing group to check status of. Optional, default all tests. |
||
113 | * |
||
114 | * @return true|array True if all tests pass. Array of failed tests. |
||
115 | */ |
||
116 | public function pass( $group = 'default' ) { |
||
129 | |||
130 | /** |
||
131 | * Return array of failed test messages. |
||
132 | * |
||
133 | * @param string $group Testing group whose failures we want. Defaults to "default". Use "all" for all tests. |
||
134 | * |
||
135 | * @return false|array False if no failed tests. Otherwise, array of failed tests. |
||
136 | */ |
||
137 | public function list_fails( $group = 'default' ) { |
||
149 | |||
150 | /** |
||
151 | * Helper function to return consistent responses for a passing test. |
||
152 | * |
||
153 | * @param string $name Test name. |
||
154 | * |
||
155 | * @return array Test results. |
||
156 | */ |
||
157 | public static function passing_test( $name = 'Unnamed' ) { |
||
165 | |||
166 | /** |
||
167 | * Helper function to return consistent responses for a skipped test. |
||
168 | * |
||
169 | * @param string $name Test name. |
||
170 | * @param string $message Reason for skipping the test. Optional. |
||
171 | * |
||
172 | * @return array Test results. |
||
173 | */ |
||
174 | public static function skipped_test( $name = 'Unnamed', $message = false ) { |
||
182 | |||
183 | /** |
||
184 | * Helper function to return consistent responses for a failing test. |
||
185 | * |
||
186 | * @param string $name Test name. |
||
187 | * @param string $message Message detailing the failure. |
||
188 | * @param string $resolution Steps to resolve. |
||
189 | * |
||
190 | * @return array Test results. |
||
191 | */ |
||
192 | public static function failing_test( $name, $message, $resolution = false ) { |
||
213 | |||
214 | /** |
||
215 | * Provide WP_CLI friendly testing results. |
||
216 | * |
||
217 | * @param string $group Testing group whose results we are outputting. Default "default". Use "all" for all tests. |
||
218 | */ |
||
219 | public function output_results_for_cli( $group = 'default' ) { |
||
241 | |||
242 | /** |
||
243 | * Provide single WP Error instance of all failures. |
||
244 | * |
||
245 | * @param string $group Testing group whose failures we want converted. Default "default". Use "all" for all tests. |
||
246 | * |
||
247 | * @return WP_Error|false WP_Error with all failed tests or false if there were no failures. |
||
248 | */ |
||
249 | public function output_fails_as_wp_error( $group = 'default' ) { |
||
271 | |||
272 | /** |
||
273 | * Encrypt data for sending to WordPress.com. |
||
274 | * |
||
275 | * @todo When PHP minimum is 5.3+, add cipher detection to use an agreed better cipher than RC4. RC4 should be the last resort. |
||
276 | * |
||
277 | * @param string $data Data to encrypt with the WP.com Public Key. |
||
278 | * |
||
279 | * @return false|array False if functionality not available. Array of encrypted data, encryption key. |
||
280 | */ |
||
281 | public function encrypt_string_for_wpcom( $data ) { |
||
302 | } |
||
303 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.