Completed
Branch master (c520c0)
by Filipe
01:29
created

UrlEncodedParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of slick/http
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Http\Message\Server\BodyParser;
11
12
use Psr\Http\Message\StreamInterface;
13
use Slick\Http\Message\Server\BodyParserInterface;
14
15
/**
16
 * UrlEncodedParser
17
 *
18
 * @package Slick\Http\Message\Server\BodyParser
19
*/
20
class UrlEncodedParser implements BodyParserInterface
21
{
22
    /**
23
     * @var StreamInterface
24
     */
25
    private $stream;
26
27
    /**
28
     * Creates an URL Encoded Body Parser
29
     *
30
     * @param StreamInterface $stream
31
     */
32
    public function __construct(StreamInterface $stream)
33
    {
34
        $this->stream = $stream;
35
    }
36
37
    /**
38
     * Parses the URL encoded body.
39
     *
40
     * @return array
41
     */
42
    public function parse()
0 ignored issues
show
Coding Style introduced by
parse uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
43
    {
44
        $this->stream->rewind();
45
        parse_str($this->stream->getContents(), $parsed);
46
        return array_merge($_POST, $parsed);
47
    }
48
}