Design::analyze_js()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 32
rs 9.2568
c 0
b 0
f 0
cc 5
nc 2
nop 0
1
<?php
2
namespace NirjharLo\Cgss\Lib\Analysis\Lib;
3
4
if ( ! defined( 'ABSPATH' ) ) exit;
5
6
7
/**
8
 * Download and analyze .css or .js files, based on an url.
9
 *
10
 * 2 properties:
11
 * @property array $css_url Input CSS urls
12
 * @property array $js_url Input JS urls
13
 */
14
15
class Design {
16
17
18
	public $css_url;
19
	public $js_url;
20
21
22
	//analyze those urls and their content
23
	public function analyze_js() {
24
25
		$num = 0;
26
		$size = 0;
27
		$compress_num = 0;
28
		$compress_size = 0;
29
30
		if ( $this->js_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->js_url 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...
31
32
			$num = count($this->js_url);
33
34
			//get files and check their bodies
35
			foreach ( $this->js_url as $val ) {
36
				$body = @file_get_contents( $val, FILE_USE_INCLUDE_PATH );
0 ignored issues
show
Bug introduced by
NirjharLo\Cgss\Lib\Analy...b\FILE_USE_INCLUDE_PATH of type integer is incompatible with the type boolean expected by parameter $use_include_path of file_get_contents(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
				$body = @file_get_contents( $val, /** @scrutinizer ignore-type */ FILE_USE_INCLUDE_PATH );
Loading history...
37
				if ($body) {
38
					$pre_size = mb_strlen( $body, '8bit' );
39
					$size += round( ( $pre_size / 1024 ), 0 );
40
					$pure_body = filter_var( $body, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW );
41
					$pure_size = mb_strlen( $pure_body, '8bit' );
42
					$percent_comp = 1 - ( $pure_size / $pre_size );
43
					if ( $percent_comp > 0.05 ) {
44
						$compress_num += 1;
45
						$compress_size += round( ( ( $pre_size - $pure_size ) / 1024 ), 0 );
46
					}
47
				}
48
			}
49
		}
50
		return array(
51
					'count' => $num,
52
					'size' => $size,
53
					'compress_num' => $compress_num,
54
					'compress_size' => $compress_size,
55
				);
56
	}
57
58
59
	//analyze those urls and their content
60
	public function analyze_css() {
61
62
		$num = 0;
63
		$size = 0;
64
		$import = 0;
65
		$media = 0;
66
		$compress_num = 0;
67
		$compress_size = 0;
68
69
		if ( $this->css_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->css_url 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...
70
71
			$num = count($this->css_url);
72
73
			//get files and check their bodies
74
			foreach ( $this->css_url as $val ) {
75
				$body = @file_get_contents( $val, FILE_USE_INCLUDE_PATH );
0 ignored issues
show
Bug introduced by
NirjharLo\Cgss\Lib\Analy...b\FILE_USE_INCLUDE_PATH of type integer is incompatible with the type boolean expected by parameter $use_include_path of file_get_contents(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
				$body = @file_get_contents( $val, /** @scrutinizer ignore-type */ FILE_USE_INCLUDE_PATH );
Loading history...
76
				if ($body) {
77
					$pre_size = mb_strlen( $body, '8bit' );
78
					$size += round( ( $pre_size / 1024 ), 0 );
79
					if ( strpos( $val, '.css' ) !== false ) {
80
						$import += substr_count( $body, '@import' );
81
						$media += substr_count( $body, '@media' );
82
					}
83
					$pure_body = filter_var( $body, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW );
84
					$pure_size = mb_strlen( $pure_body, '8bit' );
85
					$percent_comp = 1 - ( $pure_size / $pre_size );
86
					if ( $percent_comp > 0.05 ) {
87
						$compress_num += 1;
88
						$compress_size += round( ( ( $pre_size - $pure_size ) / 1024 ), 0 );
89
					}
90
				}
91
			}
92
		}
93
		return array(
94
					'count' => $num,
95
					'size' => $size,
96
					'import' => $import,
97
					'media' => $media,
98
					'compress_num' => $compress_num,
99
					'compress_size' => $compress_size,
100
				);
101
	}
102
} ?>
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...
103