Passed
Pull Request — master (#24)
by
unknown
02:33
created

DebugInfoTrait::__debugInfo()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 19
ccs 12
cts 13
cp 0.9231
crap 5.0113
rs 8.8571
1
<?php
2
/**
3
 * @author Boudewijn Schoon <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Itertools\lib\Traits;
8
9
trait DebugInfoTrait
10
{
11
    /**
12
     * This method is called by var_dump() when dumping an object to get the properties that should be shown.
13
     *
14
     * @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
15
     * @return array
16
     */
17 2
    public function __debugInfo()
18
    {
19 2
        if (!($this instanceof \Traversable)) {
20
            return [];
21
        }
22
23 2
        $duplicateKeys = [];
24 2
        $info = ['__length__' => 0];
25 2
        foreach ($this as $key => $value) {
26 2
            $key = (string)$key;
27 2
            while (array_key_exists($key, $info)) {
28 1
                $duplicateKeys[$key] = array_key_exists($key, $duplicateKeys) ? $duplicateKeys[$key] + 1 : 1;
29 1
                $key = sprintf('%s__#%s__', $key, $duplicateKeys[$key]);
30
            }
31 2
            $info[$key] = $value;
32 2
            $info['__length__'] += 1;
33
        }
34 2
        return $info;
35
    }
36
}
37