Test Failed
Push — develop ( b19cea...603822 )
by nguereza
02:52
created

Request::session()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
    defined('ROOT_PATH') or exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    class Request {
32
		
33
        /**
34
         * The request headers
35
         * @var array
36
         */
37
        private $header = null;
38
39
        /**
40
         * The current request method 'GET', 'POST', 'PUT', etc.
41
         * @var null
42
         */
43
        private $method = null;
44
45
        /**
46
         * The current request URI
47
         * @var string
48
         */
49
        private $requestUri = null;
50
		
51
        /**
52
         * Construct new request instance
53
         */
54
        public function __construct() {
55
            $this->method = $this->server('REQUEST_METHOD');
56
            $this->requestUri = $this->server('REQUEST_URI');
57
            $this->header = array();
58
            //@codeCoverageIgnoreStart
59
            if (function_exists('apache_request_headers')) {
60
                $this->header = apache_request_headers();
0 ignored issues
show
Documentation Bug introduced by
It seems like apache_request_headers() can also be of type false. However, the property $header 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...
61
            } else if (function_exists('getallheaders')) {
62
                $this->header = getallheaders();
63
            }
64
            //@codeCoverageIgnoreEnd
65
        }
66
67
        /**
68
         * Get the request method
69
         * @return string
70
         */
71
        public function method() {
72
            return $this->method;
73
        }
74
		
75
        /**
76
         * Get the request URI
77
         * @return string
78
         */
79
        public function requestUri() {
80
            return $this->requestUri;
81
        }
82
83
        /**
84
         * Get the value from $_REQUEST for given key. if the key is empty will return all values
85
         * @see GlobalVar::request 
86
         */
87
        public function query($key = null, $xss = true) {
88
            return get_instance()->globalvar->request($key, $xss);
89
        }
90
		
91
        /**
92
         * Get the value from $_GET for given key. if the key is empty will return all values
93
         * @see GlobalVar::get 
94
         */
95
        public function get($key = null, $xss = true) {
96
            return get_instance()->globalvar->get($key, $xss);
97
        }
98
		
99
        /**
100
         * Get the value from $_POST for given key. if the key is empty will return all values
101
         * @see GlobalVar::post 
102
         */
103
        public function post($key = null, $xss = true) {
104
            return get_instance()->globalvar->post($key, $xss);
105
        }
106
		
107
        /**
108
         * Get the value from $_SERVER for given key. if the key is empty will return all values
109
         * @see GlobalVar::server 
110
         */
111
        public function server($key = null, $xss = false) {
112
            return get_instance()->globalvar->server($key, $xss);
113
        }
114
		
115
        /**
116
         * Get the value from $_COOKIE for given key. if the key is empty will return all values
117
         *
118
         *  NOTE: This super global is not filter by default
119
         *  
120
         * @see GlobalVar::cookie 
121
         */
122
        public function cookie($key = null, $xss = false) {
123
            return get_instance()->globalvar->cookie($key, $xss);
124
        }
125
		
126
        /**
127
         * Get the value from $_FILES for given key. if the key is empty will return all values
128
         * @see GlobalVar::files 
129
         */
130
        public function file($key, $xss = true) {
131
            return get_instance()->globalvar->files($key, $xss);
132
        }
133
134
        /**
135
         * Get the value for header for given key. if the key is empty will return the all values
136
         *
137
         *  NOTE: This is not filter by default
138
         *  
139
         * @param  string  $key the item key to be fetched
140
         * @param  boolean $xss if need apply some XSS rule on the value
141
         * @return array|mixed       the item value if the key exists or all array if the key is null
142
         */
143
        public function header($key = null, $xss = true) {
144
            $data = null;
145
            if ($key === null) {
146
                //return all
147
                $data = $this->header;
148
            } else if (array_key_exists($key, $this->header)) {
149
                $data = $this->header[$key];
150
            }
151
            if ($xss) {
152
                $data = clean_input($data);
153
            }
154
            return $data;
155
        }
156
157
        /**
158
         * Set the value for header.
159
         * @param  string|array  $key the item key to be set or array if need set the current header  
160
         * by this value
161
         * @param mixed $value the value to set if $key is not an array
162
         * 
163
         * @return object       the current instance
164
         */
165
        public function setHeader($key, $value = null) {
166
            if (is_array($key)) {
167
                //set all
168
                $this->header = $key;
169
            } else {
170
                $this->header[$key] = $value;
171
            }
172
            return $this;
173
        }
174
175
    }
176