Test Setup Failed
Push — master ( 44d819...9dd233 )
by Php Easy Api
03:26
created

Http::httpMethodData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 13
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 25
rs 9.8333
1
<?php
2
3
namespace Resta\Support;
4
5
class Http
6
{
7
    /**
8
     * @return void|mixed
9
     */
10
    public function getInputData()
11
    {
12
        global /** @noinspection PhpUnusedLocalVariableInspection */
13
        $_PUT;
14
15
        /* PUT data comes in on the stdin stream */
16
        $putdata = fopen("php://input", "r");
17
18
        $raw_data = '';
19
20
        /* Read the data 1 KB at a time
21
           and write to the file */
22
        while ($chunk = fread($putdata, 1024))
0 ignored issues
show
Bug introduced by
It seems like $putdata can also be of type false; however, parameter $handle of fread() 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

22
        while ($chunk = fread(/** @scrutinizer ignore-type */ $putdata, 1024))
Loading history...
23
            $raw_data .= $chunk;
24
25
        /* Close the streams */
26
        fclose($putdata);
0 ignored issues
show
Bug introduced by
It seems like $putdata can also be of type false; however, parameter $handle of fclose() 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

26
        fclose(/** @scrutinizer ignore-type */ $putdata);
Loading history...
27
28
        // Fetch content and determine boundary
29
        $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));
30
31
        if(empty($boundary)){
32
            parse_str($raw_data,$data);
33
            $GLOBALS[ '_PUT' ] = $data;
34
            return;
35
        }
36
37
        // Fetch each part
38
        $parts = array_slice(explode($boundary, $raw_data), 1);
39
        $data = array();
40
41
        foreach ($parts as $part) {
42
            // If this is the last part, break
43
            if ($part == "--\r\n") break;
44
45
            // Separate content from headers
46
            $part = ltrim($part, "\r\n");
47
            list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);
48
49
            // Parse the headers list
50
            $raw_headers = explode("\r\n", $raw_headers);
51
            $headers = array();
52
            foreach ($raw_headers as $header) {
53
                list($name, $value) = explode(':', $header);
54
                $headers[strtolower($name)] = ltrim($value, ' ');
55
            }
56
57
            // Parse the Content-Disposition to get the field name, etc.
58
            if (isset($headers['content-disposition'])) {
59
                $filename = null;
60
                $tmp_name = null;
61
                preg_match(
62
                    '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/',
63
                    $headers['content-disposition'],
64
                    $matches
65
                );
66
                /** @noinspection PhpUnusedLocalVariableInspection */
67
                list(, $type, $name) = $matches;
68
69
                //Parse File
70
                if( isset($matches[4]) )
71
                {
72
                    //if labeled the same as previous, skip
73
                    if( isset( $_FILES[ $matches[ 2 ] ] ) )
74
                    {
75
                        continue;
76
                    }
77
78
                    //get filename
79
                    $filename = $matches[4];
80
81
                    //get tmp name
82
                    $filename_parts = pathinfo( $filename );
83
                    $tmp_name = tempnam( ini_get('upload_tmp_dir'), $filename_parts['filename']);
84
85
                    //populate $_FILES with information, size may be off in multibyte situation
86
                    /** @noinspection PhpUndefinedVariableInspection */
87
                    $_FILES[ $matches[ 2 ] ] = array(
88
                        'error'=>0,
89
                        'name'=>$filename,
90
                        'tmp_name'=>$tmp_name,
91
                        'size'=>strlen( $body ),
92
                        'type'=>$value
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.
Loading history...
93
                    );
94
95
                    //place in temporary directory
96
                    file_put_contents($tmp_name, $body);
97
                }
98
                //Parse Field
99
                else
100
                {
101
                    $data[$name] = substr($body, 0, strlen($body) - 2);
102
                }
103
            }
104
105
        }
106
        $GLOBALS[ '_PUT' ] = $data;
107
        return $data;
108
    }
109
110
    /**
111
     * @param string $method
112
     * @return array
113
     */
114
    public function httpMethodData($method='get')
115
    {
116
        $body = [];
117
        if(httpMethod()==$method){
118
            $rawData = json_decode(request()->getContent(),1);
119
120
            $body['params'] = get();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $body['params'] is correct as get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121
122
            if(is_null($rawData)){
123
                $inputData = $this->getInputData();
124
125
                if(!is_null($inputData)){
126
                    $body['body']['form-data'] = $this->getInputData();
127
                }
128
                else{
129
                    $body['body']['x-www-form-urlencoded'] = request()->request->all();
130
                }
131
132
            }
133
            else{
134
                $body['body']['raw-data'] = $rawData;
135
            }
136
        }
137
138
        return $body;
139
    }
140
}