Conditions | 9 |
Paths | 32 |
Total Lines | 114 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
79 | function _migrate_field_group_500( $ofg ) { |
||
80 | |||
81 | // global |
||
82 | global $wpdb; |
||
83 | |||
84 | |||
85 | // get post status |
||
86 | $post_status = $ofg->post_status; |
||
87 | |||
88 | |||
89 | // create new field group |
||
90 | $nfg = array( |
||
91 | 'ID' => 0, |
||
92 | 'title' => $ofg->post_title, |
||
93 | 'menu_order' => $ofg->menu_order, |
||
94 | ); |
||
95 | |||
96 | |||
97 | // location rules |
||
98 | $groups = array(); |
||
99 | |||
100 | |||
101 | // get all rules |
||
102 | $rules = get_post_meta($ofg->ID, 'rule', false); |
||
103 | |||
104 | if( is_array($rules) ) { |
||
105 | |||
106 | $group_no = 0; |
||
107 | |||
108 | foreach( $rules as $rule ) { |
||
109 | |||
110 | // if field group was duplicated, it may now be a serialized string! |
||
111 | $rule = maybe_unserialize($rule); |
||
112 | |||
113 | |||
114 | // does this rule have a group? |
||
115 | // + groups were added in 4.0.4 |
||
116 | if( !isset($rule['group_no']) ) { |
||
117 | |||
118 | $rule['group_no'] = $group_no; |
||
119 | |||
120 | // sperate groups? |
||
121 | if( get_post_meta($ofg->ID, 'allorany', true) == 'any' ) { |
||
122 | |||
123 | $group_no++; |
||
124 | |||
125 | } |
||
126 | |||
127 | } |
||
128 | |||
129 | |||
130 | // extract vars |
||
131 | $group = acf_extract_var( $rule, 'group_no' ); |
||
132 | $order = acf_extract_var( $rule, 'order_no' ); |
||
133 | |||
134 | |||
135 | // add to group |
||
136 | $groups[ $group ][ $order ] = $rule; |
||
137 | |||
138 | |||
139 | // sort rules |
||
140 | ksort( $groups[ $group ] ); |
||
141 | |||
142 | } |
||
143 | |||
144 | // sort groups |
||
145 | ksort( $groups ); |
||
146 | } |
||
147 | |||
148 | $nfg['location'] = $groups; |
||
149 | |||
150 | |||
151 | // settings |
||
152 | if( $position = get_post_meta($ofg->ID, 'position', true) ) { |
||
153 | |||
154 | $nfg['position'] = $position; |
||
155 | |||
156 | } |
||
157 | |||
158 | if( $layout = get_post_meta($ofg->ID, 'layout', true) ) { |
||
159 | |||
160 | $nfg['layout'] = $layout; |
||
161 | |||
162 | } |
||
163 | |||
164 | if( $hide_on_screen = get_post_meta($ofg->ID, 'hide_on_screen', true) ) { |
||
165 | |||
166 | $nfg['hide_on_screen'] = maybe_unserialize($hide_on_screen); |
||
167 | |||
168 | } |
||
169 | |||
170 | |||
171 | // Note: acf_update_field_group will call the acf_get_valid_field_group function and apply 'compatibility' changes |
||
172 | |||
173 | |||
174 | // add old ID reference |
||
175 | $nfg['old_ID'] = $ofg->ID; |
||
176 | |||
177 | |||
178 | // save field group |
||
179 | $nfg = acf_update_field_group( $nfg ); |
||
180 | |||
181 | |||
182 | // trash? |
||
183 | if( $post_status == 'trash' ) { |
||
184 | |||
185 | acf_trash_field_group( $nfg['ID'] ); |
||
186 | |||
187 | } |
||
188 | |||
189 | |||
190 | // return |
||
191 | return $nfg; |
||
192 | } |
||
193 | |||
326 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.