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 |
||
17 | class WC_Shipping_Zones { |
||
18 | |||
19 | /** |
||
20 | * Get shipping zones from the database |
||
21 | * @since 2.6.0 |
||
22 | * @return array of arrays |
||
23 | */ |
||
24 | public static function get_zones() { |
||
39 | |||
40 | /** |
||
41 | * Get shipping zone using it's ID |
||
42 | * @since 2.6.0 |
||
43 | * @param int $zone_id |
||
44 | * @return WC_Shipping_Zone|bool |
||
45 | */ |
||
46 | public static function get_zone( $zone_id ) { |
||
49 | |||
50 | /** |
||
51 | * Get shipping zone by an ID. |
||
52 | * @since 2.6.0 |
||
53 | * @param string $by zone_id or instance_id |
||
54 | * @param int $id |
||
55 | * @return WC_Shipping_Zone|bool |
||
56 | */ |
||
57 | public static function get_zone_by( $by = 'zone_id', $id = 0 ) { |
||
78 | |||
79 | /** |
||
80 | * Get shipping zone using it's ID |
||
81 | * @since 2.6.0 |
||
82 | * @param int $zone_id |
||
83 | * @return WC_Shipping_Meethod|bool |
||
84 | */ |
||
85 | public static function get_shipping_method( $instance_id ) { |
||
97 | |||
98 | /** |
||
99 | * Delete a zone using it's ID |
||
100 | * @param int $zone_id |
||
101 | * @since 2.6.0 |
||
102 | */ |
||
103 | public static function delete_zone( $zone_id ) { |
||
108 | } |
||
109 |
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.