Passed
Push — master ( 2434f2...f94701 )
by Nirjhar
08:29
created

PLUGIN_API::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * Implimentation of WordPress inbuilt API class
4
 *
5
 * Usage:
6
 *
7
 * $api = new PLUGIN_API();
8
 * $api->endpoint = 'endpoint_url'
9
 * $api->header = array( "key: $val" )
10
 * $api->data_type = 'xml' or 'json'
11
 * $api->call_type = 'GET' or 'POST'
12
 * $api->call();
13
 * $data = $api->parse();
14
 *
15
 * @author     Nirjhar Lo
16
 * @version    1.2.1
17
 * @package    wp-plugin-framework
18
 */
19
if ( ! class_exists( 'PLUGIN_API' ) ) {
20
21
	class PLUGIN_API {
22
23
24
		/**
25
		 * @var String
26
		 */
27
		public $endpoint;
28
29
30
		/**
31
		 * @var Array
32
		 */
33
		public $header;
34
35
36
		/**
37
		 * @var String
38
		 */
39
		public $data_type;
40
41
42
		/**
43
		 * @var String
44
		 */
45
		public $call_type;
46
47
48
		/**
49
		 * Define the properties inside a instance
50
		 *
51
		 * @return Void
52
		 */
53
		 public function __construct() {
54
55
			 $this->endpoint  = '';
56
			 $this->header    = array();
57
			 $this->data_type = ''; //xml or json
58
			 $this->call_type = '';
59
		 }
60
61
62
		/**
63
		 * Define the necessary database tables
64
		 *
65
		 * @return Array
66
		 */
67
		public function build() {
68
69
			$args = array(
70
						CURLOPT_URL => $this->endpoint,
71
						CURLOPT_RETURNTRANSFER => true,
72
						CURLOPT_ENCODING => "",
73
						CURLOPT_MAXREDIRS => 10,
74
						CURLOPT_TIMEOUT => 30,
75
						CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
76
						CURLOPT_CUSTOMREQUEST => $this->call_type,
77
						CURLOPT_HTTPHEADER => $this->header,
78
						);
79
80
			return $args;
81
		}
82
83
84
		/**
85
		 * Define the variables for db table creation
86
		 *
87
		 * @return Array
88
		 */
89
		public function call() {
90
91
			$curl = curl_init();
92
93
			curl_setopt_array( $curl, $this->build() );
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

93
			curl_setopt_array( /** @scrutinizer ignore-type */ $curl, $this->build() );
Loading history...
94
95
			$result = curl_exec( $curl );
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

95
			$result = curl_exec( /** @scrutinizer ignore-type */ $curl );
Loading history...
96
			$err = curl_error( $curl );
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

96
			$err = curl_error( /** @scrutinizer ignore-type */ $curl );
Loading history...
97
98
			curl_close( $curl );
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

98
			curl_close( /** @scrutinizer ignore-type */ $curl );
Loading history...
99
100
			if ( $err ) {
101
				$result = "cURL Error #:" . $err;
102
			}
103
104
			return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type boolean|string which is incompatible with the documented return type array.
Loading history...
105
		}
106
107
108
		/**
109
		 * Check options and tables and output the info to check if db install is successful
110
		 *
111
		 * @return Array
112
		 */
113
		public function parse($data) {
114
115
			call_user_func( array( $this, $this->data_type ), $data );
116
		}
117
118
119
		/**
120
		 * Parse XML data type
121
		 *
122
		 * @return Array
123
		 */
124
		public function xml( $data ) {
125
126
			libxml_use_internal_errors( true );
127
			$parsed = ( ! $data || $data == '' ? false : simplexml_load_string( $data ) );
128
129
			if ( ! $parsed ) {
130
				return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
131
				libxml_clear_errors();
0 ignored issues
show
Unused Code introduced by
libxml_clear_errors() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
132
			} else {
133
				return $parsed;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parsed returns the type SimpleXMLElement which is incompatible with the documented return type array.
Loading history...
134
			}
135
		}
136
137
138
		/**
139
		 * Parse JSON data type
140
		 *
141
		 * @return Array
142
		 */
143
		public function json( $data ) {
144
145
			$parsed = ( ! $data || $data == '' ? false : json_decode( $data, 1 ) );
146
			return $parsed;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parsed could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
147
		}
148
	}
149
} ?>
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...
150