Completed
Pull Request — master (#445)
by
unknown
01:33
created

ParamHelpers::cleanParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Tools\Traits;
4
5
trait ParamHelpers {
6
7
    /**
8
     * Create proper arrays from dot-noted parameter names
9
     *
10
     * @param array $params
11
     * @return array
12
     */
13
    protected function cleanParams(array $params)
14
    {
15
        $values = [];
16
        foreach ($params as $name => $details) {
17
            $this->cleanValueFrom($name, $description['value'], $values);
0 ignored issues
show
Bug introduced by
The variable $description does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
18
        }
19
        return $values;
20
    }
21
22
    /**
23
     * Converts dot notation names to arrays and sets the value at the right depth
24
     *
25
     * @param string $name
26
     * @param mixed $value
27
     * @param array $values The array that holds the result
28
     * @return void
29
     */
30
    protected function cleanValueFrom($name, $value, array &$values = [])
31
    {
32
        if (str_contains($name, '[')) {
33
            $name = str_replace(['][', '[', ']', '..'], ['.', '.', '', '.*.'], $name);
34
        }
35
        array_set($values, str_replace('.*', '.0', $name), $value);
36
    }
37
}
38