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 |
||
9 | class GL_Plugin_Check_v1 |
||
1 ignored issue
–
show
|
|||
10 | { |
||
11 | const MIN_PHP_VERSION = '5.6.0'; |
||
12 | const MIN_WORDPRESS_VERSION = '4.7.0'; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected static $file; |
||
18 | |||
19 | /** |
||
20 | * @var static |
||
21 | */ |
||
22 | protected static $instance; |
||
23 | |||
24 | /** |
||
25 | * @var object |
||
26 | */ |
||
27 | protected static $versions; |
||
28 | |||
29 | /** |
||
30 | * @param string $version |
||
31 | * @return bool |
||
32 | */ |
||
33 | View Code Duplication | public static function isPhpValid( $version = '' ) |
|
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | public static function isValid( array $args = array() ) |
||
51 | |||
52 | /** |
||
53 | * @param string $version |
||
54 | * @return bool |
||
55 | */ |
||
56 | View Code Duplication | public static function isWpValid( $version = '' ) |
|
64 | |||
65 | /** |
||
66 | * @param string $file |
||
67 | * @return bool |
||
68 | */ |
||
69 | public static function shouldDeactivate( $file, array $args = array() ) |
||
83 | |||
84 | /** |
||
85 | * @param string $plugin |
||
86 | * @return void |
||
87 | */ |
||
88 | public function deactivate( $plugin ) |
||
99 | |||
100 | /** |
||
101 | * @return object |
||
102 | */ |
||
103 | protected static function normalize( array $args = array() ) |
||
110 | |||
111 | /** |
||
112 | * @return void |
||
113 | */ |
||
114 | protected function redirect() |
||
123 | |||
124 | /** |
||
125 | * @param string $pluginName |
||
126 | * @return void |
||
127 | */ |
||
128 | protected function printNotice( $pluginName ) |
||
154 | } |
||
155 |
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.