Completed
Push — master ( bc6754...ed64c2 )
by
unknown
07:19
created

Comment   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 335
Duplicated Lines 4.78 %

Coupling/Cohesion

Components 5
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 335
rs 8.2608
wmc 40
lcom 5
cbo 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A author() 0 20 4
A content() 0 3 1
A approved() 0 3 1
A date() 0 6 2
A has_parent() 0 3 1
B avatar() 0 27 5
A get_avatar_email() 0 14 2
C get_default_avatar() 0 31 12
A get_avatar_host() 0 15 3
A get_avatar_url() 0 12 2
A add_child() 0 10 2
A update_child_levels() 16 16 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Comment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Comment, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Classy;
3
4
class Comment extends Basis {
5
6
	/**
7
	 * Comment ID
8
	 * @var int
9
	 */
10
	public $comment_ID;
11
12
	/**
13
	 * Comment post ID
14
	 * @var int
15
	 */
16
	public $comment_post_ID;
17
18
	/**
19
	 * Comment Author name
20
	 * @var string
21
	 */
22
	public $comment_author;
23
24
	/**
25
	 * Comment author email
26
	 * @var string
27
	 */
28
	public $comment_author_email;
29
30
	/**
31
	 * Comment author link
32
	 * @var string
33
	 */
34
	public $comment_author_url;
35
36
	/**
37
	 * Comment
38
	 * @var string
39
	 */
40
	public $comment_content;
41
42
	/**
43
	 * Comment approved
44
	 * @var boolean
45
	 */
46
	public $comment_approved;
47
48
49
	/**
50
	 * Comment date
51
	 * @var string
52
	 */
53
	public $comment_date;
54
55
	/**
56
	 * User Id
57
	 * @var int
58
	 */
59
	public $user_id;
60
61
	/**
62
	 * Comment nested Level
63
	 * @var int
64
	 */
65
	public $level;
66
67
	/**
68
	 * Comment Parent Id
69
	 * @var int
70
	 */
71
	public $comment_parent;
72
73
	/**
74
	 * Child comments
75
	 * @var array
76
	 */
77
	protected $children = array();
78
79
	/**
80
	 * Checks if provided arg is instance of WP_Comment and inits it
81
	 *
82
	 * @param \WP_Comment $item
83
	 */
84
	public function __construct( $item ) {
85
86
		if ( is_a( $item, '\WP_Comment' ) ) {
87
88
			$this->import( $item );
89
90
		}
91
92
	}
93
94
	/**
95
	 * Returns User object of comment author
96
	 *
97
	 * @return object User
98
	 */
99
	public function author() {
100
101
		if ( $this->user_id ) {
102
103
			return new Models\User( $this->user_id );
104
105
		} else {
106
107
			$author = new Models\User( 0 );
108
109
			if ( isset( $this->comment_author ) && $this->comment_author ) {
110
111
				$author->name = $this->comment_author;
112
113
			}
114
		}
115
116
		return $author;
117
118
	}
119
120
	/**
121
	 * Returns comment content
122
	 *
123
	 * @return string
124
	 */
125
	public function content() {
126
		return apply_filters( 'get_comment_text ', $this->comment_content );
127
	}
128
129
	/**
130
	 * Return true if comment is approved
131
	 *
132
	 * @return boolean
133
	 */
134
	public function approved() {
135
		return $this->comment_approved;
136
	}
137
138
	/**
139
	 * Returns comment date
140
	 *
141
	 * @param  string $date_format
142
	 * @return string
143
	 */
144
	public function date( $date_format = '' ) {
145
		$df = $date_format ? $date_format : get_option( 'date_format' );
146
		$the_date = (string) mysql2date( $df, $this->comment_date );
147
148
		return apply_filters( 'get_comment_date ', $the_date, $df );
149
	}
150
151
	/**
152
	 * Returns true if comment has parent
153
	 *
154
	 * @return boolean
155
	 */
156
	public function has_parent() {
157
		return $this->comment_parent > 0;
158
	}
159
160
	/**
161
	 * Shows gravatar
162
	 *
163
	 * @param  integer $size    avatar image size
164
	 * @param  string $default
165
	 * @return string
166
	 */
167
	public function avatar( $size = 92, $default = '' ) {
168
169
		if ( ! get_option( 'show_avatars' ) ) {
170
			return false;
171
		}
172
173
		if ( ! is_numeric( $size ) ) { $size = '92'; }
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $size. This often makes code more readable.
Loading history...
174
175
		$email = $this->get_avatar_email();
176
		$email_hash = '';
177
178
		if ( ! empty( $email ) ) {
179
			$email_hash = md5( strtolower( trim( $email ) ) );
180
		}
181
182
		$host = $this->get_avatar_host( $email_hash );
183
		$default = $this->get_default_avatar( $default, $email, $size, $host );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
184
185
		if ( ! empty( $email ) ) {
186
			$avatar = $this->get_avatar_url( $default, $host, $email_hash, $size );
187
		} else {
188
			$avatar = $default;
189
		}
190
191
		return $avatar;
192
193
	}
194
195
	/**
196
	 * Returns email address that will be used for generating avatar
197
	 *
198
	 * @return string
199
	 */
200
	protected function get_avatar_email() {
201
202
		$id = (int) $this->user_id;
203
		$user = get_userdata( $id );
204
205
		if ( $user ) {
206
			$email = $user->user_email;
207
		} else {
208
			$email = $this->comment_author_email;
209
		}
210
211
		return $email;
212
213
	}
214
215
	/**
216
	 * Returns default avatar url
217
	 *
218
	 * @param  string $default
219
	 * @param  string $email
220
	 * @param  int $size
221
	 * @param  string $host
222
	 * @return string
223
	 */
224
	protected function get_default_avatar( $default, $email, $size, $host ) {
225
226
		if ( '/' === substr( $default, 0, 1 ) ) {
227
			$default = home_url() . $default;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
228
		}
229
230
		if ( empty( $default ) ) {
231
			$avatar_default = get_option( 'avatar_default' );
232
			if ( empty( $avatar_default ) ) {
233
				$default = 'mystery';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
234
			} else {
235
				$default = $avatar_default;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
236
			}
237
		}
238
239
		if ( 'mystery' === $default ) {
240
			$default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
241
			// ad516503a11cd5ca435acc9bb6523536 == md5('[email protected]')
242
		} else if ( 'blank' === $default ) {
243
			$default = $email ? 'blank' : includes_url( 'images/blank.gif' );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
244
		} else if ( ! empty( $email ) && 'gravatar_default' === $default ) {
245
			$default = '';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
246
		} else if ( 'gravatar_default' === $default ) {
247
			$default = $host . '/avatar/?s=' . $size;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
248
		} else if ( empty( $email ) && ! strstr( $default, 'http://' ) ) {
249
			$default = $host . '/avatar/?d=' . $default . '&amp;s=' . $size;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
250
		}
251
252
		return $default;
253
254
	}
255
256
	/**
257
	 * Returns gravatar host
258
	 *
259
	 * @param  string $email_hash
260
	 * @return string
261
	 */
262
	protected function get_avatar_host( $email_hash ) {
263
264
		if ( is_ssl() ) {
265
			$host = 'https://secure.gravatar.com';
266
		} else {
267
			if ( ! empty( $email_hash ) ) {
268
				$host = sprintf( 'http://%d.gravatar.com', (hexdec( $email_hash[0] ) % 2) );
269
			} else {
270
				$host = 'http://0.gravatar.com';
271
			}
272
		}
273
274
		return $host;
275
276
	}
277
278
	/**
279
	 * Returns avatar url
280
	 *
281
	 * @param  string $default
282
	 * @param  string $host
283
	 * @param  string $email_hash
284
	 * @param  int $size
285
	 * @return string
286
	 */
287
	protected function get_avatar_url( $default, $host, $email_hash, $size ) {
288
289
		$_return = $host . '/avatar/' . $email_hash . '?s=' . $size . '&amp;d=' . urlencode( $default );
290
		$rating = get_option( 'avatar_rating' );
291
292
		if ( ! empty( $rating ) ) {
293
			$_return .= '&amp;r=' . $rating;
294
		}
295
296
		return str_replace( '&#038;', '&amp;', esc_url( $_return ) );
297
298
	}
299
300
	/**
301
	 * Adds child to current Comment
302
	 *
303
	 * @param Comment $comment
304
	 */
305
	public function add_child( $comment ) {
306
307
		$this->children[] = $comment;
308
		$item->level = $this->level + 1;
0 ignored issues
show
Bug introduced by
The variable $item does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
309
310
		if ( $item->children ) {
311
			$this->update_child_levels();
312
		}
313
314
	}
315
316
317
	/**
318
	 * Updates children nesting level param
319
	 *
320
	 * @return boolean
321
	 */
322 View Code Duplication
	protected function update_child_levels() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
323
324
		if ( is_array( $this->children ) ) {
325
326
			foreach ( $this->children as $child ) {
327
				$child->level = $this->level + 1;
328
				$child->update_child_levels();
329
			}
330
331
			return true;
332
333
		}
334
335
		return false;
336
337
	}
338
}
339