Issues (40)

php-src/Converters/CamelCaseConverter.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace kalanis\Restful\Converters;
4
5
6
use kalanis\Restful\Utils\Strings;
7
use Traversable;
8
9
10
/**
11
 * CamelCaseConverter
12
 * @package kalanis\Restful\Converters
13
 * @template TK of string
14
 * @template TVal of mixed
15
 * @implements IConverter<TK, TVal>
16
 */
17 1
class CamelCaseConverter implements IConverter
18
{
19
20
    /**
21
     * Converts resource data keys to camelCase
22
     * @param array<TK, TVal> $resource
23
     * @return array<TK, TVal>
24
     */
25
    public function convert(array $resource): array
26
    {
27 1
        $this->convertToCamel($resource);
28 1
        return $resource;
29
    }
30
31
    /**
32
     * Convert array keys to camel case
33
     * @param array<TK, TVal>|Traversable<TK, TVal> $array
34
     */
35
    private function convertToCamel(iterable &$array): void
36
    {
37 1
        if ($array instanceof Traversable) {
38
            $array = iterator_to_array($array);
39
        }
40
41 1
        foreach (array_keys($array) as $key) {
0 ignored issues
show
It seems like $array can also be of type iterable; however, parameter $array of array_keys() does only seem to accept array, 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

41
        foreach (array_keys(/** @scrutinizer ignore-type */ $array) as $key) {
Loading history...
42 1
            $value = &$array[$key];
43 1
            unset($array[$key]);
44
45 1
            $transformedKey = Strings::toCamelCase($key);
46 1
            if (is_iterable($value)) {
47
                $this->convertToCamel($value);
48
            }
49 1
            $array[$transformedKey] = $value;
50 1
            unset($value);
51
        }
52 1
    }
53
}
54