Score::text()   F
last analyzed

Complexity

Conditions 20
Paths 3121

Size

Total Lines 77
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 77
rs 0
c 0
b 0
f 0
cc 20
nc 3121
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace NirjharLo\Cgss\Lib\Analysis\Lib;
4
5
if ( ! defined( 'ABSPATH' ) ) exit;
6
7
/**
8
 * From result data found in scan, analyze a score of on-page optimization. Using approximate values
9
 * from qualitative ideas of seo. Sometimes statistical approximation has been taken.
10
 *
11
 * 5 properties:
12
 * @property array $snippet
13
 * @property array $text
14
 * @property array $design
15
 * @property array $crawl
16
 * @property array $speed
17
 */
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $links does not seem to be defined for all execution paths leading up to this point.
Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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 ) {
316
		$show = 0;
317
		$text = $this->text;
318
		$keys = $text['keys'];
319
		foreach ( $keys as $key => $val ) {
320
321
			$num = substr_count( $key, $txt );
322
			if ( $num > 0 ) {
323
				$show = 1;
324
			}
325
		}
326
		if ( $show == 0 ) {
327
			return false;
328
		} else {
329
			return $show;
330
		}
331
	}
332
}
333
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
334