Conditions | 10 |
Paths | 14 |
Total Lines | 48 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
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 |
||
67 | protected function set_js_vars() { |
||
68 | |||
69 | // Typecast to array. |
||
70 | $this->js_vars = (array) $this->js_vars; |
||
71 | |||
72 | // Check if transport is set to auto. |
||
73 | // If not, then skip the auto-calculations and exit early. |
||
74 | if ( 'auto' !== $this->transport ) { |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | // Set transport to refresh initially. |
||
79 | // Serves as a fallback in case we failt to auto-calculate js_vars. |
||
80 | $this->transport = 'refresh'; |
||
81 | |||
82 | $js_vars = array(); |
||
83 | |||
84 | // Try to auto-generate js_vars. |
||
85 | // First we need to check if js_vars are empty, and that output is not empty. |
||
86 | if ( empty( $this->js_vars ) && ! empty( $this->output ) ) { |
||
87 | |||
88 | // Start going through each item in the $output array. |
||
89 | foreach ( $this->output as $output ) { |
||
90 | |||
91 | // If 'element' is not defined, skip this. |
||
92 | if ( ! isset( $output['element'] ) ) { |
||
93 | continue; |
||
94 | } |
||
95 | if ( is_array( $output['element'] ) ) { |
||
96 | $output['element'] = implode( ',', $output['element'] ); |
||
97 | } |
||
98 | |||
99 | // If there's a sanitize_callback defined, skip this. |
||
100 | if ( isset( $output['sanitize_callback'] ) && ! empty( $output['sanitize_callback'] ) ) { |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | // If we got this far, it's safe to add this. |
||
105 | $js_vars[] = $output; |
||
106 | } |
||
107 | |||
108 | // Did we manage to get all the items from 'output'? |
||
109 | // If not, then we're missing something so don't add this. |
||
110 | if ( count( $js_vars ) !== count( $this->output ) ) { |
||
111 | return; |
||
112 | } |
||
113 | $this->js_vars = $js_vars; |
||
114 | $this->transport = 'postMessage'; |
||
115 | } |
||
118 |