Completed
Push — master ( 664783...554f9d )
by
unknown
15:59
created

Hash::arrayFilterRecursive()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 5
eloc 7
nc 4
nop 1
crap 30
1
<?php
2
/**
3
 * Hash class file
4
 */
5
6
namespace Graviton\DocumentBundle\Entity;
7
8
/**
9
 * Special type for hash fields
10
 *
11
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link     http://swisscom.ch
14
 */
15
class Hash extends \ArrayObject implements \JsonSerializable
16
{
17
    /**
18
     * Specify data which should be serialized to JSON
19
     *
20
     * @return object
21
     */
22
    public function jsonSerialize()
23
    {
24
        return (object) $this->arrayFilterRecursive($this->getArrayCopy());
25
    }
26
27
    /**
28
     * Clean up, remove empty positions of second level and above
29
     *
30
     * @param array $input to be cleaned up
31
     * @return array
32
     */
33
    private function arrayFilterRecursive($input)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
34
    {
35
        if (empty($input)) {
36
            return [];
37
        }
38
        foreach ($input as &$value) {
39
            if (is_array($value) || is_object($value)) {
40
                $value = $this->arrayFilterRecursive($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type object; however, Graviton\DocumentBundle\...:arrayFilterRecursive() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41
            }
42
        }
43
        return array_filter($input);
44
    }
45
}
46