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:
1 | <?php |
||
5 | class PodsAPI_CLI_Command extends WP_CLI_Command { |
||
6 | |||
7 | /** |
||
8 | * |
||
9 | * |
||
10 | * @synopsis --name=<name> --type=<type> --<field>=<value> |
||
11 | * @subcommand add-pod |
||
12 | */ |
||
13 | View Code Duplication | function add_pod( $args, $assoc_args ) { |
|
14 | |||
15 | if ( isset( $assoc_args['id'] ) ) { |
||
16 | unset( $assoc_args['id'] ); |
||
17 | } |
||
18 | |||
19 | $id = pods_api()->save_pod( $assoc_args ); |
||
20 | |||
21 | if ( 0 < $id ) { |
||
22 | WP_CLI::success( __( 'Pod added', 'pods' ) ); |
||
23 | WP_CLI::line( "ID: {$id}" ); |
||
24 | } else { |
||
25 | WP_CLI::error( __( 'Error adding pod', 'pods' ) ); |
||
26 | } |
||
27 | |||
28 | } |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * |
||
33 | * @synopsis --<field>=<value> |
||
34 | * @subcommand save-pod |
||
35 | */ |
||
36 | function save_pod( $args, $assoc_args ) { |
||
37 | |||
38 | $id = pods_api()->save_pod( $assoc_args ); |
||
39 | |||
40 | if ( 0 < $id ) { |
||
41 | WP_CLI::success( __( 'Pod saved', 'pods' ) ); |
||
42 | WP_CLI::line( "ID: {$id}" ); |
||
43 | } else { |
||
44 | WP_CLI::error( __( 'Error saving pod', 'pods' ) ); |
||
45 | } |
||
46 | |||
47 | } |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | * |
||
52 | * @synopsis --<field>=<value> |
||
53 | * @subcommand duplicate-pod |
||
54 | */ |
||
55 | function duplicate_pod( $args, $assoc_args ) { |
||
56 | |||
57 | $id = pods_api()->duplicate_pod( $assoc_args ); |
||
58 | |||
59 | if ( 0 < $id ) { |
||
60 | WP_CLI::success( __( 'Pod duplicated', 'pods' ) ); |
||
61 | WP_CLI::line( "New ID: {$id}" ); |
||
62 | } else { |
||
63 | WP_CLI::error( __( 'Error duplicating pod', 'pods' ) ); |
||
64 | } |
||
65 | |||
66 | } |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | * |
||
71 | * @synopsis --<field>=<value> |
||
72 | * @subcommand reset-pod |
||
73 | */ |
||
74 | function reset_pod( $args, $assoc_args ) { |
||
75 | |||
76 | $reset = pods_api()->reset_pod( $assoc_args ); |
||
77 | |||
78 | if ( $reset ) { |
||
79 | WP_CLI::success( __( 'Pod content reset', 'pods' ) ); |
||
80 | } else { |
||
81 | WP_CLI::error( __( 'Error resetting pod', 'pods' ) ); |
||
82 | } |
||
83 | |||
84 | } |
||
85 | |||
86 | /** |
||
87 | * |
||
88 | * |
||
89 | * @synopsis --<field>=<value> |
||
90 | * @subcommand delete-pod |
||
91 | */ |
||
92 | function delete_pod( $args, $assoc_args ) { |
||
93 | |||
94 | $deleted = pods_api()->delete_pod( $assoc_args ); |
||
95 | |||
96 | if ( $deleted ) { |
||
97 | WP_CLI::success( __( 'Pod deleted', 'pods' ) ); |
||
98 | } else { |
||
99 | WP_CLI::error( __( 'Error deleting pod', 'pods' ) ); |
||
100 | } |
||
101 | |||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Activate a component |
||
106 | * |
||
107 | * @synopsis --component=<component> |
||
108 | * @subcommand activate-component |
||
109 | */ |
||
110 | View Code Duplication | function activate_component( $args, $assoc_args ) { |
|
111 | |||
112 | if ( ! class_exists( 'PodsInit' ) ) { |
||
113 | WP_CLI::error( __( 'PodsInit not available', 'pods' ) ); |
||
114 | |||
115 | return; |
||
116 | } |
||
117 | |||
118 | $component = $assoc_args['component']; |
||
119 | |||
120 | $active = PodsInit::$components->is_component_active( $component ); |
||
121 | |||
122 | if ( $active ) { |
||
123 | WP_CLI::error( sprintf( __( 'Component %s is already active', 'pods' ), $component ) ); |
||
124 | } else { |
||
125 | PodsInit::$components->activate_component( $component ); |
||
126 | |||
127 | WP_CLI::success( __( 'Component activated', 'pods' ) ); |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Deactivate a component |
||
134 | * |
||
135 | * @synopsis --component=<component> |
||
136 | * @subcommand deactivate-component |
||
137 | */ |
||
138 | View Code Duplication | function deactivate_component( $args, $assoc_args ) { |
|
139 | |||
140 | if ( ! class_exists( 'PodsInit' ) ) { |
||
141 | WP_CLI::error( __( 'PodsInit not available', 'pods' ) ); |
||
142 | |||
143 | return; |
||
144 | } |
||
145 | |||
146 | $component = $assoc_args['component']; |
||
147 | |||
148 | $active = PodsInit::$components->is_component_active( $component ); |
||
149 | |||
150 | if ( ! $active ) { |
||
151 | WP_CLI::error( sprintf( __( 'Component %s is already not active', 'pods' ), $component ) ); |
||
152 | } else { |
||
153 | PodsInit::$components->deactivate_component( $component ); |
||
154 | |||
155 | WP_CLI::success( __( 'Component deactivated', 'pods' ) ); |
||
156 | } |
||
157 | |||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Clear pods cache |
||
162 | * |
||
163 | * @subcommand clear-cache |
||
164 | */ |
||
165 | function clear_cache() { |
||
171 | |||
172 | /** |
||
173 | * |
||
174 | * |
||
175 | * @synopsis --pod=<pod> --file=<file> |
||
176 | * @subcommand export-pod |
||
177 | */ |
||
178 | /*function export_pod ( $args, $assoc_args ) { |
||
179 | $data = pods_api()->load_pod( array( 'name' => $assoc_args[ 'pod' ] ) ); |
||
180 | |||
181 | if ( !empty( $data ) ) { |
||
182 | $data = json_encode( $data ); |
||
183 | |||
184 | // @todo write to file |
||
185 | } |
||
186 | |||
187 | // @todo success message |
||
188 | }*/ |
||
189 | |||
190 | /** |
||
191 | * |
||
192 | * |
||
193 | * @synopsis --file=<file> |
||
194 | * @subcommand import-pod |
||
195 | */ |
||
196 | /*function import_pod ( $args, $assoc_args ) { |
||
197 | $data = ''; // @todo get data from file |
||
198 | |||
199 | $package = array(); |
||
200 | |||
201 | if ( !empty( $data ) ) |
||
202 | $package = @json_decode( $data, true ); |
||
203 | |||
204 | if ( is_array( $package ) && !empty( $package ) ) { |
||
205 | $api = pods_api(); |
||
206 | |||
207 | if ( isset( $package[ 'id' ] ) ) |
||
208 | unset( $package[ 'id' ] ); |
||
209 | |||
210 | $try = 1; |
||
211 | $check_name = $package[ 'name' ]; |
||
212 | |||
213 | while ( $api->load_pod( array( 'name' => $check_name, 'table_info' => false ), false ) ) { |
||
214 | $try++; |
||
215 | $check_name = $package[ 'name' ] . $try; |
||
216 | } |
||
217 | |||
218 | $package[ 'name' ] = $check_name; |
||
219 | |||
220 | $id = $api->save_pod( $package ); |
||
221 | |||
222 | if ( 0 < $id ) { |
||
223 | WP_CLI::success( __( 'Pod imported', 'pods' ) ); |
||
224 | WP_CLI::line( "ID: {$id}" ); |
||
225 | } |
||
226 | else |
||
227 | WP_CLI::error( __( 'Error importing pod', 'pods' ) ); |
||
228 | } |
||
229 | else |
||
230 | WP_CLI::error( __( 'Invalid package, Pod not imported', 'pods' ) ); |
||
231 | }*/ |
||
232 | |||
233 | } |
||
234 | |||
236 |
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.