Completed
Pull Request — develop (#609)
by Narcotic
12:02 queued 07:18
created

Hash   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
c 4
b 0
f 0
lcom 0
cbo 0
dl 0
loc 51
ccs 0
cts 19
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 4 1
B arrayFilterRecursive() 0 12 5
B cleanUpArray() 0 14 7
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, [$this, 'cleanUpArray']);
44
    }
45
46
    /**
47
     * Remove NULL values or Empty array object
48
     * @param mixed $var object field value
49
     * @return bool
50
     */
51
    private function cleanUpArray($var)
52
    {
53
        $empty = empty($var);
54
        if ($empty && (
55
                is_int($var) ||
56
                is_bool($var) ||
57
                is_float($var) ||
58
                is_integer($var) ||
59
                is_string($var))
60
        ) {
61
            return true;
62
        }
63
        return !$empty;
64
    }
65
}
66