Conditions | 12 |
Paths | 48 |
Total Lines | 85 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
108 | protected function setup_course_query(){ |
||
109 | |||
110 | // query defaults |
||
111 | $query_args = array( |
||
112 | 'post_type' => 'course', |
||
113 | 'post_status' => 'publish', |
||
114 | 'orderby' => $this->orderby, |
||
115 | 'order' => $this->order, |
||
116 | 'posts_per_page' => $this->number, |
||
117 | |||
118 | ); |
||
119 | |||
120 | // setup the teacher query if any teacher was specified |
||
121 | if( !empty( $this->teacher )){ |
||
122 | |||
123 | // when users passed in a csv |
||
124 | if( strpos( $this->teacher, ',' ) ){ |
||
125 | |||
126 | $teachers = explode( ',', $this->teacher ); |
||
127 | |||
128 | // for all user names given convert them to user ID's |
||
129 | foreach( $teachers as $index => $teacher ){ |
||
130 | |||
131 | //replace the teacher value with the teachers ID |
||
132 | if( ! is_numeric( $teacher ) ){ |
||
133 | |||
134 | $user = get_user_by('login', $teacher); |
||
135 | $teachers[$index] = $user->ID; |
||
136 | |||
137 | } |
||
138 | |||
139 | } // end for each |
||
140 | |||
141 | $teacher_query_by = 'author__in'; |
||
142 | $this->teacher = $teachers; |
||
143 | |||
144 | }else{ |
||
145 | // when users passed in a single teacher value |
||
146 | $teacher_query_by = is_numeric( $this->teacher )? 'author':'author_name'; |
||
147 | |||
148 | } |
||
149 | |||
150 | // attach teacher query by and teacher query value to the course query |
||
151 | $query_args[ $teacher_query_by ] = $this->teacher; |
||
152 | |||
153 | }// end if empty teacher |
||
154 | |||
155 | |||
156 | // add the course category taxonomy query |
||
157 | if( ! empty( $this->category ) ) { |
||
158 | |||
159 | $tax_query = array(); |
||
160 | $term_id = intval( term_exists($this->category) ); |
||
161 | |||
162 | if (! empty( $term_id) ) { |
||
163 | |||
164 | $tax_query = array( |
||
165 | 'taxonomy' => 'course-category', |
||
166 | 'field' => 'id', |
||
167 | 'terms' => $term_id, |
||
168 | ); |
||
169 | |||
170 | } |
||
171 | |||
172 | $query_args['tax_query'] = array($tax_query); |
||
173 | |||
174 | } |
||
175 | |||
176 | // limit the query if the user supplied ids |
||
177 | if( ! empty( $this->ids ) && is_array( $this->ids ) ) { |
||
178 | |||
179 | $query_args['post__in'] = $this->ids; |
||
180 | |||
181 | } |
||
182 | |||
183 | // exclude the course by id fromt he query |
||
184 | if( ! empty( $this->exclude ) && is_array( $this->exclude ) ) { |
||
185 | |||
186 | $query_args['post__not_in'] = $this->exclude; |
||
187 | |||
188 | } |
||
189 | |||
190 | $this->query = new WP_Query( $query_args ); |
||
191 | |||
192 | }// end setup _course_query |
||
193 | |||
220 | } |
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.