Completed
Pull Request — master (#59)
by
unknown
04:53
created

HydratorGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 10
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\Builder\Param;
26
use PhpParser\Node;
27
use PhpParser\NodeTraverser;
28
use PhpParser\Node\Name;
29
use PhpParser\Node\Stmt\Class_;
30
use PhpParser\Node\Stmt\Namespace_;
31
use ReflectionClass;
32
33
/**
34
 * Generator for highly performing {@see \Zend\Hydrator\HydratorInterface}
35
 * for objects
36
 *
37
 * {@inheritDoc}
38
 *
39
 * @author Marco Pivetta <[email protected]>
40
 * @author Pierre Rineau <[email protected]>
41
 * @license MIT
42
 */
43
class HydratorGenerator implements HydratorGeneratorInterface
44
{
45
    /**
46
     * Generates an AST of {@see \PhpParser\Node[]} out of a given reflection class
47
     * and a map of properties to be used to
48
     *
49
     * @param \ReflectionClass $originalClass
50
     *
51
     * @return \PhpParser\Node[]
52
     */
53 4
    public function generate(ReflectionClass $originalClass) : array
54
    {
55 4
        $class = new Class_($originalClass->getShortName());
56
57 4
        $ast = array($class);
58 4
        if ($namespace = $originalClass->getNamespaceName()) {
59 4
            $ast = array(new Namespace_(new Name(explode('\\', $namespace)), $ast));
60
        }
61
62 4
        $implementor = new NodeTraverser();
63 4
        $implementor->addVisitor(new HydratorMethodsVisitor($originalClass));
64 4
        $implementor->addVisitor(
65 4
            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...
66
        );
67
68 4
        return $implementor->traverse($ast);
69
    }
70
}
71