Passed
Push — master ( 2137d7...b51f94 )
by Muhammed
37s
created

DebugInfoTrait::__debugInfo()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 17
ccs 12
cts 12
cp 1
crap 5
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
        $duplicateKeys = [];
20 2
        $info = ['__length__' => 0];
21 2
        if ($this instanceof \Traversable) {
22 2
            foreach ($this as $key => $value) {
23 2
                $key = (string)$key;
24 2
                while (array_key_exists($key, $info)) {
25 1
                    $duplicateKeys[$key] = array_key_exists($key, $duplicateKeys) ? $duplicateKeys[$key] + 1 : 1;
26 1
                    $key = sprintf('%s__#%s__', $key, $duplicateKeys[$key]);
27
                }
28 2
                $info[$key] = $value;
29 2
                $info['__length__'] += 1;
30
            }
31
        }
32 2
        return $info;
33
    }
34
}
35