Total Complexity | 70 |
Total Lines | 312 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Score 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.
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 Score, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Score { |
||
19 | |||
20 | |||
21 | public $snippet; |
||
22 | public $text; |
||
23 | public $design; |
||
24 | public $crawl; |
||
25 | public $speed; |
||
26 | |||
27 | |||
28 | public function exact() { |
||
29 | $score = $this->snip() + $this->text() + $this->design() + $this->crawl() + $this->speed() + 245; |
||
30 | return $score; |
||
31 | } |
||
32 | |||
33 | //out of [-245, +400] we score the page. First, add the grace 150 to total number obtained. |
||
34 | // Now devide it in a series of 8 segment, remember Gap, betwwen the fingers. Put approx values. |
||
35 | public function calculate() { |
||
36 | $score = $this->exact(); |
||
37 | |||
38 | $star = 0; |
||
39 | switch ( true ) { |
||
40 | case ( $score >= 560 ): |
||
41 | $star = 10; |
||
42 | break; |
||
43 | case ( $score >= 490 ): |
||
44 | $star = 9; |
||
45 | break; |
||
46 | case ( $score >= 420 ): |
||
47 | $star = 8; |
||
48 | break; |
||
49 | case ( $score >= 350 ): |
||
50 | $star = 7; |
||
51 | break; |
||
52 | case ( $score >= 280 ): |
||
53 | $star = 6; |
||
54 | break; |
||
55 | case ( $score >= 210 ): |
||
56 | $star = 5; |
||
57 | break; |
||
58 | case ( $score >= 140 ): |
||
59 | $star = 4; |
||
60 | break; |
||
61 | case ( $score >= 70 ): |
||
62 | $star = 3; |
||
63 | break; |
||
64 | case ( $score >= 0 ): |
||
65 | $star = 2; |
||
66 | break; |
||
67 | case ( $score <= 0 ): |
||
68 | $star = 1; |
||
69 | break; |
||
70 | } |
||
71 | |||
72 | return $star; |
||
73 | } |
||
74 | |||
75 | //Check snippet lengths and score it. Find most used word in them and score: 50, 50 |
||
76 | public function snip() { |
||
77 | $data = $this->snippet; |
||
78 | $score = 0; |
||
79 | |||
80 | $arr = array( $data['title'], $data['desc'] ); |
||
81 | foreach ( $arr as $key ) { |
||
82 | switch ( true ) { |
||
83 | case ( $key && $key != '' ): |
||
84 | $score += 25; |
||
85 | break; |
||
86 | case ( ! $key || $key == '' ): |
||
87 | $score -= 25; |
||
88 | break; |
||
89 | } |
||
90 | } |
||
91 | return $score; |
||
92 | } |
||
93 | |||
94 | //Analyze text for calculating score by words count, html/text ratio, link to word ratio, |
||
95 | //image link to all link ratio and usage of most used word in anchor texts and heading texts: 200, 75 |
||
96 | public function text() { |
||
97 | $data = $this->text; |
||
98 | if ( $data ) { |
||
|
|||
99 | $score = 0; |
||
100 | $count = $data['count']; |
||
101 | switch ( true ) { |
||
102 | case ( $count < 200 ): |
||
103 | $score -= 10; |
||
104 | break; |
||
105 | case ( $count > 200 ): |
||
106 | $score += 25; |
||
107 | break; |
||
108 | } |
||
109 | $ratio = $data['ratio']; |
||
110 | switch ( true ) { |
||
111 | case ( $ratio <= 15 ): |
||
112 | $score -= 10; |
||
113 | break; |
||
114 | case ( $ratio <= 70 ): |
||
115 | $score += 25; |
||
116 | break; |
||
117 | case ( $ratio > 70 ): |
||
118 | $score -= 10; |
||
119 | break; |
||
120 | } |
||
121 | if ( $data['links'] ) { |
||
122 | $links = $data['links']; |
||
123 | if ( $count > 0 ) { |
||
124 | $link_ratio = ( ( $links['count'] / $count ) * 100 ); |
||
125 | switch ( true ) { |
||
126 | case ( $link_ratio <= 50 ): |
||
127 | $score += 10; |
||
128 | break; |
||
129 | case ( $link_ratio > 50 ): |
||
130 | $score -= 5; |
||
131 | break; |
||
132 | } |
||
133 | } |
||
134 | switch ( true ) { |
||
135 | case ( $links['no_text'] < 2 ): |
||
136 | $score += 10; |
||
137 | break; |
||
138 | case ( $links['no_text'] > 2 ): |
||
139 | $score -= 5; |
||
140 | break; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | //Check for keywords in various area: 60, 0 |
||
145 | $anch = $links['anchors']; |
||
146 | $htags = $data['htags']['content']; |
||
147 | $hds = ''; |
||
148 | foreach ($htags as $value) { |
||
149 | $hds .= ' ' . implode(' ', $value); |
||
150 | } |
||
151 | $snip_data = $this->snippet; |
||
152 | $design_data = $this->design; |
||
153 | $img_data = $design_data['image']; |
||
154 | $alts = (!empty($img_data['alt_value']) ? implode(' ', $img_data['alt_value']) : []); |
||
155 | $url = implode( " ", explode( "-", $snip_data['url'] ) ); |
||
156 | $arr = array( $snip_data['title'], $snip_data['desc'], $alts, $url, $anch, $hds ); |
||
157 | foreach ( $arr as $val ) { |
||
158 | if ( $val && $val != '' ) { |
||
159 | $find_key = $this->key_chk( $val ); |
||
160 | switch ( true ) { |
||
161 | case ( ! $find_key ): |
||
162 | $score += 0; |
||
163 | break; |
||
164 | case ( $find_key > 0 ): |
||
165 | $score += 10; |
||
166 | break; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | return $score; |
||
171 | } else { |
||
172 | return 0; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | //Score based on alt tags presence in images and top word found in those alt tags: 70, 45 |
||
177 | public function design() { |
||
178 | $data = $this->design; |
||
179 | if ( $data ) { |
||
180 | $score = 0; |
||
181 | $img_data = $data['image']; |
||
182 | $no_alt_data = $img_data['no_alt_count']; |
||
183 | switch ( $no_alt_data ) { |
||
184 | case 0: |
||
185 | $score += 25; |
||
186 | break; |
||
187 | default: |
||
188 | $score -= 25; |
||
189 | break; |
||
190 | } |
||
191 | |||
192 | $nested_table = $data['nested_table']; |
||
193 | switch ( $nested_table ) { |
||
194 | case 0: |
||
195 | $score += 10; |
||
196 | break; |
||
197 | default: |
||
198 | $score -= 5; |
||
199 | break; |
||
200 | } |
||
201 | |||
202 | $tag_style = $data['tag_style']; |
||
203 | switch ( $tag_style['count'] ) { |
||
204 | case 0: |
||
205 | $score += 10; |
||
206 | break; |
||
207 | default: |
||
208 | $score -= 5; |
||
209 | break; |
||
210 | } |
||
211 | |||
212 | $vport = $data['vport']; |
||
213 | $media = $data['media']; |
||
214 | $media_ok = $media['ok']; |
||
215 | if ( $vport and $media_ok and strlen( $vport ) > 0 and $media_ok > 0 ) { |
||
216 | $score += 25; |
||
217 | } else { |
||
218 | $score -= 10; |
||
219 | } |
||
220 | return $score; |
||
221 | } else { |
||
222 | return 0; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | //determine score number of http requests and presence of code errors, viewport and social tags: 80, 40 |
||
227 | public function crawl() { |
||
228 | $data = $this->crawl; |
||
229 | if ( $data ) { |
||
230 | $ip = $data['ip']; |
||
231 | $meta_robot = $data['meta_robot']; |
||
232 | |||
233 | if ( ! $data['cano'] or strlen( $data['cano'] ) != 0 ) { |
||
234 | $cano = 1; |
||
235 | } else { |
||
236 | $cano = 0; |
||
237 | } |
||
238 | $arr = array( $data['ssl'], $data['dynamic'], $data['underscore'], $cano, $ip, $data['if_mod'], $meta_robot['ok'] ); |
||
239 | $score = 0; |
||
240 | foreach ( $arr as $val ) { |
||
241 | switch ( $val ) { |
||
242 | case 1: |
||
243 | $score += 10; |
||
244 | break; |
||
245 | default: |
||
246 | $score -= 5; |
||
247 | break; |
||
248 | } |
||
249 | } |
||
250 | return $score; |
||
251 | } else { |
||
252 | return 0; |
||
253 | } |
||
254 | } |
||
255 | |||
256 | //Overview score for url and canonical tag. Excluding robots tag,to be taken care of by jquery: 70, 35 |
||
257 | public function speed() { |
||
258 | $data = $this->speed; |
||
259 | if ( $data ) { |
||
260 | $score = 0; |
||
261 | $down_time = $data['down_time']; |
||
262 | switch ( true ) { |
||
263 | case ( $down_time > 10 ): |
||
264 | $score -= 5; |
||
265 | break; |
||
266 | case ( $down_time < 3 ): |
||
267 | $score += 10; |
||
268 | break; |
||
269 | case ( $down_time <= 5 ): |
||
270 | $score += 5; |
||
271 | break; |
||
272 | } |
||
273 | $arr = array( $data['gzip'], $data['cache'] ); |
||
274 | foreach ( $arr as $val ) { |
||
275 | switch ( $val ) { |
||
276 | case 1: |
||
277 | $score += 10; |
||
278 | break; |
||
279 | default: |
||
280 | $score -= 5; |
||
281 | break; |
||
282 | } |
||
283 | } |
||
284 | $css = $data['css']; |
||
285 | $js = $data['js']; |
||
286 | $files_arr = array( $css['count'], $js['count'] ); |
||
287 | foreach ( $files_arr as $val ) { |
||
288 | switch ( true ) { |
||
289 | case ( $val <= 10 ): |
||
290 | $score += 10; |
||
291 | break; |
||
292 | case ( $val > 10 ): |
||
293 | $score -= 5; |
||
294 | break; |
||
295 | } |
||
296 | } |
||
297 | $files_size_arr = array( $css['size'], $js['size'] ); |
||
298 | foreach ( $files_size_arr as $val ) { |
||
299 | switch ( true ) { |
||
300 | case ( $val <= 100 ): |
||
301 | $score += 10; |
||
302 | break; |
||
303 | case ( $val > 100 ): |
||
304 | $score -= 5; |
||
305 | break; |
||
306 | } |
||
307 | } |
||
308 | return $score; |
||
309 | } else { |
||
310 | return 0; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | //Get the top word found |
||
315 | public function key_chk( $txt ) { |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | ?> |
||
334 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.