Issues (26)

src/Components/Body.php (6 issues)

1
<?php
2
3
namespace Raptor\Request\Components;
4
5
class Body
6
{
7
    /**
8
     * Request body parameters ($_POST).
9
     *
10
     * @var array
11
     */
12
    protected $param;
13
14
    /**
15
     * Uploaded files ($_FILES).
16
     *
17
     * @var array
18
     */
19
    protected $files;
20
21
    /**
22
     * Content length ($_SERVER['CONTENT_LENGTH']).
23
     *
24
     * @var array
25
     */
26
    protected $contentLength;
27
28
    /**
29
     * Content type ($_SERVER['CONTENT_TYPE']).
30
     *
31
     * @var array
32
     */
33
    protected $contentType;
34
35
    /**
36
     * Get request body parameters.
37
     *
38
     * @param  string $key (optional)
39
     * @param  mixed $default (optional)
40
     * @return  mixed
41
     */
42
    public function param($key = null, $default = null)
43
    {
44
        if ($this->param === null) {
45
            $this->param = $_POST;
46
            if (
47
                isset($_SERVER['CONTENT_TYPE']) && 
48
                $_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded'
49
            ) {
50
                parse_str(file_get_contents("php://input"), $this->param);
0 ignored issues
show
$this->param of type void is incompatible with the type array|null expected by parameter $arr of parse_str(). ( Ignorable by Annotation )

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

50
                parse_str(file_get_contents("php://input"), /** @scrutinizer ignore-type */ $this->param);
Loading history...
51
            }
52
        }
53
        if ($key === null) {
54
            return $this->param;
55
        }
56
        return isset($this->param[$key]) ? $this->param[$key] : $default;
57
    }
58
59
    /**
60
     * Get uploaded files.
61
     *
62
     * @param  string $key (optional)
63
     * @return  array
64
     */
65
    public function files($key = null)
66
    {
67
        if ($this->files === null) {
68
            $this->files = $_FILES;
69
        }
70
        if ($key === null) {
71
            return $this->files;
72
        }
73
        return isset($this->files[$key]) ? $this->files[$key] : null;
74
    }
75
76
    /**
77
     * Get the content length.
78
     *
79
     * @return int
80
     */
81
    public function contentLength()
82
    {
83
        if ($this->contentLength) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->contentLength 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...
84
            return $this->contentLength;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->contentLength returns the type array which is incompatible with the documented return type integer.
Loading history...
85
        }
86
        return $this->contentLength = isset($_SERVER['CONTENT_LENGTH']) ? (int) $_SERVER['CONTENT_LENGTH'] : null;
0 ignored issues
show
Documentation Bug introduced by
It seems like IssetNode ? (int)$_SERVE...CONTENT_LENGTH'] : null can also be of type integer. However, the property $contentLength is declared as type array. 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...
87
    }
88
89
    /**
90
     * Get the content type.
91
     *
92
     * @return string
93
     */
94
    public function contentType()
95
    {
96
        if ($this->contentType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->contentType 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...
97
            return $this->contentType;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->contentType returns the type array which is incompatible with the documented return type string.
Loading history...
98
        }
99
        return $this->contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : null;
100
    }
101
}