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 |
||
18 | class Jetpack_Cxn_Test_Base { |
||
19 | |||
20 | /** |
||
21 | * Tests to run on the Jetpack connection. |
||
22 | * |
||
23 | * @var array $tests |
||
24 | */ |
||
25 | protected $tests = array(); |
||
26 | |||
27 | /** |
||
28 | * Results of the Jetpack connection tests. |
||
29 | * |
||
30 | * @var array $results |
||
31 | */ |
||
32 | protected $results = array(); |
||
33 | |||
34 | /** |
||
35 | * Status of the testing suite. |
||
36 | * |
||
37 | * Used internally to determine if a test should be skipped since the tests are already failing. Assume passing. |
||
38 | * |
||
39 | * @var bool $pass |
||
40 | */ |
||
41 | protected $pass = true; |
||
42 | |||
43 | /** |
||
44 | * Jetpack_Cxn_Test constructor. |
||
45 | */ |
||
46 | public function __construct() { |
||
50 | |||
51 | /** |
||
52 | * Adds a new test to the Jetpack Connection Testing suite. |
||
53 | * |
||
54 | * @since 7.1.0 |
||
55 | * @since 7.3.0 Adds name parameter and returns WP_Error on failure. |
||
56 | * |
||
57 | * @param callable $callable Test to add to queue. |
||
58 | * @param string $name Unique name for the test. |
||
59 | * @param string $type Optional. Core Site Health type: 'direct' if test can be run during initial load or 'async' if test should run async. |
||
60 | * @param array $groups Optional. Testing groups to add test to. |
||
61 | * |
||
62 | * @return mixed True if successfully added. WP_Error on failure. |
||
63 | */ |
||
64 | public function add_test( $callable, $name, $type = 'direct', $groups = array( 'default' ) ) { |
||
83 | |||
84 | /** |
||
85 | * Lists all tests to run. |
||
86 | * |
||
87 | * @since 7.3.0 |
||
88 | * |
||
89 | * @param string $type Optional. Core Site Health type: 'direct' or 'async'. All by default. |
||
90 | * @param string $group Optional. A specific testing group. All by default. |
||
91 | * |
||
92 | * @return array $tests Array of tests with test information. |
||
93 | */ |
||
94 | public function list_tests( $type = 'all', $group = 'all' ) { |
||
114 | |||
115 | /** |
||
116 | * Run a specific test. |
||
117 | * |
||
118 | * @since 7.3.0 |
||
119 | * |
||
120 | * @param string $name Name of test. |
||
121 | * |
||
122 | * @return mixed $result Test result array or WP_Error if invalid name. { |
||
123 | * @type string $name Test name |
||
124 | * @type mixed $pass True if passed, false if failed, 'skipped' if skipped. |
||
125 | * @type string $message Human-readable test result message. |
||
126 | * @type string $resolution Human-readable resolution steps. |
||
127 | * } |
||
128 | */ |
||
129 | public function run_test( $name ) { |
||
136 | |||
137 | /** |
||
138 | * Runs the Jetpack connection suite. |
||
139 | */ |
||
140 | public function run_tests() { |
||
150 | |||
151 | /** |
||
152 | * Returns the full results array. |
||
153 | * |
||
154 | * @param string $group Testing group whose results we want. Defaults to "default" group. Use "all" for all tests. |
||
155 | * @return array Array of test results. |
||
156 | */ |
||
157 | public function raw_results( $group = 'default' ) { |
||
176 | |||
177 | /** |
||
178 | * Returns the status of the connection suite. |
||
179 | * |
||
180 | * @param string $group Testing group to check status of. Optional, default all tests. |
||
181 | * |
||
182 | * @return true|array True if all tests pass. Array of failed tests. |
||
183 | */ |
||
184 | public function pass( $group = 'default' ) { |
||
197 | |||
198 | /** |
||
199 | * Return array of failed test messages. |
||
200 | * |
||
201 | * @param string $group Testing group whose failures we want. Defaults to "default". Use "all" for all tests. |
||
202 | * |
||
203 | * @return false|array False if no failed tests. Otherwise, array of failed tests. |
||
204 | */ |
||
205 | public function list_fails( $group = 'default' ) { |
||
217 | |||
218 | /** |
||
219 | * Helper function to return consistent responses for a passing test. |
||
220 | * |
||
221 | * @param string $name Test name. |
||
222 | * |
||
223 | * @return array Test results. |
||
224 | */ |
||
225 | public static function passing_test( $name = 'Unnamed' ) { |
||
233 | |||
234 | /** |
||
235 | * Helper function to return consistent responses for a skipped test. |
||
236 | * |
||
237 | * @param string $name Test name. |
||
238 | * @param string $message Reason for skipping the test. Optional. |
||
239 | * |
||
240 | * @return array Test results. |
||
241 | */ |
||
242 | public static function skipped_test( $name = 'Unnamed', $message = false ) { |
||
250 | |||
251 | /** |
||
252 | * Helper function to return consistent responses for a failing test. |
||
253 | * |
||
254 | * @since 7.1.0 |
||
255 | * @since 7.3.0 Added $action for resolution action link, $severity for issue severity. |
||
256 | * |
||
257 | * @param string $name Test name. |
||
258 | * @param string $message Message detailing the failure. |
||
259 | * @param string $resolution Optional. Steps to resolve. |
||
260 | * @param string $action Optional. URL to direct users to self-resolve. |
||
261 | * @param string $severity Optional. "critical" or "recommended" for failure stats. "good" for passing. |
||
262 | * |
||
263 | * @return array Test results. |
||
264 | */ |
||
265 | public static function failing_test( $name, $message, $resolution = false, $action = false, $severity = 'critical' ) { |
||
289 | |||
290 | /** |
||
291 | * Provide WP_CLI friendly testing results. |
||
292 | * |
||
293 | * @param string $group Testing group whose results we are outputting. Default "default". Use "all" for all tests. |
||
294 | */ |
||
295 | public function output_results_for_cli( $group = 'default' ) { |
||
317 | |||
318 | /** |
||
319 | * Output results of failures in format expected by Core's Site Health tool. |
||
320 | * |
||
321 | * Specifically not asking for a testing group since we're opinionated that Site Heath should see all. |
||
322 | * |
||
323 | * @return array Array of test results |
||
324 | */ |
||
325 | public function output_results_for_core_site_health() { |
||
379 | |||
380 | /** |
||
381 | * Provide single WP Error instance of all failures. |
||
382 | * |
||
383 | * @param string $group Testing group whose failures we want converted. Default "default". Use "all" for all tests. |
||
384 | * |
||
385 | * @return WP_Error|false WP_Error with all failed tests or false if there were no failures. |
||
386 | */ |
||
387 | public function output_fails_as_wp_error( $group = 'default' ) { |
||
409 | |||
410 | /** |
||
411 | * Encrypt data for sending to WordPress.com. |
||
412 | * |
||
413 | * @todo When PHP minimum is 5.3+, add cipher detection to use an agreed better cipher than RC4. RC4 should be the last resort. |
||
414 | * |
||
415 | * @param string $data Data to encrypt with the WP.com Public Key. |
||
416 | * |
||
417 | * @return false|array False if functionality not available. Array of encrypted data, encryption key. |
||
418 | */ |
||
419 | public function encrypt_string_for_wpcom( $data ) { |
||
440 | } |
||
441 |
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.