Completed
Push — master ( 725e76...e58edb )
by Marco
04:59
created

HydratorGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types=1);
20
21
namespace GeneratedHydrator\ClassGenerator;
22
23
use CodeGenerationUtils\Visitor\ClassImplementorVisitor;
24
use GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor;
25
use PhpParser\NodeTraverser;
26
use PhpParser\Node\Name;
27
use PhpParser\Node\Stmt\Class_;
28
use PhpParser\Node\Stmt\Namespace_;
29
use ReflectionClass;
30
31
/**
32
 * Generator for highly performing {@see \Zend\Hydrator\HydratorInterface}
33
 * for objects
34
 *
35
 * {@inheritDoc}
36
 *
37
 * @author Marco Pivetta <[email protected]>
38
 * @author Pierre Rineau <[email protected]>
39
 * @license MIT
40
 */
41
class HydratorGenerator implements HydratorGeneratorInterface
42
{
43
    /**
44
     * Generates an AST of {@see \PhpParser\Node[]} out of a given reflection class
45
     * and a map of properties to be used to
46
     *
47
     * @param \ReflectionClass $originalClass
48
     *
49
     * @return \PhpParser\Node[]
50 4
     */
51
    public function generate(ReflectionClass $originalClass) : array
52 4
    {
53
        $ast = [new Class_($originalClass->getShortName())];
54 4
55
        if ($namespace = $originalClass->getNamespaceName()) {
56
            $ast = [new Namespace_(new Name(explode('\\', $namespace)), $ast)];
57 4
        }
58
59 4
        $implementor = new NodeTraverser();
60 4
        $implementor->addVisitor(new HydratorMethodsVisitor($originalClass));
61 4
        $implementor->addVisitor(
62
            new ClassImplementorVisitor($originalClass->getName(), array('Zend\\Hydrator\\HydratorInterface'))
0 ignored issues
show
Bug introduced by
Consider using $originalClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
63 4
        );
64
65
        return $implementor->traverse($ast);
66 4
    }
67
}
68