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 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' ) ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Lists all tests to run. |
||
| 87 | * |
||
| 88 | * @since 7.3.0 |
||
| 89 | * |
||
| 90 | * @param string $type Optional. Core Site Health type: 'direct' or 'async'. All by default. |
||
| 91 | * @param string $group Optional. A specific testing group. All by default. |
||
| 92 | * |
||
| 93 | * @return array $tests Array of tests with test information. |
||
| 94 | */ |
||
| 95 | public function list_tests( $type = 'all', $group = 'all' ) { |
||
| 96 | if ( ! ( 'all' === $type || 'direct' === $type || 'async' === $type ) ) { |
||
| 97 | _doing_it_wrong( 'Jetpack_Cxn_Test_Base->list_tests', 'Type must be all, direct, or async', '7.3.0' ); |
||
| 98 | } |
||
| 99 | |||
| 100 | $tests = array(); |
||
| 101 | foreach ( $this->tests as $name => $value ) { |
||
| 102 | // Get all valid tests by group staged. |
||
| 103 | if ( 'all' === $group || $group === $value['group'] ) { |
||
| 104 | $tests[ $name ] = $value; |
||
| 105 | } |
||
| 106 | |||
| 107 | // Next filter out any that do not match the type. |
||
| 108 | if ( 'all' !== $type && $type !== $value['type'] ) { |
||
| 109 | unset( $tests[ $name ] ); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $tests; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Run a specific test. |
||
| 118 | * |
||
| 119 | * @since 7.3.0 |
||
| 120 | * |
||
| 121 | * @param string $name Name of test. |
||
| 122 | * |
||
| 123 | * @return mixed $result Test result array or WP_Error if invalid name. { |
||
| 124 | * @type string $name Test name |
||
| 125 | * @type mixed $pass True if passed, false if failed, 'skipped' if skipped. |
||
| 126 | * @type string $message Human-readable test result message. |
||
| 127 | * @type string $resolution Human-readable resolution steps. |
||
| 128 | * } |
||
| 129 | */ |
||
| 130 | public function run_test( $name ) { |
||
| 131 | if ( array_key_exists( $name, $this->tests ) ) { |
||
| 132 | return call_user_func( $this->tests[ $name ]['test'] ); |
||
| 133 | } |
||
| 134 | return new WP_Error( __( 'There is no test by that name: ', 'jetpack' ) . $name ); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Runs the Jetpack connection suite. |
||
| 139 | */ |
||
| 140 | public function run_tests() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns the full results array. |
||
| 154 | * |
||
| 155 | * @since 7.1.0 |
||
| 156 | * @since 7.3.0 Add 'type' |
||
| 157 | * |
||
| 158 | * @param string $type Test type, async or direct. |
||
| 159 | * @param string $group Testing group whose results we want. Defaults to all tests. |
||
| 160 | * @return array Array of test results. |
||
| 161 | */ |
||
| 162 | public function raw_results( $type = 'all', $group = 'all' ) { |
||
| 163 | if ( ! $this->results ) { |
||
|
|
|||
| 164 | $this->run_tests(); |
||
| 165 | } |
||
| 166 | |||
| 167 | $results = $this->results; |
||
| 168 | |||
| 169 | if ( 'all' !== $group ) { |
||
| 170 | foreach ( $results as $test => $result ) { |
||
| 171 | if ( ! in_array( $group, $result['group'], true ) ) { |
||
| 172 | unset( $results[ $test ] ); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if ( 'all' !== $type ) { |
||
| 178 | foreach ( $results as $test => $result ) { |
||
| 179 | if ( $type !== $result['type'] ) { |
||
| 180 | unset( $results[ $test ] ); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | return $results; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns the status of the connection suite. |
||
| 190 | * |
||
| 191 | * @since 7.1.0 |
||
| 192 | * @since 7.3.0 Add 'type' |
||
| 193 | * |
||
| 194 | * @param string $type Test type, async or direct. Optional, direct all tests. |
||
| 195 | * @param string $group Testing group to check status of. Optional, default all tests. |
||
| 196 | * |
||
| 197 | * @return true|array True if all tests pass. Array of failed tests. |
||
| 198 | */ |
||
| 199 | public function pass( $type = 'all', $group = 'all' ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Return array of failed test messages. |
||
| 215 | * |
||
| 216 | * @since 7.1.0 |
||
| 217 | * @since 7.3.0 Add 'type' |
||
| 218 | * |
||
| 219 | * @param string $type Test type, direct or async. |
||
| 220 | * @param string $group Testing group whose failures we want. Defaults to "all". |
||
| 221 | * |
||
| 222 | * @return false|array False if no failed tests. Otherwise, array of failed tests. |
||
| 223 | */ |
||
| 224 | public function list_fails( $type = 'all', $group = 'all' ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Helper function to return consistent responses for a passing test. |
||
| 239 | * |
||
| 240 | * @param string $name Test name. |
||
| 241 | * |
||
| 242 | * @return array Test results. |
||
| 243 | */ |
||
| 244 | View Code Duplication | public static function passing_test( $name = 'Unnamed' ) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Helper function to return consistent responses for a skipped test. |
||
| 256 | * |
||
| 257 | * @param string $name Test name. |
||
| 258 | * @param string $message Reason for skipping the test. Optional. |
||
| 259 | * |
||
| 260 | * @return array Test results. |
||
| 261 | */ |
||
| 262 | View Code Duplication | public static function skipped_test( $name = 'Unnamed', $message = false ) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Helper function to return consistent responses for a failing test. |
||
| 274 | * |
||
| 275 | * @since 7.1.0 |
||
| 276 | * @since 7.3.0 Added $action for resolution action link, $severity for issue severity. |
||
| 277 | * |
||
| 278 | * @param string $name Test name. |
||
| 279 | * @param string $message Message detailing the failure. |
||
| 280 | * @param string $resolution Optional. Steps to resolve. |
||
| 281 | * @param string $action Optional. URL to direct users to self-resolve. |
||
| 282 | * @param string $severity Optional. "critical" or "recommended" for failure stats. "good" for passing. |
||
| 283 | * |
||
| 284 | * @return array Test results. |
||
| 285 | */ |
||
| 286 | public static function failing_test( $name, $message, $resolution = false, $action = false, $severity = 'critical' ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Provide WP_CLI friendly testing results. |
||
| 313 | * |
||
| 314 | * @since 7.1.0 |
||
| 315 | * @since 7.3.0 Add 'type' |
||
| 316 | * |
||
| 317 | * @param string $type Test type, direct or async. |
||
| 318 | * @param string $group Testing group whose results we are outputting. Default all tests. |
||
| 319 | */ |
||
| 320 | public function output_results_for_cli( $type = 'all', $group = 'all' ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Output results of failures in format expected by Core's Site Health tool for async tests. |
||
| 345 | * |
||
| 346 | * Specifically not asking for a testing group since we're opinionated that Site Heath should see all. |
||
| 347 | * |
||
| 348 | * @since 7.3.0 |
||
| 349 | * |
||
| 350 | * @return array Array of test results |
||
| 351 | */ |
||
| 352 | public function output_results_for_core_async_site_health() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Provide single WP Error instance of all failures. |
||
| 409 | * |
||
| 410 | * @since 7.1.0 |
||
| 411 | * @since 7.3.0 Add 'type' |
||
| 412 | * |
||
| 413 | * @param string $type Test type, direct or async. |
||
| 414 | * @param string $group Testing group whose failures we want converted. Default all tests. |
||
| 415 | * |
||
| 416 | * @return WP_Error|false WP_Error with all failed tests or false if there were no failures. |
||
| 417 | */ |
||
| 418 | public function output_fails_as_wp_error( $type = 'all', $group = 'all' ) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Encrypt data for sending to WordPress.com. |
||
| 443 | * |
||
| 444 | * @todo When PHP minimum is 5.3+, add cipher detection to use an agreed better cipher than RC4. RC4 should be the last resort. |
||
| 445 | * |
||
| 446 | * @param string $data Data to encrypt with the WP.com Public Key. |
||
| 447 | * |
||
| 448 | * @return false|array False if functionality not available. Array of encrypted data, encryption key. |
||
| 449 | */ |
||
| 450 | public function encrypt_string_for_wpcom( $data ) { |
||
| 471 | } |
||
| 472 |
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.