Passed
Push — master ( 7c83a0...cd350b )
by Thierry
02:08
created

ArgumentManager::__convertStringToBool()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * ArgumentManager.php
5
 *
6
 * This class processes the input arguments from the GET or POST data of the request.
7
 * If this is a request for the initial page load, no arguments will be processed.
8
 * During a jaxon request, any arguments found in the GET or POST will be converted to a PHP array.
9
 *
10
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
11
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
14
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
15
 * @author Thierry Feuzeu <[email protected]>
16
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
17
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
18
 * @copyright 2016 Thierry Feuzeu <[email protected]>
19
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
20
 * @link https://github.com/jaxon-php/jaxon-core
21
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
22
23
namespace Jaxon\Request\Handler;
24
25
use Jaxon\Utils\Config\Config;
26
use Jaxon\Utils\Translation\Translator;
27
use Jaxon\Exception\RequestException;
28
29
use function strcasecmp;
30
use function is_numeric;
31
use function is_string;
32
use function is_array;
33
use function is_bool;
34
use function substr;
35
use function strlen;
36
use function floor;
37
use function json_decode;
38
use function call_user_func;
39
use function array_walk;
40
use function function_exists;
41
use function iconv;
42
use function mb_convert_encoding;
43
use function utf8_decode;
44
45
class ArgumentManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ArgumentManager
Loading history...
46
{
47
    /*
48
     * Request methods
49
     */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
50
    const METHOD_UNKNOWN = 0;
51
    const METHOD_GET = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
    const METHOD_POST = 2;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53
54
    /**
55
     * @var Config
56
     */
57
    protected $xConfig;
58
59
    /**
60
     * @var Translator
61
     */
62
    protected $xTranslator;
63
64
    /**
65
     * An array of arguments received via the GET or POST parameter jxnargs.
66
     *
67
     * @var array
68
     */
69
    private $aArgs;
70
71
    /**
72
     * Stores the method that was used to send the arguments from the client.
73
     * Will be one of: self::METHOD_UNKNOWN, self::METHOD_GET, self::METHOD_POST.
74
     *
75
     * @var integer
76
     */
77
    private $nMethod;
78
79
    /**
80
     * The function which decodes utf8 string.
81
     *
82
     * @var callable
83
     */
84
    private $cUtf8Decoder;
85
86
    /**
87
     * The constructor
88
     *
89
     * Get and decode the arguments of the HTTP request
90
     *
91
     * @param Config $xConfig
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
92
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     */
94
    public function __construct(Config $xConfig, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
95
    {
96
        $this->xConfig = $xConfig;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
97
        $this->xTranslator = $xTranslator;
98
        $this->aArgs = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
99
        $this->nMethod = self::METHOD_UNKNOWN;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
100
101
        if(isset($_POST['jxnargs']))
102
        {
103
            $this->nMethod = self::METHOD_POST;
104
            $this->aArgs = $_POST['jxnargs'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105
        }
106
        elseif(isset($_GET['jxnargs']))
107
        {
108
            $this->nMethod = self::METHOD_GET;
109
            $this->aArgs = $_GET['jxnargs'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
110
        }
111
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
112
113
    /**
114
     * Return the method that was used to send the arguments from the client
115
     *
116
     * The method is one of: self::METHOD_UNKNOWN, self::METHOD_GET, self::METHOD_POST.
117
     *
118
     * @return int
119
     */
120
    public function getRequestMethod(): int
121
    {
122
        return $this->nMethod;
123
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
124
125
    /**
126
     * Return true if the current request method is GET
127
     *
128
     * @return bool
129
     */
130
    public function requestMethodIsGet(): bool
131
    {
132
        return ($this->getRequestMethod() === self::METHOD_GET);
133
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
134
135
    /**
136
     * Converts a string to a bool var
137
     *
138
     * @param string $sValue    The string to be converted
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
139
     *
140
     * @return bool
141
     */
142
    private function __convertStringToBool(string $sValue): bool
0 ignored issues
show
Coding Style introduced by
Method name "ArgumentManager::__convertStringToBool" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
143
    {
144
        if(strcasecmp($sValue, 'true') === 0)
145
        {
146
            return true;
147
        }
148
        if(strcasecmp($sValue, 'false') === 0)
149
        {
150
            return false;
151
        }
152
        if(is_numeric($sValue))
153
        {
154
            return ($sValue !== '0');
155
        }
156
        return false;
157
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
158
159
    /**
160
     * Convert a Jaxon request argument to its value
161
     *
162
     * Depending on its first char, the Jaxon request argument is converted to a given type.
163
     *
164
     * @param string $sValue    The keys of the options in the file
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
165
     *
166
     * @return string|bool|integer|double|null
167
     */
168
    private function __convertValue(string $sValue)
0 ignored issues
show
Coding Style introduced by
Method name "ArgumentManager::__convertValue" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
169
    {
170
        $cType = substr($sValue, 0, 1);
171
        $sValue = substr($sValue, 1);
172
        switch($cType)
173
        {
174
        case 'S':
175
            $value = !$sValue ? '' : $sValue;
176
            break;
177
        case 'B':
178
            $value = $this->__convertStringToBool($sValue);
179
            break;
180
        case 'N':
181
            $value = ($sValue == floor($sValue) ? (int)$sValue : (float)$sValue);
0 ignored issues
show
Bug introduced by
$sValue of type string is incompatible with the type double|integer expected by parameter $num of floor(). ( Ignorable by Annotation )

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

181
            $value = ($sValue == floor(/** @scrutinizer ignore-type */ $sValue) ? (int)$sValue : (float)$sValue);
Loading history...
182
            break;
183
        case '*':
184
        default:
185
            $value = null;
186
            break;
187
        }
188
        return $value;
189
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
190
191
    /**
192
     * Decode and convert an Jaxon request argument from JSON
193
     *
194
     * @param string $sArg    The Jaxon request argument
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
195
     *
196
     * @return void
197
     */
198
    private function __argumentDecode(string &$sArg)
0 ignored issues
show
Coding Style introduced by
Method name "ArgumentManager::__argumentDecode" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
199
    {
200
        if($sArg === '')
201
        {
202
            return;
203
        }
204
205
        // Arguments are url encoded when uploading files
206
        $sType = 'multipart/form-data';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
207
        $nLen = strlen($sType);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
208
        $sContentType = '';
209
        if(isset($_SERVER['CONTENT_TYPE']))
210
        {
211
            $sContentType = substr($_SERVER['CONTENT_TYPE'], 0, $nLen);
212
        }
213
        elseif(isset($_SERVER['HTTP_CONTENT_TYPE']))
214
        {
215
            $sContentType = substr($_SERVER['HTTP_CONTENT_TYPE'], 0, $nLen);
216
        }
217
        if($sContentType == $sType)
218
        {
219
            $sArg = urldecode($sArg);
220
        }
221
222
        $data = json_decode($sArg, true);
223
224
        if($data !== null && $sArg != $data)
225
        {
226
            $sArg = $data;
227
        }
228
        else
229
        {
230
            $sArg = $this->__convertValue($sArg);
231
        }
232
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
233
234
    /**
235
     * Decode an Jaxon request argument from UTF8
236
     *
237
     * @param array $aDst    An array to store the decoded arguments
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
238
     * @param string $sKey    The key of the argument being decoded
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
239
     * @param string|array $mValue    The value of the argument being decoded
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
240
     *
241
     * @return void
242
     */
243
    private function _decode_utf8_argument(array &$aDst, string $sKey, $mValue)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
244
    {
245
        // Decode the key
246
        $sDestKey = call_user_func($this->cUtf8Decoder, $sKey);
247
248
        if(is_array($mValue))
249
        {
250
            $aDst[$sDestKey] = [];
251
            foreach($mValue as $_sKey => &$_mValue)
252
            {
253
                $this->_decode_utf8_argument($aDst[$sDestKey], $_sKey, $_mValue);
254
            }
255
        }
256
        elseif(is_numeric($mValue) || is_bool($mValue))
257
        {
258
            $aDst[$sDestKey] = $mValue;
259
        }
260
        elseif(is_string($mValue))
0 ignored issues
show
introduced by
The condition is_string($mValue) is always true.
Loading history...
261
        {
262
            $aDst[$sDestKey] = call_user_func($this->cUtf8Decoder, $mValue);
263
        }
264
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
265
266
    /**
267
     * Return the array of arguments that were extracted and parsed from the GET or POST data
268
     *
269
     * @return array
270
     * @throws RequestException
271
     */
272
    public function process(): array
273
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
274
275
        array_walk($this->aArgs, [$this, '__argumentDecode']);
276
277
        if(!$this->xConfig->getOption('core.decode_utf8'))
278
        {
279
            return $this->aArgs;
280
        }
281
        // By default, no decoding
282
        $this->cUtf8Decoder = function($sStr) {
283
            return $sStr;
284
        };
285
        $sEncoding = $this->xConfig->getOption('core.encoding', '');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
286
        if(function_exists('iconv'))
287
        {
288
            $this->cUtf8Decoder = function($sStr) use($sEncoding) {
289
                return iconv("UTF-8", $sEncoding . '//TRANSLIT', $sStr);
290
            };
291
        }
292
        elseif(function_exists('mb_convert_encoding'))
293
        {
294
            $this->cUtf8Decoder = function($sStr) use($sEncoding) {
295
                return mb_convert_encoding($sStr, $sEncoding, "UTF-8");
296
            };
297
        }
298
        elseif($sEncoding == "ISO-8859-1")
299
        {
300
            $this->cUtf8Decoder = function($sStr) {
301
                return utf8_decode($sStr);
302
            };
303
        }
304
        else
305
        {
306
            throw new RequestException($this->xTranslator->trans('errors.request.conversion'));
307
        }
308
309
        $aDst = [];
310
        foreach($this->aArgs as $sKey => &$mValue)
311
        {
312
            $this->_decode_utf8_argument($aDst, $sKey, $mValue);
313
        };
314
        $this->aArgs = $aDst;
315
316
        $this->xConfig->setOption('core.decode_utf8', false);
317
318
        return $this->aArgs;
319
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
320
}
321