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 Gallery |
||
13 | { |
||
14 | public $image; |
||
15 | public $postmeta; |
||
16 | public $theme; |
||
17 | public $utility; |
||
18 | |||
19 | protected $args; |
||
20 | |||
21 | View Code Duplication | public function __construct( Image $image, PostMeta $postmeta, Theme $theme, Utility $utility ) |
|
1 ignored issue
–
show
|
|||
22 | { |
||
23 | $this->image = $image; |
||
24 | $this->postmeta = $postmeta; |
||
25 | $this->theme = $theme; |
||
26 | $this->utility = $utility; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return WP_Query |
||
31 | */ |
||
32 | public function query( array $args = [] ) |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | */ |
||
50 | public function render( WP_Query $gallery ) |
||
57 | |||
58 | /** |
||
59 | * @return null|string |
||
60 | */ |
||
61 | public function renderImage( WP_Post $attachment ) |
||
79 | |||
80 | /** |
||
81 | * @return null|string |
||
82 | */ |
||
83 | public function renderPagination( WP_Query $query ) |
||
95 | |||
96 | /** |
||
97 | * @param string $key |
||
98 | * @param mixed $value |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | protected function getBoolValue( $key, $value = null ) |
||
113 | |||
114 | /** |
||
115 | * @param mixed $value |
||
116 | * |
||
117 | * @return int |
||
118 | */ |
||
119 | protected function getGalleryArg( $value = null ) |
||
130 | |||
131 | /** |
||
132 | * @param mixed $value |
||
133 | * |
||
134 | * @return int |
||
135 | */ |
||
136 | protected function getImagesPerPageArg( $value = null ) |
||
137 | { |
||
138 | $perPage = $this->getValue( 'images_per_page', $value ); |
||
139 | |||
140 | if( !is_numeric( $perPage ) && is_string( $perPage )) { |
||
141 | $perPage = $this->postmeta->get( $perPage ); |
||
142 | } |
||
143 | return !!intval( $perPage ) |
||
144 | ? $perPage |
||
145 | : -1; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param mixed $value |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | protected function getLazyloadArg( $value = null ) |
||
157 | |||
158 | /** |
||
159 | * @param mixed $value |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | protected function getMediaArg( $value = null ) |
||
177 | |||
178 | /** |
||
179 | * @return int |
||
180 | */ |
||
181 | protected function getPaged() |
||
185 | |||
186 | /** |
||
187 | * @param mixed $value |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | protected function getPaginationArg( $value = null ) |
||
195 | |||
196 | /** |
||
197 | * @param mixed $value |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | protected function getPermalinksArg( $value = null ) |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | protected function getPhotoswipeData( $image ) |
||
220 | |||
221 | /** |
||
222 | * @param string $key |
||
223 | * @param mixed $value |
||
224 | * |
||
225 | * @return mixed |
||
226 | */ |
||
227 | protected function getValue( $key, $value = null ) |
||
234 | |||
235 | /** |
||
236 | * @return array |
||
237 | */ |
||
238 | protected function normalizeArgs( array $args = [] ) |
||
260 | |||
261 | /** |
||
262 | * @param object $image |
||
263 | * |
||
264 | * @return null|string |
||
265 | */ |
||
266 | protected function renderImageTag( $image ) |
||
282 | } |
||
283 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.