Completed
Pull Request — master (#133)
by
unknown
21:29
created

AbstractHydratorGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GeneratedHydrator\ClassGenerator;
6
7
use CodeGenerationUtils\Visitor\ClassExtensionVisitor;
8
use GeneratedHydrator\CodeGenerator\Visitor\AbstractHydratorMethodsVisitor;
9
use PhpParser\Node;
10
use PhpParser\Node\Name;
11
use PhpParser\Node\Stmt\Class_;
12
use PhpParser\Node\Stmt\Namespace_;
13
use PhpParser\NodeTraverser;
14
use ReflectionClass;
15
use Zend\Hydrator\AbstractHydrator;
16
use function explode;
17
18
/**
19
 * Generator for highly performing {@see \Zend\Hydrator\HydratorInterface}
20
 * for objects
21
 *
22
 * {@inheritDoc}
23
 */
24
class AbstractHydratorGenerator implements HydratorGenerator
25
{
26
    /**
27
     * Generates an AST of {@see \PhpParser\Node[]} out of a given reflection class
28
     * and a map of properties to be used to
29
     *
30
     * @return Node[]
31
     */
32
    public function generate(ReflectionClass $originalClass): array
33
    {
34
        $ast = [new Class_($originalClass->getShortName())];
35
36
        $namespace = $originalClass->getNamespaceName();
37
        if ($namespace) {
38
            $ast = [new Namespace_(new Name(explode('\\', $namespace)), $ast)];
39
        }
40
41
        $implementor = new NodeTraverser();
42
        $implementor->addVisitor(new AbstractHydratorMethodsVisitor($originalClass));
43
        $implementor->addVisitor(new ClassExtensionVisitor($originalClass->getName(), AbstractHydrator::class));
0 ignored issues
show
Bug introduced by
Consider using $originalClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
44
45
        return $implementor->traverse($ast);
46
    }
47
}
48