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 |
||
| 22 | class Wordlift_Configuration_Service { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The entity base path option name. |
||
| 26 | * |
||
| 27 | * @since 3.6.0 |
||
| 28 | */ |
||
| 29 | const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The skip wizard (admin installation wizard) option name. |
||
| 33 | * |
||
| 34 | * @since 3.9.0 |
||
| 35 | */ |
||
| 36 | const SKIP_WIZARD = 'wl_skip_wizard'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * WordLift's key option name. |
||
| 40 | * |
||
| 41 | * @since 3.9.0 |
||
| 42 | */ |
||
| 43 | const KEY = 'key'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * WordLift's configured language option name. |
||
| 47 | * |
||
| 48 | * @since 3.9.0 |
||
| 49 | */ |
||
| 50 | const LANGUAGE = 'site_language'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The publisher entity post ID option name. |
||
| 54 | * |
||
| 55 | * @since 3.9.0 |
||
| 56 | */ |
||
| 57 | const PUBLISHER_ID = 'publisher_id'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The dataset URI option name |
||
| 61 | * |
||
| 62 | * @since 3.10.0 |
||
| 63 | */ |
||
| 64 | const DATASET_URI = 'redlink_dataset_uri'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The Wordlift_Configuration_Service's singleton instance. |
||
| 68 | * |
||
| 69 | * @since 3.6.0 |
||
| 70 | * |
||
| 71 | * @access private |
||
| 72 | * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
||
| 73 | */ |
||
| 74 | private static $instance; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Create a Wordlift_Configuration_Service's instance. |
||
| 78 | * |
||
| 79 | * @since 3.6.0 |
||
| 80 | */ |
||
| 81 | public function __construct() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the singleton instance. |
||
| 89 | * |
||
| 90 | * @since 3.6.0 |
||
| 91 | * |
||
| 92 | * @return \Wordlift_Configuration_Service |
||
| 93 | */ |
||
| 94 | public static function get_instance() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get a configuration given the option name and a key. The option value is |
||
| 101 | * expected to be an array. |
||
| 102 | * |
||
| 103 | * @since 3.6.0 |
||
| 104 | * |
||
| 105 | * @param string $option The option name. |
||
| 106 | * @param string $key A key in the option value array. |
||
| 107 | * @param string $default The default value in case the key is not found (by default an empty string). |
||
| 108 | * |
||
| 109 | * @return mixed The configuration value or the default value if not found. |
||
| 110 | */ |
||
| 111 | private function get( $option, $key, $default = '' ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set a configuration parameter. |
||
| 120 | * |
||
| 121 | * @since 3.9.0 |
||
| 122 | * |
||
| 123 | * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
||
| 124 | * @param string $key The value key. |
||
| 125 | * @param mixed $value The value. |
||
| 126 | */ |
||
| 127 | View Code Duplication | private function set( $option, $key, $value ) { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get the entity base path, by default 'entity'. |
||
| 138 | * |
||
| 139 | * @since 3.6.0 |
||
| 140 | * |
||
| 141 | * @return string The entity base path. |
||
| 142 | */ |
||
| 143 | public function get_entity_base_path() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the entity base path. |
||
| 150 | * |
||
| 151 | * @since 3.9.0 |
||
| 152 | * |
||
| 153 | * @param string $value The entity base path. |
||
| 154 | */ |
||
| 155 | public function set_entity_base_path( $value ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Whether the installation skip wizard should be skipped. |
||
| 162 | * |
||
| 163 | * @since 3.9.0 |
||
| 164 | * |
||
| 165 | * @return bool True if it should be skipped otherwise false. |
||
| 166 | */ |
||
| 167 | public function is_skip_wizard() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set the skip wizard parameter. |
||
| 174 | * |
||
| 175 | * @since 3.9.0 |
||
| 176 | * |
||
| 177 | * @param bool $value True to skip the wizard. We expect a boolean value. |
||
| 178 | */ |
||
| 179 | public function set_skip_wizard( $value ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get WordLift's key. |
||
| 187 | * |
||
| 188 | * @since 3.9.0 |
||
| 189 | * |
||
| 190 | * @return WordLift's key or an empty string if not set. |
||
| 191 | */ |
||
| 192 | public function get_key() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set WordLift's key. |
||
| 199 | * |
||
| 200 | * @since 3.9.0 |
||
| 201 | * |
||
| 202 | * @param string $value WordLift's key. |
||
| 203 | */ |
||
| 204 | public function set_key( $value ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get WordLift's configured language, by default 'en'. |
||
| 211 | * |
||
| 212 | * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
||
| 213 | * |
||
| 214 | * @since 3.9.0 |
||
| 215 | * |
||
| 216 | * @return string WordLift's configured language code ('en' by default). |
||
| 217 | */ |
||
| 218 | public function get_language_code() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
||
| 225 | * |
||
| 226 | * @since 3.9.0 |
||
| 227 | * |
||
| 228 | * @param string $value WordLift's language code. |
||
| 229 | */ |
||
| 230 | public function set_language_code( $value ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get the publisher entity post id. |
||
| 238 | * |
||
| 239 | * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
||
| 240 | * Article markup. |
||
| 241 | * |
||
| 242 | * @since 3.9.0 |
||
| 243 | * |
||
| 244 | * @return int|NULL The publisher entity post id or NULL if not set. |
||
| 245 | */ |
||
| 246 | public function get_publisher_id() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set the publisher entity post id. |
||
| 253 | * |
||
| 254 | * @since 3.9.0 |
||
| 255 | * |
||
| 256 | * @param int $value The publisher entity post id. |
||
| 257 | */ |
||
| 258 | public function set_publisher_id( $value ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the dataset URI. |
||
| 266 | * |
||
| 267 | * @since 3.10.0 |
||
| 268 | * |
||
| 269 | * @return string The dataset URI or an empty string if not set. |
||
| 270 | */ |
||
| 271 | public function get_dataset_uri() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set the dataset URI. |
||
| 278 | * |
||
| 279 | * @since 3.10.0 |
||
| 280 | * |
||
| 281 | * @param string $value The dataset URI. |
||
| 282 | */ |
||
| 283 | public function set_dataset_uri( $value ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Intercept the change of the WordLift key in order to set the dataset URI. |
||
| 290 | * |
||
| 291 | * @since 3.11.0 |
||
| 292 | * |
||
| 293 | * @param array $old_value The old settings. |
||
| 294 | * @param array $new_value The new settings. |
||
| 295 | */ |
||
| 296 | public function update_key( $old_value, $new_value ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the API URI to retrieve the dataset URI using the WordLift Key. |
||
| 329 | * |
||
| 330 | * @since 3.11.0 |
||
| 331 | * |
||
| 332 | * @param string $key The WordLift key to use. |
||
| 333 | * |
||
| 334 | * @return string The API URI. |
||
| 335 | */ |
||
| 336 | public function get_accounts_by_key_dataset_uri( $key ) { |
||
| 340 | |||
| 341 | } |
||
| 342 |
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.