Passed
Pull Request — master (#24)
by Michael
02:52
created

Issues   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 58
c 1
b 0
f 1
dl 0
loc 235
rs 10
wmc 21

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setError() 0 3 1
A getCachedEtag() 0 3 2
A handleHeaderLine() 0 4 1
A getSessPrefix() 0 3 1
A getHeaderFromArray() 0 10 4
A getServiceUrl() 0 3 1
A getsKeyHdrSize() 0 3 1
A __construct() 0 11 1
A getsKeyEtag() 0 3 1
A execCurl() 0 31 1
A getCurlResponse() 0 3 2
A getsKeyResponse() 0 3 1
A getError() 0 3 1
A setSessPrefix() 0 4 1
A getHdrSize() 0 3 1
A getsKeyArray() 0 3 1
1
<?php
2
3
namespace XoopsModules\Tag;
4
5
/*
6
 You may not change or alter any portion of this comment or credits of
7
 supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit
9
 authors.
10
11
 This program is distributed in the hope that it will be useful, but
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 */
15
 /**
16
 * Module: Tag
17
 *
18
 * @package   XoopsModules\Tag
19
 * @author    ZySpec <[email protected]>
20
 * @copyright Copyright (c) 2001-2019 {@link https://xoops.org XOOPS Project}}
21
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
22
 * @since     2.00
23
 */
24
25
/**
26
 * Issues class to collect information from GitHub
27
 *
28
 */
29
 class Issues {
30
    /**
31
     * @var array $hdrs
32
     */
33
    protected $hdrs;
34
    /**
35
     * @var string $dirname module directory name
36
     */
37
    protected $dirname;
38
    /**
39
     * @var string $curl_respones response from involking curl
40
     */
41
    protected $curl_response;
42
    /**
43
     * @var int $hdrSize Curl response header size
44
     */
45
    protected $hdrSize;
46
    /**
47
     * @var string $serviceUrl Service URL for curl
48
     */
49
    protected $serviceUrl;
50
    /**
51
     * @var string $sessPrefix prefix for all SESSION vars
52
     */
53
    protected $sessPrefix;
54
    /**
55
     * @var string $err class error text
56
     */
57
    protected $err;
58
59
    /**
60
     * Constructor
61
     *
62
     * return void
63
     */
64
    public function __construct()
65
    {
66
        $this->hdrs          = [];
67
        $this->curl_response = '';
68
        $this->hdrSize       = 0;
69
        $this->dirname       = basename(dirname(__DIR__));
70
        //$this->serviceUrl    = 'https://api.github.com/repos/xoops/xoopscore25/issues?state=open';
71
        //$this->serviceUrl    = 'https://github.com/zyspec/' . $this->dirname . '/issues?state=open';
72
        $this->serviceUrl    = 'https://api.github.com/repos/XoopsModules25x/' . $this->dirname . '/issues?state=open';
73
        $this->setSessPrefix($this->dirname);
74
        $this->err           = '';
75
    }
76
77
     /**
78
      * Function to put HTTP headers in an array
79
      *
80
      * @param        $curl
81
      * @param string $hdrLine
82
      *
83
      * @return int length of header line put into array
84
      */
85
    public function handleHeaderLine($curl, $hdrLine)
86
    {
87
        $this->hdrs[] = trim($hdrLine);
88
        return strlen($hdrLine);
89
    }
90
91
     /**
92
      * Function to get a header from the header array
93
      *
94
      * @param string $hdr
95
      * @param bool   $asArray
96
      *
97
      * @return array|false array($hdr => value) or false if not found
98
      */
99
    public function getHeaderFromArray($hdr, $asArray = false)
100
    {
101
        $val = '';
102
        foreach ($this->hdrs as $thisHdr) {
103
            if (preg_match("/^{$hdr}/i", $thisHdr)) {
104
                $val = substr($thisHdr, strlen($hdr));
105
                break;
106
            }
107
        }
108
        return (bool)$asArray ? [$hdr => trim($val)] : trim($val);
0 ignored issues
show
Bug Best Practice introduced by
The expression return (bool)$asArray ? ...rim($val)) : trim($val) also could return the type string which is incompatible with the documented return type array|false.
Loading history...
109
    }
110
    /**
111
     * Returns response from involking Curl
112
     *
113
     * @param bool $serialized (default = false)
114
     *
115
     * @return string
116
     */
117
    public function getCurlResponse($serialized = false)
118
    {
119
        return (bool)$serialized ? serialize(base64_encode($this->curl_response)) : $this->curl_response;
120
    }
121
    /**
122
     * Get the size of curl response headers
123
     *
124
     * @return int size of header in bytes
125
     */
126
    public function getHdrSize()
127
    {
128
        return $this->hdrSize;
129
    }
130
    /**
131
     * Get the URL for curl
132
     *
133
     * @return string
134
     */
135
    public function getServiceUrl()
136
    {
137
        return $this->serviceUrl;
138
    }
139
    /**
140
     * Get the Prefix for SESSION variable
141
     *
142
     * @return string
143
     */
144
    public function getSessPrefix()
145
    {
146
        return $this->sessPrefix();
0 ignored issues
show
Bug introduced by
The method sessPrefix() does not exist on XoopsModules\Tag\Issues. ( Ignorable by Annotation )

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

146
        return $this->/** @scrutinizer ignore-call */ sessPrefix();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
    }
148
    /**
149
     * Set the Prefix for SESSION variable
150
     *
151
     * @param string $prefix string to prepend to session variable
152
     *
153
     * @return string prefix
154
     */
155
    public function setSessPrefix($prefix)
156
    {
157
        $this->sessPrefix = htmlspecialchars($prefix, ENT_QUOTES | ENT_HTML5) . '_';
158
        return $this->sessPrefix;
159
    }
160
    /**
161
     * Get the SESSION variable name for Etag key
162
     *
163
     * @return string
164
     */
165
    public function getsKeyEtag()
166
    {
167
        return $this->sessPrefix . 'github_etag';
168
    }
169
    /**
170
     * Get the SESSION variable name for Header Size key
171
     *
172
     * @return string
173
     */
174
    public function getsKeyHdrSize()
175
    {
176
        return $this->sessPrefix . 'github_hdr_size';
177
    }
178
    /**
179
     * Get the SESSION variable name for Response key
180
     *
181
     * @return string
182
     */
183
    public function getsKeyResponse()
184
    {
185
        return $this->sessPrefix . 'github_curl_response';
186
    }
187
188
     /**
189
      * Get the SESSION variable name for Array key
190
      *
191
      * @return array
192
      */
193
    public function getsKeyArray()
194
    {
195
        return [$this->getsKeyEtag(), $this->getsKeyHdrSize(), $this->getsKeyResponse()];
196
    }
197
    /**
198
     * Get the SESSION cached Etag key contents
199
     *
200
     * @return string|bool Etag key or false if tag not set
201
     */
202
    public function getCachedEtag()
203
    {
204
        return isset($_SESSION[$this->getsKeyEtag()]) ? base64_decode(unserialize($_SESSION[$this->getsKeyEtag()])) : false;
205
    }
206
    /**
207
     * Set the error message associated with the latest Curl operation
208
     *
209
     * @param string $msg the error message to save
210
     *
211
     * @return void
212
     */
213
    public function setError($msg)
214
    {
215
        $this->err = $msg;
216
    }
217
    /**
218
     * Get the error message associated with the latest Curl operation
219
     *
220
     * @return string the current error message
221
     */
222
    public function getError()
223
    {
224
        return $this->err;
225
    }
226
    /**
227
     * Execute a curl operation to retrieve data from GitHub server
228
     *
229
     * Also sets the SESSION vars for the curl operation
230
     *
231
     * @return int the current header size from curl
232
     */
233
    public function execCurl()
234
    {
235
        $curl = curl_init($this->getServiceUrl());
236
        curl_setopt_array($curl, [
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

236
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, [
Loading history...
237
                                   CURLOPT_RETURNTRANSFER => true,
238
                                   CURLOPT_HEADER         => true,
239
                                   CURLOPT_VERBOSE        => true,
240
                                   CURLOPT_TIMEOUT        => 5,
241
                                   CURLOPT_HTTPGET        => true,
242
                                   CURLOPT_USERAGENT      => 'XOOPS-' . $this->dirname,
243
                                   CURLOPT_HTTPHEADER     => [
244
                                       'Content-type:application/json',
245
                                                                       'If-None-Match: ' . $this->getCachedEtag()
0 ignored issues
show
Bug introduced by
Are you sure $this->getCachedEtag() of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

245
                                                                       'If-None-Match: ' . /** @scrutinizer ignore-type */ $this->getCachedEtag()
Loading history...
246
                                   ],
247
                                   CURLINFO_HEADER_OUT    => true,
248
                                   CURLOPT_HEADERFUNCTION => [$this, 'handleHeaderLine']
249
                               ]
250
        );
251
        // execute the session
252
        $this->curl_response = curl_exec($curl);
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_exec($curl) can also be of type boolean. However, the property $curl_response is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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

252
        $this->curl_response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
253
        // get the header size and finish off the session
254
        $this->hdrSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_getinfo() 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

254
        $this->hdrSize = curl_getinfo(/** @scrutinizer ignore-type */ $curl, CURLINFO_HEADER_SIZE);
Loading history...
255
        $this->hdrSize = (int)$this->hdrSize;
256
        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

256
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
257
258
        $hdrEtag = $this->getHeaderFromArray('Etag: ');
259
260
        $_SESSION[$this->getsKeyEtag()]     = serialize(base64_encode($hdrEtag));
0 ignored issues
show
Bug introduced by
$hdrEtag of type array<string,string> is incompatible with the type string expected by parameter $data of base64_encode(). ( Ignorable by Annotation )

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

260
        $_SESSION[$this->getsKeyEtag()]     = serialize(base64_encode(/** @scrutinizer ignore-type */ $hdrEtag));
Loading history...
261
        $_SESSION[$this->getsKeyHdrSize()]  = serialize($this->hdrSize);
262
        $_SESSION[$this->getsKeyResponse()] = serialize(base64_encode($this->curl_response));
263
        return $this->hdrSize;
264
    }
265
}
266