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 |
||
7 | class PodsField_Link extends PodsField_Website { |
||
8 | |||
9 | /** |
||
10 | * Field Type Group |
||
11 | * |
||
12 | * @var string |
||
13 | * @since 2.0 |
||
14 | */ |
||
15 | public static $group = 'Text'; |
||
16 | |||
17 | /** |
||
18 | * Field Type Identifier |
||
19 | * |
||
20 | * @var string |
||
21 | * @since 2.0 |
||
22 | */ |
||
23 | public static $type = 'link'; |
||
24 | |||
25 | /** |
||
26 | * Field Type Label |
||
27 | * |
||
28 | * @var string |
||
29 | * @since 2.0 |
||
30 | */ |
||
31 | public static $label = 'Link'; |
||
32 | |||
33 | /** |
||
34 | * Field Type Preparation |
||
35 | * |
||
36 | * @var string |
||
37 | * @since 2.0 |
||
38 | */ |
||
39 | public static $prepare = '%s'; |
||
40 | |||
41 | /** |
||
42 | * Do things like register/enqueue scripts and stylesheets |
||
43 | * |
||
44 | * @since 2.0 |
||
45 | */ |
||
46 | public function __construct() { |
||
47 | self::$label = __( 'Link', 'pods' ); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Add options and set defaults to |
||
52 | * |
||
53 | * @param array $options |
||
54 | * |
||
55 | * @since 2.0 |
||
56 | */ |
||
57 | public function options() { |
||
129 | |||
130 | /** |
||
131 | * Define the current field's schema for DB table storage |
||
132 | * |
||
133 | * @param array $options |
||
134 | * |
||
135 | * @return array |
||
136 | * @since 2.0 |
||
137 | */ |
||
138 | public function schema( $options = null ) { |
||
145 | |||
146 | /** |
||
147 | * Change the value of the field |
||
148 | * |
||
149 | * @param mixed $value |
||
150 | * @param string $name |
||
151 | * @param array $options |
||
152 | * @param array $pod |
||
153 | * @param int $id |
||
154 | * |
||
155 | * @return mixed|null|string |
||
156 | * @since 2.3 |
||
157 | */ |
||
158 | public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
163 | |||
164 | /** |
||
165 | * Change the way the value of the field is displayed with Pods::get |
||
166 | * |
||
167 | * @param mixed $value |
||
168 | * @param string $name |
||
169 | * @param array $options |
||
170 | * @param array $pod |
||
171 | * @param int $id |
||
172 | * |
||
173 | * @return mixed|null|string |
||
174 | * @since 2.0 |
||
175 | */ |
||
176 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
228 | |||
229 | /** |
||
230 | * Customize output of the form field |
||
231 | * |
||
232 | * @param string $name |
||
233 | * @param mixed $value |
||
234 | * @param array $options |
||
235 | * @param array $pod |
||
236 | * @param int $id |
||
237 | * |
||
238 | * @since 2.0 |
||
239 | */ |
||
240 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
248 | |||
249 | /** |
||
250 | * Validate a value before it's saved |
||
251 | * |
||
252 | * @param mixed $value |
||
253 | * @param string $name |
||
254 | * @param array $options |
||
255 | * @param array $fields |
||
256 | * @param array $pod |
||
257 | * @param int $id |
||
258 | * |
||
259 | * @since 2.0 |
||
260 | */ |
||
261 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
290 | |||
291 | /** |
||
292 | * Change the value or perform actions after validation but before saving to the DB |
||
293 | * |
||
294 | * @param mixed $value |
||
295 | * @param int $id |
||
296 | * @param string $name |
||
297 | * @param array $options |
||
298 | * @param array $fields |
||
299 | * @param array $pod |
||
300 | * @param object $params |
||
301 | * |
||
302 | * @since 2.0 |
||
303 | */ |
||
304 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
328 | |||
329 | /** |
||
330 | * Init the editor needed for WP Link modal to work |
||
331 | */ |
||
332 | public function validate_link_modal() { |
||
345 | |||
346 | /** |
||
347 | * Echo the link modal code |
||
348 | */ |
||
349 | public function add_link_modal() { |
||
364 | } |
||
365 |
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.