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 |
||
12 | class Sensei_Course_Results { |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | public $courses_url_base; |
||
18 | |||
19 | /** |
||
20 | * Constructor. |
||
21 | * @since 1.4.0 |
||
22 | */ |
||
23 | public function __construct () { |
||
24 | |||
25 | // Setup learner profile URL base |
||
26 | $this->courses_url_base = apply_filters( 'sensei_course_slug', _x( 'course', 'post type single url slug', 'woothemes-sensei' ) ); |
||
27 | |||
28 | // Setup permalink structure for course results |
||
29 | add_action( 'init', array( $this, 'setup_permastruct' ) ); |
||
30 | add_filter( 'wp_title', array( $this, 'page_title' ), 10, 2 ); |
||
31 | |||
32 | // Load course results |
||
33 | add_action( 'sensei_course_results_content_inside_before', array( $this, 'deprecate_course_result_info_hook' ), 10 ); |
||
34 | |||
35 | // Add class to body tag |
||
36 | add_filter( 'body_class', array( $this, 'body_class' ), 10, 1 ); |
||
37 | |||
38 | } // End __construct() |
||
39 | |||
40 | /** |
||
41 | * Setup permalink structure for course results |
||
42 | * @since 1.4.0 |
||
43 | * @return void |
||
44 | */ |
||
45 | public function setup_permastruct() { |
||
49 | |||
50 | /** |
||
51 | * Adding page title for course results page |
||
52 | * @param string $title Original title |
||
53 | * @param string $sep Seeparator string |
||
54 | * @return string Modified title |
||
55 | */ |
||
56 | public function page_title( $title, $sep = null ) { |
||
64 | |||
65 | /** |
||
66 | * Get permalink for course results based on course ID |
||
67 | * @since 1.4.0 |
||
68 | * @param integer $course_id ID of course |
||
69 | * @return string The course results page permalink |
||
70 | */ |
||
71 | public function get_permalink( $course_id = 0 ) { |
||
88 | |||
89 | /** |
||
90 | * Load content for course results |
||
91 | * @since 1.4.0 |
||
92 | * @return void |
||
93 | */ |
||
94 | public function content() { |
||
102 | |||
103 | /** |
||
104 | * Load course results info |
||
105 | * @since 1.4.0 |
||
106 | * @return void |
||
107 | */ |
||
108 | public function course_info() { |
||
119 | |||
120 | /** |
||
121 | * Load template for displaying course lessons |
||
122 | * |
||
123 | * @since 1.4.0 |
||
124 | * @return void |
||
125 | */ |
||
126 | public function course_lessons() { |
||
132 | |||
133 | /** |
||
134 | * Adding class to body tag |
||
135 | * @param array $classes Existing classes |
||
136 | * @return array Modified classes |
||
137 | */ |
||
138 | public function body_class( $classes ) { |
||
145 | |||
146 | /** |
||
147 | * Deprecate the sensei_course_results_content hook |
||
148 | * |
||
149 | * @deprecated since 1.9.0 |
||
150 | */ |
||
151 | public static function deprecate_sensei_course_results_content_hook(){ |
||
156 | |||
157 | /** |
||
158 | * Fire the sensei frontend message hook |
||
159 | * |
||
160 | * @since 1.9.0 |
||
161 | */ |
||
162 | public static function fire_sensei_message_hook(){ |
||
167 | |||
168 | /** |
||
169 | * Deprecate the course_results info hook |
||
170 | * |
||
171 | * @since 1.9.0 |
||
172 | */ |
||
173 | public static function deprecate_course_result_info_hook(){ |
||
178 | |||
179 | /** |
||
180 | * Deprecate the sensei_course_results_top hook |
||
181 | * |
||
182 | * @deprecate since 1.9.0 |
||
183 | */ |
||
184 | public static function deprecate_course_results_top_hook(){ |
||
190 | |||
191 | /** |
||
192 | * Fire the course image hook |
||
193 | * |
||
194 | * @since 1.8.0 |
||
195 | */ |
||
196 | public static function fire_course_image_hook(){ |
||
202 | |||
203 | } // End Class |
||
204 | |||
211 |
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.