Completed
Push — master ( 58e367...bcfef7 )
by BENOIT
01:08
created

ArrayValuesNormalizerRenderer::render()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\QueryString\Renderer;
4
5
use BenTools\QueryString\Pairs;
6
use BenTools\QueryString\QueryString;
7
use IteratorIterator;
8
9
final class ArrayValuesNormalizerRenderer implements QueryStringRendererInterface
10
{
11
    /**
12
     * @var NativeRenderer
13
     */
14
    private $renderer;
15
16
    /**
17
     * ArrayValuesStringifier constructor.
18
     */
19
    protected function __construct(QueryStringRendererInterface $renderer = null)
20
    {
21
        $this->renderer = $renderer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $renderer can also be of type object<BenTools\QueryStr...tringRendererInterface>. However, the property $renderer is declared as type object<BenTools\QueryStr...enderer\NativeRenderer>. 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...
22
    }
23
24
    public static function factory(QueryStringRendererInterface $renderer = null)
25
    {
26
        return new self($renderer ?? NativeRenderer::factory());
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function render(QueryString $queryString): string
33
    {
34
        $separator = $this->getSeparator() ?? ini_get('arg_separator.output');
35
        $input = $this->renderer->render($queryString);
36
        $output = '';
37
38
        $iterator = new IteratorIterator(new Pairs($input, false, false, $separator));
39
        $iterator->rewind();
40
        while (true === $iterator->valid()) {
41
            $key = $iterator->key();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $key is correct as $iterator->key() (which targets IteratorIterator::key()) 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...
42
            $value = $iterator->current();
43
            $iterator->next();
44
            $output .= sprintf(
45
                '%s=%s',
46
                preg_replace('/\%5B\d+\%5D/', '%5B%5D', $key),
47
                $value
48
            );
49
            if (false !== $iterator->valid()) {
50
                $output .= $separator;
51
            }
52
        }
53
54
        return $output;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getEncoding(): int
61
    {
62
        return $this->renderer->getEncoding();
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function withEncoding(int $encoding): QueryStringRendererInterface
69
    {
70
        return new self($this->renderer->withEncoding($encoding));
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function getSeparator(): ?string
77
    {
78
        return $this->renderer->getSeparator();
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function withSeparator(?string $separator): QueryStringRendererInterface
85
    {
86
        return new self($this->renderer->withSeparator($separator));
87
    }
88
}
89