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 |
||
24 | class DynamicCSSCompiler |
||
|
|||
25 | { |
||
26 | /** |
||
27 | * @var Singleton The reference to *Singleton* instance of this class |
||
28 | */ |
||
29 | private static $instance; |
||
30 | |||
31 | /** |
||
32 | * @var array The list of dynamic styles paths to compile |
||
33 | */ |
||
34 | private $stylesheets = array(); |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $callbacks = array(); |
||
40 | |||
41 | /** |
||
42 | * Returns the *Singleton* instance of this class. |
||
43 | * |
||
44 | * @return Singleton The *Singleton* instance. |
||
45 | */ |
||
46 | public static function get_instance() |
||
54 | |||
55 | /** |
||
56 | * Enqueue the PHP script used for compiling dynamic stylesheets that are |
||
57 | * loaded externally |
||
58 | */ |
||
59 | public function wp_enqueue_style() |
||
68 | |||
69 | /** |
||
70 | * Parse all styles in $this->stylesheets and print them if the flag 'print' |
||
71 | * is set to true. Used for printing styles to the document head. |
||
72 | */ |
||
73 | public function compile_printed_styles() |
||
91 | |||
92 | /** |
||
93 | * Parse all styles in $this->stylesheets and print them if the flag 'print' |
||
94 | * is not set to true. Used for loading styles externally via an http request. |
||
95 | */ |
||
96 | public function compile_external_styles() |
||
111 | |||
112 | /** |
||
113 | * Add a style path to the pool of styles to be compiled |
||
114 | * |
||
115 | * @param string $handle The stylesheet's name/id |
||
116 | * @param string $path The absolute path to the dynamic style |
||
117 | * @param boolean $print Whether to print the compiled CSS to the document |
||
118 | * head, or include it as an external CSS file |
||
119 | */ |
||
120 | public function enqueue_style( $handle, $path, $print ) |
||
128 | |||
129 | /** |
||
130 | * Register a value retrieval function and associate it with the given handle |
||
131 | * |
||
132 | * @param type $handle The stylesheet's name/id |
||
133 | * @param type $callback |
||
134 | */ |
||
135 | public function register_callback( $handle, $callback ) |
||
139 | |||
140 | /** |
||
141 | * This filter is used to return only the styles that are set to be printed |
||
142 | * in the document head |
||
143 | * |
||
144 | * @param array $style |
||
145 | * @return boolean |
||
146 | */ |
||
147 | protected function filter_print( $style ) |
||
151 | |||
152 | /** |
||
153 | * This filter is used to return only the styles that are set to be loaded |
||
154 | * externally |
||
155 | * |
||
156 | * @param array $style |
||
157 | * @return boolean |
||
158 | */ |
||
159 | protected function filter_external( $style ) |
||
163 | |||
164 | /** |
||
165 | * Parse the given CSS string by converting the variables to their |
||
166 | * corresponding values retrieved by applying the callback function |
||
167 | * |
||
168 | * @param callable $callback A function that replaces the variables with |
||
169 | * their values. The function accepts the variable's name as a parameter |
||
170 | * @param string $css A string containing dynamic CSS (pre-compiled CSS with |
||
171 | * variables) |
||
172 | * @return string The compiled CSS after converting the variables to their |
||
173 | * corresponding values |
||
174 | */ |
||
175 | protected function compile_css( $css, $callback ) |
||
181 | } |
||
182 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.