Conditions | 10 |
Paths | 2 |
Total Lines | 83 |
Lines | 9 |
Ratio | 10.84 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | $core_tests['direct'][ $test['name'] ] = array( |
||
35 | 'label' => __( 'Jetpack: ', 'jetpack' ) . $test['name'], |
||
36 | 'test' => function() use ( $test, $cxn_tests ) { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.Found |
||
37 | $results = $cxn_tests->run_test( $test['name'] ); |
||
38 | // Test names are, by default, `test__some_string_of_text`. Let's convert to "Some String Of Text" for humans. |
||
39 | $label = ucwords( |
||
40 | str_replace( |
||
41 | '_', |
||
42 | ' ', |
||
43 | str_replace( 'test__', '', $test['name'] ) |
||
44 | ) |
||
45 | ); |
||
46 | $return = array( |
||
47 | 'label' => $label, |
||
48 | 'status' => 'good', |
||
49 | 'badge' => array( |
||
50 | 'label' => __( 'Jetpack', 'jetpack' ), |
||
51 | 'color' => 'green', |
||
52 | ), |
||
53 | 'description' => sprintf( |
||
54 | '<p>%s</p>', |
||
55 | __( 'This test successfully passed!', 'jetpack' ) |
||
56 | ), |
||
57 | 'actions' => '', |
||
58 | 'test' => 'jetpack_' . $test['name'], |
||
59 | ); |
||
60 | if ( is_wp_error( $results ) ) { |
||
61 | return; |
||
62 | } |
||
63 | if ( false === $results['pass'] ) { |
||
64 | $return['label'] = $results['message']; |
||
65 | if ( $results['label'] ) { |
||
66 | // Allow tests to override the strange message => label logic with an actual label. |
||
67 | $return['label'] = $results['label']; |
||
68 | } |
||
69 | |||
70 | // Most tests pass a `resolution` property to use as a description. |
||
71 | $return['description'] = sprintf( |
||
72 | '<p>%s</p>', |
||
73 | $results['resolution'] |
||
74 | ); |
||
75 | |||
76 | if ( $results['description'] ) { |
||
77 | // Allow tests to override 'resolution' with their own HTML description. |
||
78 | $return['description'] = $results['description']; |
||
79 | } |
||
80 | |||
81 | $return['status'] = $results['severity']; |
||
82 | View Code Duplication | if ( ! empty( $results['action'] ) ) { |
|
83 | $return['actions'] = sprintf( |
||
84 | '<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>', |
||
85 | esc_url( $results['action'] ), |
||
86 | $results['action_label'], |
||
87 | /* translators: accessibility text */ |
||
88 | __( '(opens in a new tab)', 'jetpack' ) |
||
89 | ); |
||
90 | } |
||
91 | } elseif ( true === $results['pass'] ) { |
||
92 | // Passing tests can chose to override defaults. |
||
93 | if ( $results['label'] ) { |
||
94 | $return['label'] = $results['label']; |
||
95 | } |
||
96 | |||
97 | if ( $results['description'] ) { |
||
98 | $return['description'] = $results['description']; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | return $return; |
||
103 | }, |
||
104 | ); |
||
105 | } |
||
106 | $core_tests['async']['jetpack_test_suite'] = array( |
||
107 | 'label' => __( 'Jetpack Tests', 'jetpack' ), |
||
108 | 'test' => 'jetpack_local_testing_suite', |
||
109 | ); |
||
110 | |||
111 | return $core_tests; |
||
112 | } |
||
113 | |||
114 |