Issues (10)

src/OAuth1/Util.php (2 issues)

1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\OAuth1;
9
10
class Util
11
{
12
    /**
13
     * @param mixed $input
14
     * @return array|string
15
     */
16 6
    public static function urlencodeRFC3986($input)
17
    {
18 6
        if (is_int($input)) {
19 5
            return (string) $input;
20
        }
21
22 6
        if (is_array($input)) {
23 6
            return array_map(array(
24 6
                __NAMESPACE__ . '\Util',
25
                'urlencodeRFC3986'
26
            ), $input);
27
        }
28
29 6
        if (is_scalar($input)) {
30 6
            return rawurlencode($input);
31
        }
32
33
        $type = gettype($input);
34
        throw new \InvalidArgumentException("Unsupported type: {$type}");
35
    }
36
37
    /**
38
     * This decode function isn't taking into consideration the above
39
     * modifications to the encoding process. However, this method doesn't
40
     * seem to be used anywhere so leaving it as is.
41
     *
42
     * @param string $string
43
     * @return string
44
     */
45
    public static function urldecodeRFC3986($string)
46
    {
47
        return urldecode($string);
48
    }
49
50
    /**
51
     * @param array $params
52
     * @return string
53
     */
54 5
    public static function buildHttpQuery(array $params)
55
    {
56 5
        if (!$params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params 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...
57
            return '';
58
        }
59
60 5
        $keys   = self::urlencodeRFC3986(array_keys($params));
61 5
        $values = self::urlencodeRFC3986(array_values($params));
62 5
        $params = array_combine($keys, $values);
63
64
        // Parameters are sorted by name, using lexicographical byte value ordering.
65
        // Ref: Spec: 9.1.1 (1)
66 5
        uksort($params, 'strcmp');
0 ignored issues
show
It seems like $params can also be of type false; however, parameter $array of uksort() does only seem to accept array, 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

66
        uksort(/** @scrutinizer ignore-type */ $params, 'strcmp');
Loading history...
67
68 5
        $pairs = [];
69
70 5
        foreach ($params as $parameter => $value) {
71 5
            if (is_array($value)) {
72
                // If two or more parameters share the same name, they are sorted by their value
73
                // Ref: Spec: 9.1.1 (1)
74
                // June 12th, 2010 - changed to sort because of issue 164 by hidetaka
75
                sort($value, SORT_STRING);
76
                foreach ($value as $duplicate_value) {
77
                    $pairs[] = $parameter . '=' . $duplicate_value;
78
                }
79
            } else {
80 5
                $pairs[] = $parameter . '=' . $value;
81
            }
82
        }
83
84
        // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
85
        // Each name-value pair is separated by an '&' character (ASCII code 38)
86 5
        return implode('&', $pairs);
87
    }
88
}
89