Completed
Push — renovate/webpack-cli-3.x ( a49c46...d89017 )
by
unknown
44:08 queued 37:51
created

debug-functions.php ➔ jetpack_debugger_site_status_tests()   C

Complexity

Conditions 10
Paths 2

Size

Total Lines 76

Duplication

Lines 9
Ratio 11.84 %

Importance

Changes 0
Metric Value
cc 10
nc 2
nop 1
dl 9
loc 76
rs 6.6569
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * WP Site Health functionality temporarily stored in this file until all of Jetpack is PHP 5.3+
4
 *
5
 * @package Jetpack.
6
 */
7
8
/**
9
 * Test runner for Core's Site Health module.
10
 *
11
 * @since 7.3.0
12
 */
13
function jetpack_debugger_ajax_local_testing_suite() {
14
	check_ajax_referer( 'health-check-site-status' );
15
	if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
16
		wp_send_json_error();
17
	}
18
	$tests = new Jetpack_Cxn_Tests();
19
	wp_send_json_success( $tests->output_results_for_core_async_site_health() );
20
}
21
/**
22
 * Adds the Jetpack Local Testing Suite to the Core Site Health system.
23
 *
24
 * @since 7.3.0
25
 *
26
 * @param array $core_tests Array of tests from Core's Site Health.
27
 *
28
 * @return array $core_tests Array of tests for Core's Site Health.
29
 */
30
function jetpack_debugger_site_status_tests( $core_tests ) {
31
	$cxn_tests = new Jetpack_Cxn_Tests();
32
	$tests     = $cxn_tests->list_tests( 'direct' );
33
	foreach ( $tests as $test ) {
34
35
		$core_tests['direct'][ $test['name'] ] = array(
36
			'label' => __( 'Jetpack: ', 'jetpack' ) . $test['name'],
37
			'test'  => function() use ( $test, $cxn_tests ) { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.Found
38
				$results = $cxn_tests->run_test( $test['name'] );
39
				if ( is_wp_error( $results ) ) {
40
					return;
41
				}
42
43
				if ( isset( $results['show_in_site_health'] ) && false === $results['show_in_site_health'] ) {
44
					return;
45
				}
46
47
				$label = $results['label'] ?
48
					$results['label'] :
49
					ucwords(
50
						str_replace(
51
							'_',
52
							' ',
53
							str_replace( 'test__', '', $test['name'] )
54
						)
55
					);
56
				if ( $results['long_description'] ) {
57
					$description = $results['long_description'];
58
				} elseif ( $results['short_description'] ) {
59
					$description = sprintf(
60
						'<p>%s</p>',
61
						$results['short_description']
62
					);
63
				} else {
64
					$description = sprintf(
65
						'<p>%s</p>',
66
						__( 'This test successfully passed!', 'jetpack' )
67
					);
68
				}
69
70
				$return = array(
71
					'label'       => $label,
72
					'status'      => 'good',
73
					'badge'       => array(
74
						'label' => __( 'Jetpack', 'jetpack' ),
75
						'color' => 'green',
76
					),
77
					'description' => $description,
78
					'actions'     => '',
79
					'test'        => 'jetpack_' . $test['name'],
80
				);
81
82
				if ( false === $results['pass'] ) {
83
					$return['status'] = $results['severity'];
84 View Code Duplication
					if ( ! empty( $results['action'] ) ) {
85
						$return['actions'] = sprintf(
86
							'<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
87
							esc_url( $results['action'] ),
88
							$results['action_label'],
89
							/* translators: accessibility text */
90
							__( '(opens in a new tab)', 'jetpack' )
91
						);
92
					}
93
				}
94
95
				return $return;
96
			},
97
		);
98
	}
99
	$core_tests['async']['jetpack_test_suite'] = array(
100
		'label' => __( 'Jetpack Tests', 'jetpack' ),
101
		'test'  => 'jetpack_local_testing_suite',
102
	);
103
104
	return $core_tests;
105
}
106
107