Neo4jResultExtension::doGetType()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 2
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neo4j\Neo4jBundle\Collector\Twig;
6
7
use GraphAware\Neo4j\Client\Formatter\Type\Node;
8
use Twig\Extension\AbstractExtension;
9
10
/**
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
class Neo4jResultExtension extends AbstractExtension
14
{
15
    /**
16
     * {@inheritdoc}
17
     *
18
     * @return array
19
     */
20
    public function getFilters()
21
    {
22
        return [
23
            new \Twig_SimpleFilter('neo4jResult', [$this, 'getType']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: since Twig 2.7, use "Twig\TwigFilter" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
24
        ];
25
    }
26
27
    /**
28
     * @param mixed $object
29
     *
30
     * @return string
31
     */
32 6
    public function getType($object): string
33
    {
34 6
        return $this->doGetType($object, true);
35
    }
36
37
    public function getName(): string
38
    {
39
        return 'neo4j.result';
40
    }
41
42
    /**
43
     * @param mixed $object
44
     * @param bool  $recursive
45
     *
46
     * @return string
47
     */
48 6
    private function doGetType($object, bool $recursive): string
49
    {
50 6
        if ($object instanceof Node) {
51 1
            return sprintf('%s: %s', $object->identity(), implode(', ', $object->labels()));
52 5
        } elseif (is_array($object) && $recursive) {
53 3
            if (empty($object)) {
54 1
                return 'Empty array';
55
            }
56 2
            $ret = [];
57 2
            foreach ($object as $o) {
58 2
                $ret[] = $this->doGetType($o, false);
59
            }
60
61 2
            return sprintf('[%s]', implode(', ', $ret));
62
        }
63
64 4
        return is_object($object) ? get_class($object) : gettype($object);
65
    }
66
}
67