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:
Complex classes like Sensei_Language_Pack_Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sensei_Language_Pack_Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Sensei_Language_Pack_Manager { |
||
19 | |||
20 | /** |
||
21 | * Languages repository |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected static $repo = 'https://github.com/woothemes/sensei-language-packs/raw/'; |
||
26 | |||
27 | /** |
||
28 | * Initialize the language pack manager |
||
29 | */ |
||
30 | public function __construct() { |
||
34 | |||
35 | /** |
||
36 | * Get translation package URI. |
||
37 | * |
||
38 | * @param string $locale |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | public static function get_package_uri( $locale ) { |
||
45 | |||
46 | /** |
||
47 | * Get settings URI. |
||
48 | * |
||
49 | * @param string $action |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | protected static function get_settings_uri( $action ) { |
||
56 | |||
57 | /** |
||
58 | * Get the install language package URI. |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public static function get_install_uri() { |
||
65 | |||
66 | /** |
||
67 | * Get the dismiss language package message URI. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public static function get_dismiss_uri() { |
||
74 | |||
75 | /** |
||
76 | * Triggered when WPLANG is changed |
||
77 | * |
||
78 | * @param string $old |
||
79 | * @param string $new |
||
80 | */ |
||
81 | public function updated_language_option( $old, $new ) { |
||
84 | |||
85 | /** |
||
86 | * Check if has available language pack install |
||
87 | * |
||
88 | * @param string $locale |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | public static function has_language_pack_available( $locale = null ) { |
||
93 | |||
94 | if ( is_null( $locale ) ) { |
||
95 | |||
96 | $locale = get_locale(); |
||
97 | |||
98 | } |
||
99 | |||
100 | if ( 'en_US' === $locale ) { |
||
101 | |||
102 | return false; |
||
103 | |||
104 | } |
||
105 | |||
106 | if ( 'yes' === get_option( 'sensei_needs_language_pack_install' ) ) { |
||
107 | |||
108 | return true; |
||
109 | |||
110 | } |
||
111 | |||
112 | if( isset( $_GET['translation_updated'] ) && 5 == $_GET['translation_updated'] ){ |
||
113 | |||
114 | return false; |
||
115 | |||
116 | } |
||
117 | |||
118 | $version = get_option( 'woothemes_sensei_language_pack_version', array( '0', $locale ) ); |
||
119 | |||
120 | if ( ! is_array( $version ) || version_compare( $version[0], Sensei()->version, '<' ) || $version[1] !== $locale ) { |
||
121 | if ( self::check_if_language_pack_exists( $locale ) ) { |
||
122 | update_option( 'sensei_needs_language_pack_install', 'yes' ); |
||
123 | |||
124 | return true; |
||
125 | } else { |
||
126 | // Updated the woothemes_sensei_language_pack_version to avoid searching translations for this release again |
||
127 | self::update_language_pack_version( $locale ); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | return false; |
||
132 | |||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Check if language pack exists |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public static function check_if_language_pack_exists( $locale ) { |
||
149 | |||
150 | /** |
||
151 | * Update language pack version. |
||
152 | * |
||
153 | * @param string $locale |
||
154 | */ |
||
155 | public static function update_language_pack_version( $locale ) { |
||
162 | |||
163 | /** |
||
164 | * Manual language update. |
||
165 | */ |
||
166 | public function language_package_actions() { |
||
184 | |||
185 | /** |
||
186 | * Install language pack. |
||
187 | */ |
||
188 | protected function language_pack_install() { |
||
252 | |||
253 | /** |
||
254 | * Hide language pack notice. |
||
255 | */ |
||
256 | protected function dismiss_language_pack_notice() { |
||
264 | |||
265 | /** |
||
266 | * Language pack messages |
||
267 | */ |
||
268 | public static function messages() { |
||
292 | } |
||
293 | |||
295 |
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.